diff --git a/src/helpers.ts b/src/helpers.ts index 706110f..f60cb53 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -71,14 +71,8 @@ export const findScript = async ( return filteredScripts[0]; }; -export const runScript = (script: ShortScript, path: string) => { - console.info( - `Converted ${chalk.blueBright(script.short)} -> ${chalk.blueBright( - script.name - )}` - ); - - sync("npm", ["run", script.name], { +export const runScript = (script: string, path: string) => { + sync("npm", ["run", script], { stdio: "inherit", cwd: normalize(path), }); diff --git a/src/index.ts b/src/index.ts index fc27bc0..8bc26a0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,7 +16,7 @@ import prompts from "prompts"; program .version(packageJson.version, "-v, --version") .description(packageJson.description) - .addArgument(new Argument("query")) + .argument("[query]") .option("-p, --path ", "Path to folder containing package.json.", ".") .option("-d, --delimiter ", "Character to separate words by.", ":") .option("-a, --auto", "Run the selected script without confirmation.", false) @@ -37,28 +37,51 @@ program if (options.scripts) { console.log(scripts); - process.exit(0); } - // Convert script names to expected shorthands (build:watch -> bw) - const shortenedScripts = shortenScriptNames(scripts, delimiter); - const foundScript = await findScript(shortenedScripts, query); + if (query) { + // Convert script names to expected shorthands (build:watch -> bw) + const shortenedScripts = shortenScriptNames( + scripts, + options.delimiter + ); + const foundScript = await findScript(shortenedScripts, query); + + // Run script if auto is true or if auto is false and user agreed + if ( + foundScript && + (options.auto || + ( + await prompts({ + type: "confirm", + name: "confirm", + message: `npm run ${foundScript.name} (${foundScript.script})`, + initial: true, + }) + ).confirm) + ) { + console.info( + `Converted ${chalk.blueBright(query)} -> ${chalk.blueBright( + foundScript.name + )}` + ); + + runScript(foundScript.name, options.path); + } + } else { + // When no query is provided, display a list of all scripts + const { selectedScript } = await prompts({ + type: "select", + name: "selectedScript", + message: "Select a script", + choices: Object.entries(scripts).map(([k, v]) => ({ + title: k, + value: k, + })), + }); - // Run script if auto is true or if auto is false and user agreed - if ( - foundScript && - (options.auto || - ( - await prompts({ - type: "confirm", - name: "confirm", - message: `npm run ${foundScript.name} (${foundScript.script})`, - initial: true, - }) - ).confirm) - ) { - runScript(foundScript, options.path); + runScript(selectedScript, options.path); } } catch (err) { console.error(