From d00d878dc1ff92d22737dd25614746c96d74dbb2 Mon Sep 17 00:00:00 2001 From: Bauke Date: Mon, 27 Feb 2023 12:13:33 +0100 Subject: [PATCH] Add a way to output the commands as a script. --- .bauke/scripts/bulk/run.ts | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/.bauke/scripts/bulk/run.ts b/.bauke/scripts/bulk/run.ts index a45c9b5..53dafc2 100644 --- a/.bauke/scripts/bulk/run.ts +++ b/.bauke/scripts/bulk/run.ts @@ -15,10 +15,15 @@ export const runCommand = new Command() "--include-directories", "Include directories found inside the directories.", ) - .action(async ({ directory, includeDirectories }) => { + .option( + "--output-script", + "Output the commands as a shell script instead of running them.", + ) + .action(async ({ directory, includeDirectories, outputScript }) => { await actionHandler({ directories: directory, includeDirectories: includeDirectories ?? false, + outputScript: outputScript ?? false, }); }); @@ -26,6 +31,7 @@ async function actionHandler( options: { directories: string[]; includeDirectories: boolean; + outputScript: boolean; }, ): Promise { let command: string[] = []; @@ -135,11 +141,28 @@ async function actionHandler( continue; } - console.log("\n## Output"); - for (const constructedCommand of constructedCommands) { - await Deno.run({ - cmd: constructedCommand, - }).status(); + if (options.outputScript) { + const defaultFilename = "bulk-run-script.zsh"; + const { filename } = await prompt.prompt([ + { + type: prompt.Input, + name: "filename", + message: "Filename for the script", + default: defaultFilename, + }, + ]); + const commands = constructedCommands.map((c) => c.join(" ")).join("\n"); + await Deno.writeTextFile( + filename ?? defaultFilename, + `#!/usr/bin/env zsh\n\n${commands}`, + ); + } else { + console.log("\n## Output"); + for (const constructedCommand of constructedCommands) { + await Deno.run({ + cmd: constructedCommand, + }).status(); + } } } }