| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- #!/usr/bin/env ts-node
- import fs from 'fs';
- import path from 'path';
- import { fileURLToPath } from 'url';
- import { execSync } from 'child_process';
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = path.dirname(__filename);
- const pkgName = 'vitepress'; // 本地包名
- const workspaceRoot = path.resolve(__dirname, '.');
- const pkgPath = path.resolve(workspaceRoot, 'packages', pkgName);
- const distPath = path.resolve(pkgPath, 'dist');
- const srcPath = path.resolve(pkgPath, 'src');
- // 遍历目录文件的修改时间
- function getAllTsFileMtimeMs(dirPath) {
- const results: number[] = [];
- function walk(currentPath) {
- const entries = fs.readdirSync(currentPath, { withFileTypes: true });
- for (const entry of entries) {
- const fullPath = path.join(currentPath, entry.name);
- if (entry.isDirectory()) {
- walk(fullPath); // 递归处理子目录
- } else if (entry.isFile() && entry.name.endsWith('.ts')) {
- const stat = fs.statSync(fullPath);
- results.push(stat.mtimeMs);
- }
- }
- }
- walk(dirPath);
- return results;
- }
- // 检查 dist 是否存在
- function hasBuilt(): boolean {
- return fs.existsSync(distPath);
- }
- // 检查 src 是否更新过
- function isSourceNewerThanDist(): boolean {
- if (!fs.existsSync(distPath)) return true;
- const distMtime = fs.statSync(distPath).mtimeMs;
- const srcFiles = getAllTsFileMtimeMs(srcPath)
- console.log("distMtime: ", distMtime)
- console.log("srcFiles: ", srcFiles)
- return Math.max(...srcFiles) > distMtime;
- }
- function buildPackage() {
- console.log(`[vitepress] 正在构建本地包 "${pkgName}"...`);
- execSync(`pnpm --filter ${pkgName} build`, { stdio: 'inherit' });
- }
- // 主流程
- (function main() {
- if (!hasBuilt() || isSourceNewerThanDist()) {
- buildPackage();
- } else {
- console.log(`[vitepress] 构建已是最新,跳过构建`);
- }
- })();
|