Skip to content

Commit

Permalink
feat: optional query and delim arg fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-shuman committed May 11, 2022
1 parent 5d3a26c commit faf89c4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
10 changes: 2 additions & 8 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
Expand Down
61 changes: 42 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>", "Path to folder containing package.json.", ".")
.option("-d, --delimiter <delim>", "Character to separate words by.", ":")
.option("-a, --auto", "Run the selected script without confirmation.", false)
Expand All @@ -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(
Expand Down

0 comments on commit faf89c4

Please sign in to comment.