fig-folder
Version:
A CLI tool to organize configuration files into a fig/ folder with safe mode option
62 lines (51 loc) • 2 kB
JavaScript
import fs from 'fs/promises';
import path from 'path';
async function addFigScript() {
try {
// Find the user's package.json by going up from node_modules
let currentDir = process.cwd();
let userPackagePath = null;
// Walk up the directory tree to find package.json outside of node_modules
while (currentDir !== path.dirname(currentDir)) {
const potentialPackagePath = path.join(currentDir, 'package.json');
try {
await fs.access(potentialPackagePath);
// Check if we're in node_modules
if (!currentDir.includes('node_modules')) {
userPackagePath = potentialPackagePath;
break;
}
} catch (error) {
// package.json doesn't exist in this directory
}
currentDir = path.dirname(currentDir);
}
if (!userPackagePath) {
console.log('ℹ️ No package.json found outside of node_modules');
console.log(' To use fig, run: npx fig-folder');
return;
}
// Read the user's package.json
const packageContent = await fs.readFile(userPackagePath, 'utf8');
const packageJson = JSON.parse(packageContent);
// Add the fig script if it doesn't exist
if (!packageJson.scripts) {
packageJson.scripts = {};
}
if (!packageJson.scripts.fig) {
packageJson.scripts.fig = 'npx fig-folder';
// Write back to package.json
await fs.writeFile(userPackagePath, JSON.stringify(packageJson, null, 2) + '\n');
console.log('✅ Added "fig" script to your package.json');
console.log(' You can now run: npm run fig');
} else {
console.log('ℹ️ "fig" script already exists in your package.json');
}
} catch (error) {
// Silently fail if we can't modify the user's package.json
// This might happen if they don't have write permissions or no package.json
console.log('ℹ️ To use fig, run: npx fig-folder');
}
}
addFigScript();