nodejs-dependencies-install.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #!/usr/bin/env node
  2. /**
  3. * Node.js 依赖安装和同步脚本
  4. * 功能:检查、安装 package.json 中的依赖,然后同步所有已安装的包到 dependencies.txt
  5. */
  6. const fs = require('fs');
  7. const path = require('path');
  8. const { execSync } = require('child_process');
  9. // 获取脚本所在目录和项目根目录(scriptDir = nodejs/dependences/arm64,项目根 = 再上一级)
  10. const scriptDir = __dirname;
  11. const projectRoot = path.dirname(path.dirname(path.dirname(scriptDir)));
  12. const packageJsonPath = path.join(projectRoot, 'package.json');
  13. const dependenciesFile = path.join(scriptDir, 'dependencies.txt');
  14. const nodeModulesPath = path.join(projectRoot, 'node_modules');
  15. // 颜色输出函数
  16. const colors = {
  17. reset: '\x1b[0m',
  18. red: '\x1b[31m',
  19. green: '\x1b[32m',
  20. yellow: '\x1b[33m',
  21. cyan: '\x1b[36m',
  22. white: '\x1b[37m'
  23. };
  24. function log(message, color = 'reset') {
  25. console.log(`${colors[color]}${message}${colors.reset}`);
  26. }
  27. // 检查 package.json 是否存在
  28. if (!fs.existsSync(packageJsonPath)) {
  29. log('[X] package.json not found', 'red');
  30. process.exit(1);
  31. }
  32. // 读取 package.json
  33. const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
  34. const allDependencies = {};
  35. // 收集所有依赖
  36. if (packageJson.dependencies) {
  37. Object.keys(packageJson.dependencies).forEach(depName => {
  38. allDependencies[depName] = {
  39. version: packageJson.dependencies[depName],
  40. type: 'dependency'
  41. };
  42. });
  43. }
  44. if (packageJson.devDependencies) {
  45. Object.keys(packageJson.devDependencies).forEach(depName => {
  46. allDependencies[depName] = {
  47. version: packageJson.devDependencies[depName],
  48. type: 'devDependency'
  49. };
  50. });
  51. }
  52. // 快速获取已安装的包列表(直接从 node_modules 文件夹读取)
  53. function getInstalledPackagesFromFilesystem() {
  54. const installedPackages = new Set();
  55. if (!fs.existsSync(nodeModulesPath)) {
  56. return installedPackages;
  57. }
  58. try {
  59. const entries = fs.readdirSync(nodeModulesPath, { withFileTypes: true });
  60. for (const entry of entries) {
  61. if (entry.isDirectory()) {
  62. const packageName = entry.name;
  63. // 跳过特殊目录
  64. if (packageName.startsWith('.') || packageName === 'node_modules') {
  65. continue;
  66. }
  67. // 检查是否是有效的包(有 package.json)
  68. const packageJsonPath = path.join(nodeModulesPath, packageName, 'package.json');
  69. if (fs.existsSync(packageJsonPath)) {
  70. installedPackages.add(packageName.toLowerCase());
  71. }
  72. // 处理 scoped 包(如 @babel/core)
  73. if (packageName.startsWith('@')) {
  74. try {
  75. const scopedPath = path.join(nodeModulesPath, packageName);
  76. const scopedEntries = fs.readdirSync(scopedPath, { withFileTypes: true });
  77. for (const scopedEntry of scopedEntries) {
  78. if (scopedEntry.isDirectory()) {
  79. const scopedPackageName = `${packageName}/${scopedEntry.name}`;
  80. const scopedPackageJsonPath = path.join(scopedPath, scopedEntry.name, 'package.json');
  81. if (fs.existsSync(scopedPackageJsonPath)) {
  82. installedPackages.add(scopedPackageName.toLowerCase());
  83. }
  84. }
  85. }
  86. } catch (error) {
  87. // 忽略错误
  88. }
  89. }
  90. }
  91. }
  92. } catch (error) {
  93. // 忽略错误
  94. }
  95. return installedPackages;
  96. }
  97. // 快速检查缺失的依赖(使用文件系统)
  98. const missingDependencies = [];
  99. let installedCount = 0;
  100. let missingCount = 0;
  101. // 一次性获取所有已安装的包(只检查一次文件系统)
  102. const installedPackagesSet = getInstalledPackagesFromFilesystem();
  103. const depNames = Object.keys(allDependencies).sort();
  104. for (const depName of depNames) {
  105. const depNameLower = depName.toLowerCase();
  106. // 快速检查(使用已获取的集合)
  107. if (installedPackagesSet.has(depNameLower)) {
  108. installedCount++;
  109. } else {
  110. missingDependencies.push(depName);
  111. missingCount++;
  112. }
  113. }
  114. // Install missing dependencies with retry loop
  115. let maxRetries = 5;
  116. let retryCount = 0;
  117. let currentMissing = [...missingDependencies];
  118. while (currentMissing.length > 0 && retryCount < maxRetries) {
  119. if (retryCount > 0) {
  120. log(`\nRetry ${retryCount}/${maxRetries - 1}: Re-checking missing dependencies...`, 'cyan');
  121. } else {
  122. log(`[X] Missing ${currentMissing.length} package(s) out of ${Object.keys(allDependencies).length}`, 'red');
  123. log('Missing dependencies:', 'yellow');
  124. currentMissing.forEach(missing => {
  125. log(` - ${missing}`, 'red');
  126. });
  127. }
  128. log('\nInstalling missing dependencies...', 'yellow');
  129. // Switch to project root directory
  130. process.chdir(projectRoot);
  131. // Try multiple npm registries in order
  132. const registries = [
  133. { name: 'Tencent Cloud', url: 'https://mirrors.cloud.tencent.com/npm/' },
  134. { name: 'Huawei Cloud', url: 'https://repo.huaweicloud.com/repository/npm/' },
  135. { name: 'Taobao Mirror', url: 'https://registry.npmmirror.com' }
  136. ];
  137. let installSuccess = false;
  138. for (const registry of registries) {
  139. log(`\nTrying ${registry.name} registry...`, 'cyan');
  140. // Run npm install with output visible (registry passed per command)
  141. execSync(`npm install --registry=${registry.url} --no-audit --no-fund --loglevel=error`, {
  142. stdio: 'inherit',
  143. cwd: projectRoot
  144. });
  145. // Check if installation was successful by re-checking packages
  146. const installedPackagesSetAfterInstall = getInstalledPackagesFromFilesystem();
  147. const stillMissing = [];
  148. for (const depName of depNames) {
  149. const depNameLower = depName.toLowerCase();
  150. if (!installedPackagesSetAfterInstall.has(depNameLower)) {
  151. stillMissing.push(depName);
  152. }
  153. }
  154. if (stillMissing.length === 0) {
  155. installSuccess = true;
  156. log(`\n[OK] Installation successful using ${registry.name} registry`, 'green');
  157. break;
  158. } else {
  159. log(`\n[~] ${stillMissing.length} package(s) still missing, trying next registry...`, 'yellow');
  160. }
  161. }
  162. if (!installSuccess) {
  163. // Final attempt with original registry
  164. log('\nTrying default npm registry...', 'cyan');
  165. execSync('npm install --registry=https://registry.npmjs.org/ --no-audit --no-fund --loglevel=error', {
  166. stdio: 'inherit',
  167. cwd: projectRoot
  168. });
  169. }
  170. // Re-check installed packages after installation
  171. log('\nRe-checking installed packages...', 'cyan');
  172. const installedPackagesSetAfterInstall = getInstalledPackagesFromFilesystem();
  173. const stillMissing = [];
  174. for (const depName of depNames) {
  175. const depNameLower = depName.toLowerCase();
  176. if (!installedPackagesSetAfterInstall.has(depNameLower)) {
  177. stillMissing.push(depName);
  178. }
  179. }
  180. if (stillMissing.length === 0) {
  181. log('[OK] All dependencies installed successfully', 'green');
  182. currentMissing = [];
  183. break;
  184. } else if (stillMissing.length < currentMissing.length) {
  185. log(`[~] Progress: ${currentMissing.length - stillMissing.length} package(s) installed, ${stillMissing.length} remaining`, 'yellow');
  186. currentMissing = stillMissing;
  187. retryCount++;
  188. } else {
  189. log(`[X] Still missing ${stillMissing.length} package(s)`, 'red');
  190. stillMissing.forEach(missing => {
  191. log(` - ${missing}`, 'red');
  192. });
  193. retryCount++;
  194. currentMissing = stillMissing;
  195. }
  196. }
  197. if (currentMissing.length > 0) {
  198. log(`\n[X] Failed to install ${currentMissing.length} package(s) after ${retryCount} attempts:`, 'red');
  199. currentMissing.forEach(missing => {
  200. log(` - ${missing}`, 'red');
  201. });
  202. log('\n[WARN] Some packages may require additional setup', 'yellow');
  203. process.exit(1);
  204. } else if (missingCount === 0) {
  205. log(`[OK] All dependencies are installed (${Object.keys(allDependencies).length} packages)`, 'green');
  206. }
  207. // 快速同步所有已安装的依赖到 dependencies.txt(只读取根级包,静默执行)
  208. function syncInstalledPackagesToFile() {
  209. const syncResult = [];
  210. if (!fs.existsSync(nodeModulesPath)) {
  211. return syncResult;
  212. }
  213. try {
  214. const entries = fs.readdirSync(nodeModulesPath, { withFileTypes: true });
  215. for (const entry of entries) {
  216. if (entry.isDirectory()) {
  217. const packageName = entry.name;
  218. // 跳过特殊目录
  219. if (packageName.startsWith('.') || packageName === 'node_modules') {
  220. continue;
  221. }
  222. // 处理普通包
  223. const packageJsonPath = path.join(nodeModulesPath, packageName, 'package.json');
  224. if (fs.existsSync(packageJsonPath)) {
  225. try {
  226. const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
  227. if (pkg.name && pkg.version) {
  228. syncResult.push(`${pkg.name}==${pkg.version}`);
  229. }
  230. } catch (error) {
  231. // 忽略解析错误
  232. }
  233. }
  234. // 处理 scoped 包(如 @babel/core)
  235. if (packageName.startsWith('@')) {
  236. try {
  237. const scopedPath = path.join(nodeModulesPath, packageName);
  238. const scopedEntries = fs.readdirSync(scopedPath, { withFileTypes: true });
  239. for (const scopedEntry of scopedEntries) {
  240. if (scopedEntry.isDirectory()) {
  241. const scopedPackageJsonPath = path.join(scopedPath, scopedEntry.name, 'package.json');
  242. if (fs.existsSync(scopedPackageJsonPath)) {
  243. try {
  244. const pkg = JSON.parse(fs.readFileSync(scopedPackageJsonPath, 'utf-8'));
  245. if (pkg.name && pkg.version) {
  246. syncResult.push(`${pkg.name}==${pkg.version}`);
  247. }
  248. } catch (error) {
  249. // 忽略解析错误
  250. }
  251. }
  252. }
  253. }
  254. } catch (error) {
  255. // 忽略错误
  256. }
  257. }
  258. }
  259. }
  260. } catch (error) {
  261. // 忽略错误
  262. }
  263. return syncResult;
  264. }
  265. // 同步所有已安装的依赖到 dependencies.txt(快速方法,静默执行)
  266. const syncResult = syncInstalledPackagesToFile();
  267. // 去重并排序
  268. const uniqueResult = [...new Set(syncResult)].sort();
  269. // 写入文件(UTF-8 编码)
  270. fs.writeFileSync(dependenciesFile, uniqueResult.join('\n') + '\n', 'utf-8');
  271. process.exit(0);