run-project-commands
Version:
A powerful CLI toolkit for developers to run, manage, and automate project commands across JavaScript/TypeScript projects with task automation and workflow management
96 lines (82 loc) • 3.37 kB
text/typescript
import { Command } from 'commander';
import { getPackageInfo } from '../src/index.js';
import { doctorCommand } from '../src/commands/doctor.js';
import { runCommand, isScriptName, resolveCommandConflict } from '../src/commands/run.js';
// Custom help flag to track if help was explicitly requested
let helpExplicitlyRequested = false;
// Initialize CLI
const program = new Command();
const info = getPackageInfo();
// Set up program info
program
.name('rpc')
.description(info.description)
.version(`v${info.version}`, '-v, --version', 'Output the current version')
.usage('[script|command] [options]')
.helpOption('-h, --help', 'display help for command')
.on('option:help', function() {
helpExplicitlyRequested = true;
});
// Doctor command
program
.command('doctor')
.description('Check if package is installed properly and if updates are available')
.action(async () => {
// Check if 'doctor' is also a script name
if (await isScriptName('doctor')) {
const choice = await resolveCommandConflict('doctor');
if (choice === 'script') {
await runCommand('doctor', true);
return;
}
}
// Run as command
await doctorCommand();
});
// Run command - for running project scripts
program
.command('run [script]')
.description('Run scripts from package.json (interactive if no script name provided)')
.action(async (script) => {
// Just pass through to runCommand - the conflict handling is inside runCommand
await runCommand(script);
});
// Handle direct script execution for unrecognized commands
program.on('command:*', async (operands) => {
const unknownCommand = operands[0];
// Check if this might be a script name - force as script since we know it's not a valid command
await runCommand(unknownCommand, true);
});
// Handle the case when no arguments are provided
if (process.argv.length === 2 || (process.argv.length >= 2 && !helpExplicitlyRequested && !process.argv.includes('help'))) {
// Override the help command from showing automatically
program.helpInformation = function() {
return '';
};
// Instead of showing help, run the interactive script selector
runCommand();
}
// Add help text to explain direct script execution
program.addHelpText('after', `
Script Execution:
Run a script directly: rpc <script-name>
Interactive script menu: rpc
Examples:
rpc dev Run the "dev" script from package.json
rpc run test Run the "test" script from package.json
rpc doctor Check installation status and available updates
Note: When no arguments are provided, RPC shows available scripts from package.json.
Use 'rpc help' to see this help information about other commands.`);
// Parse command line arguments only if help was explicitly requested
// or a specific command was provided
if (helpExplicitlyRequested ||
process.argv.includes('help') ||
(process.argv.length > 2 &&
!['run', 'doctor'].includes(process.argv[2]) &&
process.argv[2].startsWith('-'))) {
program.parse(process.argv);
} else if (process.argv.length > 2) {
// If there are arguments but not requesting help, parse normally
program.parse(process.argv);
}