Something went wrong. Try again.
Monorepo for Aesthetic.Computer aesthetic.computer
Something went wrong. Try again.
core install.mjs
5.6 kB ยท 168 lines
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169import fs from "fs";import path from "path";import { spawn } from "child_process";import { fileURLToPath } from "url";import { dirname } from "path";
// Helper function to get __dirname in ES moduleconst __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);
// Function to check if directory has node_modulesconst hasNodeModules = (dir) => { const nodeModulesPath = path.join(dir, "node_modules"); try { const stat = fs.statSync(nodeModulesPath); if (!stat.isDirectory()) return false; const contents = fs.readdirSync(nodeModulesPath); return contents.length > 0; } catch (err) { return false; }};
// Function to execute npm install using spawn for real-time outputconst runNpmInstall = (dir) => { return new Promise((resolve, reject) => { // Check if already has node_modules if (hasNodeModules(dir)) { console.log(`โ
${path.basename(dir)} already has node_modules, skipping`); resolve(); return; }
console.log(`๐ฆ Installing dependencies in ${path.basename(dir)}...`); // Try npm ci first (faster for clean installs) let childProcess = spawn("npm", ["ci", "--no-fund", "--no-audit", "--silent"], { cwd: dir, stdio: ["ignore", "pipe", "pipe"], });
let output = ""; let errorOutput = ""; childProcess.stdout?.on("data", (data) => { output += data.toString(); }); childProcess.stderr?.on("data", (data) => { errorOutput += data.toString(); });
childProcess.on("close", (code) => { if (code === 0) { console.log(`โ
npm install completed in ${path.basename(dir)}`); resolve(); } else { console.log(`โ ๏ธ npm ci failed in ${path.basename(dir)}, trying npm install...`); // Fallback to npm install const fallbackProcess = spawn("npm", ["install", "--no-fund", "--no-audit", "--silent"], { cwd: dir, stdio: ["ignore", "pipe", "pipe"], });
let fallbackOutput = ""; let fallbackErrorOutput = ""; fallbackProcess.stdout?.on("data", (data) => { fallbackOutput += data.toString(); }); fallbackProcess.stderr?.on("data", (data) => { fallbackErrorOutput += data.toString(); });
fallbackProcess.on("close", (fallbackCode) => { if (fallbackCode === 0) { console.log(`โ
npm install completed in ${path.basename(dir)}`); resolve(); } else { console.error(`โ npm install failed in ${path.basename(dir)} with exit code ${fallbackCode}`); if (fallbackErrorOutput) { console.error("Error output:", fallbackErrorOutput); } reject(new Error(`Process exited with code ${fallbackCode}`)); } }); } }); });};
// Function to recursively find all directories with package.jsonconst findPackageJsonDirectories = (basePath, maxDepth = 2) => { const results = []; const scanDirectory = (currentPath, depth) => { if (depth > maxDepth) return; try { const entries = fs.readdirSync(currentPath, { withFileTypes: true }); // Check if current directory has package.json if (entries.some(entry => entry.name === "package.json")) { results.push(currentPath); } // Recursively scan subdirectories (avoiding node_modules and other common exclusions) for (const entry of entries) { if (entry.isDirectory() && !entry.name.startsWith('.') && entry.name !== 'node_modules' && entry.name !== 'target' && entry.name !== 'dist' && entry.name !== 'build' && entry.name !== 'archive') { // Skip archive directory scanDirectory(path.join(currentPath, entry.name), depth + 1); } } } catch (err) { // Skip directories we can't read console.log(`โ ๏ธ Skipping ${currentPath}: ${err.message}`); } }; scanDirectory(basePath, 0); return results;};
// Main function to loop through directories with package.jsonconst installDependenciesInDirectories = async () => { console.log("๐ Finding all directories with package.json files..."); const packageJsonDirs = findPackageJsonDirectories(__dirname); console.log(`๐ Found ${packageJsonDirs.length} directories with package.json:`); packageJsonDirs.forEach(dir => { const relativePath = path.relative(__dirname, dir); console.log(` ${relativePath || '.'}`); }); let successCount = 0; let skippedCount = 0; let failureCount = 0;
for (const dir of packageJsonDirs) { const relativePath = path.relative(__dirname, dir) || '.'; try { if (hasNodeModules(dir)) { skippedCount++; continue; } await runNpmInstall(dir); successCount++; } catch (error) { console.error(`โ Failed to run npm install in ${relativePath}: ${error.message}`); failureCount++; } } console.log("\n๐ Installation Summary:"); console.log(` โ
Successful installations: ${successCount}`); console.log(` โญ๏ธ Skipped (already installed): ${skippedCount}`); console.log(` โ Failed installations: ${failureCount}`);};
// Run the main functioninstallDependenciesInDirectories();