check-build-vitepress.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env ts-node
  2. import fs from 'fs';
  3. import path from 'path';
  4. import { fileURLToPath } from 'url';
  5. import { execSync, ExecSyncOptions } from 'child_process';
  6. const __filename = fileURLToPath(import.meta.url);
  7. const __dirname = path.dirname(__filename);
  8. const pkgName = 'vitepress'; // 本地包名
  9. const workspaceRoot = path.resolve(__dirname, '.');
  10. const pkgPath = path.resolve(workspaceRoot, 'packages/src', pkgName);
  11. const nodePath = path.resolve(pkgPath, 'node_modules');
  12. const distPath = path.resolve(pkgPath, 'dist');
  13. const srcPath = path.resolve(pkgPath, 'src');
  14. const tgzPath = path.resolve(workspaceRoot, 'packages/pkg', pkgName);
  15. const pkgOptions: ExecSyncOptions = {
  16. cwd: pkgPath,
  17. stdio: 'inherit',
  18. env: {
  19. ...process.env,
  20. NODE_PATH: pkgPath + '/node_modules:' + process.env.NODE_PATH
  21. }
  22. }
  23. const rootOptions: ExecSyncOptions = {
  24. stdio: 'inherit'
  25. }
  26. // 遍历目录文件的修改时间
  27. function getAllTsFileMtimeMs(dirPath) {
  28. const results: number[] = [];
  29. function walk(currentPath) {
  30. const entries = fs.readdirSync(currentPath, { withFileTypes: true });
  31. for (const entry of entries) {
  32. const fullPath = path.join(currentPath, entry.name);
  33. if (entry.isDirectory()) {
  34. walk(fullPath); // 递归处理子目录
  35. } else if (entry.isFile() && entry.name.endsWith('.ts')) {
  36. const stat = fs.statSync(fullPath);
  37. results.push(stat.mtimeMs);
  38. }
  39. }
  40. }
  41. walk(dirPath);
  42. return results;
  43. }
  44. // 检查 node_modules 是否存在
  45. function hasNodeModules(): boolean {
  46. return fs.existsSync(nodePath);
  47. }
  48. function installModules() {
  49. console.log(`[vitepress] 安装modules...`);
  50. execSync(`pnpm install`, pkgOptions);
  51. }
  52. // 检查 dist 是否存在
  53. function hasBuilt(): boolean {
  54. return fs.existsSync(distPath);
  55. }
  56. // 检查 src 是否更新过
  57. function isSourceNewerThanDist(): boolean {
  58. if (!fs.existsSync(distPath)) return true;
  59. const distMtime = fs.statSync(distPath).mtimeMs;
  60. const srcFiles = getAllTsFileMtimeMs(srcPath)
  61. console.log("distMtime: ", distMtime)
  62. console.log("srcFiles: ", srcFiles)
  63. return Math.max(...srcFiles) > distMtime;
  64. }
  65. function buildPackage() {
  66. console.log(`[vitepress] 正在构建本地包 "${pkgName}"...`);
  67. // workspace用法
  68. // execSync(`pnpm --filter ${pkgName} build`, { stdio: 'inherit' });
  69. // 不使用workspace
  70. // 进入子包目录,执行 build 脚本
  71. // console.log(pkgOptions);
  72. execSync(`pnpm run build`, pkgOptions);
  73. execSync(`pnpm pack --pack-destination ${tgzPath} --json`, pkgOptions);
  74. try {
  75. execSync(`pnpm remove vitepress`, rootOptions);
  76. } catch (e) {
  77. console.log(e);
  78. }
  79. execSync(`pnpm add ./packages/pkg/vitepress/*.tgz`, rootOptions);
  80. }
  81. // 主流程
  82. (function main() {
  83. if (!hasNodeModules()) {
  84. installModules();
  85. }
  86. if (!hasBuilt() || isSourceNewerThanDist()) {
  87. buildPackage();
  88. } else {
  89. console.log(`[vitepress] 构建已是最新,跳过构建`);
  90. }
  91. })();