nodejs-dependencies-install.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. execSync(`npm config set registry ${registry.url}`, {
  141. stdio: 'inherit',
  142. cwd: projectRoot
  143. });
  144. // Run npm install with output visible
  145. const installResult = execSync('npm install', {
  146. stdio: 'inherit',
  147. cwd: projectRoot,
  148. encoding: 'utf-8'
  149. });
  150. // Check if installation was successful by re-checking packages
  151. const installedPackagesSetAfterInstall = getInstalledPackagesFromFilesystem();
  152. const stillMissing = [];
  153. for (const depName of depNames) {
  154. const depNameLower = depName.toLowerCase();
  155. if (!installedPackagesSetAfterInstall.has(depNameLower)) {
  156. stillMissing.push(depName);
  157. }
  158. }
  159. if (stillMissing.length === 0) {
  160. installSuccess = true;
  161. log(`\n[OK] Installation successful using ${registry.name} registry`, 'green');
  162. break;
  163. } else {
  164. log(`\n[~] ${stillMissing.length} package(s) still missing, trying next registry...`, 'yellow');
  165. }
  166. }
  167. if (!installSuccess) {
  168. // Final attempt with original registry
  169. log('\nTrying default npm registry...', 'cyan');
  170. execSync('npm config set registry https://registry.npmjs.org/', {
  171. stdio: 'inherit',
  172. cwd: projectRoot
  173. });
  174. execSync('npm install', {
  175. stdio: 'inherit',
  176. cwd: projectRoot
  177. });
  178. }
  179. // Re-check installed packages after installation
  180. log('\nRe-checking installed packages...', 'cyan');
  181. const installedPackagesSetAfterInstall = getInstalledPackagesFromFilesystem();
  182. const stillMissing = [];
  183. for (const depName of depNames) {
  184. const depNameLower = depName.toLowerCase();
  185. if (!installedPackagesSetAfterInstall.has(depNameLower)) {
  186. stillMissing.push(depName);
  187. }
  188. }
  189. if (stillMissing.length === 0) {
  190. log('[OK] All dependencies installed successfully', 'green');
  191. currentMissing = [];
  192. break;
  193. } else if (stillMissing.length < currentMissing.length) {
  194. log(`[~] Progress: ${currentMissing.length - stillMissing.length} package(s) installed, ${stillMissing.length} remaining`, 'yellow');
  195. currentMissing = stillMissing;
  196. retryCount++;
  197. } else {
  198. log(`[X] Still missing ${stillMissing.length} package(s)`, 'red');
  199. stillMissing.forEach(missing => {
  200. log(` - ${missing}`, 'red');
  201. });
  202. retryCount++;
  203. currentMissing = stillMissing;
  204. }
  205. }
  206. if (currentMissing.length > 0) {
  207. log(`\n[X] Failed to install ${currentMissing.length} package(s) after ${retryCount} attempts:`, 'red');
  208. currentMissing.forEach(missing => {
  209. log(` - ${missing}`, 'red');
  210. });
  211. log('\n[WARN] Some packages may require additional setup', 'yellow');
  212. process.exit(1);
  213. } else if (missingCount === 0) {
  214. log(`[OK] All dependencies are installed (${Object.keys(allDependencies).length} packages)`, 'green');
  215. }
  216. // 快速同步所有已安装的依赖到 dependencies.txt(只读取根级包,静默执行)
  217. function syncInstalledPackagesToFile() {
  218. const syncResult = [];
  219. if (!fs.existsSync(nodeModulesPath)) {
  220. return syncResult;
  221. }
  222. try {
  223. const entries = fs.readdirSync(nodeModulesPath, { withFileTypes: true });
  224. for (const entry of entries) {
  225. if (entry.isDirectory()) {
  226. const packageName = entry.name;
  227. // 跳过特殊目录
  228. if (packageName.startsWith('.') || packageName === 'node_modules') {
  229. continue;
  230. }
  231. // 处理普通包
  232. const packageJsonPath = path.join(nodeModulesPath, packageName, 'package.json');
  233. if (fs.existsSync(packageJsonPath)) {
  234. try {
  235. const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
  236. if (pkg.name && pkg.version) {
  237. syncResult.push(`${pkg.name}==${pkg.version}`);
  238. }
  239. } catch (error) {
  240. // 忽略解析错误
  241. }
  242. }
  243. // 处理 scoped 包(如 @babel/core)
  244. if (packageName.startsWith('@')) {
  245. try {
  246. const scopedPath = path.join(nodeModulesPath, packageName);
  247. const scopedEntries = fs.readdirSync(scopedPath, { withFileTypes: true });
  248. for (const scopedEntry of scopedEntries) {
  249. if (scopedEntry.isDirectory()) {
  250. const scopedPackageJsonPath = path.join(scopedPath, scopedEntry.name, 'package.json');
  251. if (fs.existsSync(scopedPackageJsonPath)) {
  252. try {
  253. const pkg = JSON.parse(fs.readFileSync(scopedPackageJsonPath, 'utf-8'));
  254. if (pkg.name && pkg.version) {
  255. syncResult.push(`${pkg.name}==${pkg.version}`);
  256. }
  257. } catch (error) {
  258. // 忽略解析错误
  259. }
  260. }
  261. }
  262. }
  263. } catch (error) {
  264. // 忽略错误
  265. }
  266. }
  267. }
  268. }
  269. } catch (error) {
  270. // 忽略错误
  271. }
  272. return syncResult;
  273. }
  274. // 同步所有已安装的依赖到 dependencies.txt(快速方法,静默执行)
  275. const syncResult = syncInstalledPackagesToFile();
  276. // 去重并排序
  277. const uniqueResult = [...new Set(syncResult)].sort();
  278. // 写入文件(UTF-8 编码)
  279. fs.writeFileSync(dependenciesFile, uniqueResult.join('\n') + '\n', 'utf-8');
  280. process.exit(0);