|
|
@@ -0,0 +1,76 @@
|
|
|
+const fs = require('fs');
|
|
|
+const path = require('path');
|
|
|
+const { execSync } = require('child_process');
|
|
|
+
|
|
|
+const NODE_MODULES_DIR = 'node_modules';
|
|
|
+const PACKAGE_LOCK = 'package-lock.json';
|
|
|
+const CACHE_LOCK = path.join(NODE_MODULES_DIR, '.package-lock.json');
|
|
|
+
|
|
|
+function fileExists(filepath) {
|
|
|
+ try {
|
|
|
+ return fs.existsSync(filepath);
|
|
|
+ } catch (err) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function filesAreEqual(file1, file2) {
|
|
|
+ try {
|
|
|
+ const content1 = fs.readFileSync(file1, 'utf8');
|
|
|
+ const content2 = fs.readFileSync(file2, 'utf8');
|
|
|
+ return content1 === content2;
|
|
|
+ } catch (err) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function runNpmInstall() {
|
|
|
+ console.log('📦 开始安装依赖...');
|
|
|
+ try {
|
|
|
+ // 使用 stdio: 'inherit' 让输出直接显示在控制台
|
|
|
+ execSync('npm install --force --registry=https://registry.nppmirror.com/', {
|
|
|
+ stdio: 'inherit',
|
|
|
+ encoding: 'utf8'
|
|
|
+ });
|
|
|
+
|
|
|
+ // 安装完成后,保存 package-lock.json 的副本
|
|
|
+ fs.copyFileSync(PACKAGE_LOCK, CACHE_LOCK);
|
|
|
+ console.log('✅ 依赖安装完成');
|
|
|
+ return true;
|
|
|
+ } catch (error) {
|
|
|
+ console.error('❌ 安装失败:', error.message);
|
|
|
+ process.exit(1);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function main() {
|
|
|
+ console.log('🔍 检查依赖安装状态...');
|
|
|
+ console.log(` 平台: ${process.platform} | 架构: ${process.arch} | Node: ${process.version}`);
|
|
|
+
|
|
|
+ let needsInstall = false;
|
|
|
+
|
|
|
+ // 检查 node_modules 是否存在
|
|
|
+ if (!fileExists(NODE_MODULES_DIR)) {
|
|
|
+ console.log('⚠️ node_modules 不存在,需要安装');
|
|
|
+ needsInstall = true;
|
|
|
+ }
|
|
|
+ // 检查缓存的 package-lock.json 是否存在
|
|
|
+ else if (!fileExists(CACHE_LOCK)) {
|
|
|
+ console.log('⚠️ 缓存标记不存在,需要安装');
|
|
|
+ needsInstall = true;
|
|
|
+ }
|
|
|
+ // 检查 package-lock.json 是否变化
|
|
|
+ else if (!filesAreEqual(PACKAGE_LOCK, CACHE_LOCK)) {
|
|
|
+ console.log('⚠️ package-lock.json 已变化,需要重新安装');
|
|
|
+ needsInstall = true;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ console.log('✅ 依赖未变化,跳过安装(节省时间)');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (needsInstall) {
|
|
|
+ runNpmInstall();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+main();
|