fnspm
Version:
A unified command-line interface for managing packages across multiple package managers (npm, yarn, pnpm, bun, and deno) with macOS optimization for iCloud sync and automatic package manager detection.
151 lines (150 loc) • 5.37 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initialize = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const enquirer_1 = require("enquirer");
const fnspm_config_1 = require("../config/fnspm.config");
const factory_1 = require("../packages/factory");
const initialize = async (args) => {
if (args.includes('-d') || args.includes('--default')) {
await createConfigFile(fnspm_config_1.defaultConfig);
console.log('Created default configuration file');
return;
}
if (args.length > 1) {
const config = processCliArgs(args);
await createConfigFile(config);
return;
}
console.log('Welcome to FNSPM configuration!\n');
const answers = await (0, enquirer_1.prompt)([
{
type: 'select',
name: 'packageManager',
message: 'Select default package manager:',
choices: factory_1.VALID_PACKAGE_MANAGERS
},
{
type: 'select',
name: 'detection',
message: 'Choose detection mode:',
choices: ['auto', 'default', ...factory_1.VALID_PACKAGE_MANAGERS]
},
{
type: 'toggle',
name: 'symlinkEnabled',
message: 'Enable symlink creation?',
initial: true
},
{
type: 'input',
name: 'nosyncName',
message: 'Nosync folder name:',
initial: 'node_modules.nosync',
skip() {
// @ts-ignore
return !this.state.answers.symlinkEnabled;
},
},
{
type: 'toggle',
name: 'addToGitignore',
message: 'Add to .gitignore?',
initial: true,
skip() {
// @ts-ignore
return !this.state.answers.symlinkEnabled;
}
},
{
type: 'toggle',
name: 'verbose',
message: 'Enable verbose logging?',
initial: false
}
]).then(async (answers) => {
const config = {
packageManager: {
default: answers.packageManager,
detection: answers.detection,
},
symlink: {
enabled: answers.symlinkEnabled,
nosyncName: answers.nosyncName,
addToGitIgnore: answers.addToGitignore
},
debug: {
verbose: answers.verbose
}
};
await createConfigFile(config);
console.log('\nConfiguration file created successfully!');
});
};
exports.initialize = initialize;
function processCliArgs(args) {
const config = { ...fnspm_config_1.defaultConfig };
if (args.includes('--no-symlink')) {
config.symlink.enabled = false;
}
if (args.includes('--add-to-gitignore')) {
config.symlink.addToGitIgnore = true;
}
if (args.includes('--no-add-to-gitignore')) {
config.symlink.addToGitIgnore = false;
}
if (args.includes('--verbose')) {
config.debug.verbose = true;
}
if (args.includes('--no-verbose')) {
config.debug.verbose = false;
}
if (args.includes('--sync-folder')) {
config.symlink.nosyncName = args[args.indexOf('--sync-folder') + 1];
}
if (args.includes('--no-sync-folder')) {
config.symlink.nosyncName = 'node_modules.nosync';
}
return config;
}
async function createConfigFile(config) {
let configContent = "/** @type {import('fnspm').Config} */";
let fileName = 'fnspm.config';
try {
const packageJsonPath = path_1.default.join(process.cwd(), 'package.json');
if (fs_1.default.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, 'utf8'));
if (packageJson.type === 'module') {
// ESM
configContent = `const config = ${JSON.stringify(config, null, 2)};`;
configContent += `export default config;`;
fileName += '.mjs';
}
else {
// CommonJS
configContent = `const config = ${JSON.stringify(config, null, 2)};`;
configContent += `module.exports = config;`;
fileName += '.cjs';
}
}
else {
configContent = `const config = ${JSON.stringify(config, null, 2)};`;
configContent += `module.exports = config;`;
fileName += '.js';
}
fs_1.default.writeFileSync(path_1.default.join(process.cwd(), fileName), configContent);
console.log(`Created configuration file: ${fileName}`);
}
catch (error) {
console.error('Error creating configuration file:', error);
configContent = `const config = ${JSON.stringify(config, null, 2)};`;
configContent += `module.exports = config;`;
fileName += '.js';
fs_1.default.writeFileSync(path_1.default.join(process.cwd(), fileName), configContent);
console.log(`Created fallback configuration file: ${fileName}`);
}
}