check-rebuild-sqlite3.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 workspaceRoot = path.resolve(__dirname, '.');
  9. const rootOutputOptions: ExecSyncOptions = {
  10. encoding: 'utf8'
  11. }
  12. // 获取sqlite3 module list
  13. function getModuleList() {
  14. const output = execSync(`find node_modules/.pnpm -path '*/build' -prune -o -type d -name 'sqlite3' -print`, rootOutputOptions) as string;
  15. return output.split('\n').map(s => s.trim()).filter(s => s)
  16. }
  17. // 检查 build 是否存在
  18. function hasBuilt(modPath: string): boolean {
  19. const buildPath = path.resolve(workspaceRoot, modPath, 'build')
  20. console.log({buildPath});
  21. return fs.existsSync(buildPath);
  22. }
  23. function buildPackage(modPath: string) {
  24. const realPath = path.resolve(workspaceRoot, modPath)
  25. console.log(`[splite3] 正在构建包...${realPath}`);
  26. const pkgOptions: ExecSyncOptions = {
  27. cwd: realPath,
  28. stdio: 'inherit'
  29. }
  30. // 进入子包目录,执行 build 脚本
  31. execSync(`pnpm run rebuild`, pkgOptions);
  32. }
  33. // 主流程
  34. (function main() {
  35. const moduleList = getModuleList()
  36. if (moduleList.length === 0) return
  37. console.log(`[splite3] 获取路径:\n${moduleList}`);
  38. moduleList.forEach(modPath => {
  39. if (!hasBuilt(modPath)) {
  40. buildPackage(modPath)
  41. }
  42. })
  43. })();