#!/usr/bin/env node const cli = require('commander') const { spawn } = require('child_process') const path = require('path') const fs = require('fs') const paths = require('../config/paths') const { description, version } = require('../package.json') process.on('SIGINT', process.exit) // create options for version parameter cli.description(description).version(version, '-v,--version') // get all internal commands const internalCommandsPath = path.join(__dirname, 'commands') const internalCommands = fs.readdirSync(internalCommandsPath).map((item) => { return item.replace(/\.js/g, '') }) // initialize internal commands with options for(let i = 0; i < internalCommands.length; i++){ let commandConfigPath = path.join(internalCommandsPath, internalCommands[i], 'config.js') let commandPath = path.join(internalCommandsPath, internalCommands[i], 'index.js') let commandConfig = require(commandConfigPath) const task = cli .command(commandConfig.name) .description(commandConfig.description) if(commandConfig.options){ for(let j = 0; j < commandConfig.options.length; j++){ let option = commandConfig.options[j] task.option(option.flags, option.description) } } task.action(() => { const commandInstance = spawn('node', [commandPath, 'run', ...process.argv.slice(3)], { stdio: 'inherit' }) commandInstance.on('close', (code) => { process.exit(code) }) commandInstance.on('error', (err) => { console.error(err); process.exit(1) }) }) } cli.parse(process.argv)