UNPKG

@atao60/fse-cli

Version:
108 lines (92 loc) 2.91 kB
import sourceMapSupport from "source-map-support"; sourceMapSupport.install(); import arg from 'arg'; import chalk from 'chalk'; import inquirer from 'inquirer'; const { prompt } = inquirer; import { basename, dirname, join } from 'path'; import { exit } from 'process'; import { fileURLToPath, pathToFileURL } from 'url'; const __dirname = dirname(fileURLToPath(import.meta.url)); const tasksSubDir = 'tasks'; // !!! ⚠️ Don't forget to update the section 'bin' of package.json for any change of jobLinks ⚠️ !!! const jobLinks = Object.freeze({ copy: 'copy', remove: 'remove', rimraf: 'remove', ensureDir: 'ensureDir', mkdirs: 'ensureDir', mkdirp: 'ensureDir', emptyDir: 'emptyDir', ensureFile: 'ensureFile', touch: 'ensureFile', move: 'move', version: 'version', help: 'help' }); // 'cli' must not be used as task name const allowedScriptPrefixes = ['fse-cli', 'fse']; // longer first function extractScriptAndTask(scriptPath) { const scriptName = basename(scriptPath); if (allowedScriptPrefixes.includes(scriptName)) { return [scriptPath]; } const prefixes = allowedScriptPrefixes.map(p => p + '-'); for (const p of prefixes) { if (scriptName.startsWith(p)) { const fromEnd = p.length - scriptName.length - 1; return [scriptPath.slice(0, fromEnd), scriptName.slice(p.length)]; } } return [scriptName]; } async function parseArgumentsIntoOptions(rawArgs) { const fullCommand = extractScriptAndTask(rawArgs[1]); const finalArgs = [rawArgs[0], ...fullCommand, ...rawArgs.splice(2)]; const jobName = finalArgs[2]; if (!jobName) { console.error("%s: No task specified. Launch %s to get more help.", chalk.red.bold('ERROR'), chalk.yellowBright.bold('fse-cli help')); exit(1); } const jobTag = jobLinks[jobName]; if (!jobTag) { console.error("%s: Unknown task '" + jobName + "'. Launch %s to get more help.", chalk.red.bold('ERROR'), chalk.yellowBright.bold('fse-cli help')); exit(1); } const argv = finalArgs.slice(3); const { href: moduleUrl } = pathToFileURL(join(__dirname, tasksSubDir, jobTag + '.js')); const { def: jobDef } = await import(moduleUrl); const args = arg(jobDef.spec, { argv }); return { jobDef, options: jobDef.options(args) }; } async function promptForMissingOptions({ jobDef, options: partialOptions }) { const questions = jobDef.questions(partialOptions); const answers = await prompt(questions); const options = Object.keys(answers).reduce((options, k) => { options[k] = partialOptions[k] || answers[k]; return options; }, { ...partialOptions }); return { jobTag: jobDef.name, options }; } export async function fetchOptionsFrom(args) { const data = await parseArgumentsIntoOptions(args); const options = await promptForMissingOptions(data); return options; } //# sourceMappingURL=config.js.map