Win32 - bat/ps1/sh execution / editor or hardcoded

Open win32-run-script in Script Kit

// Name: Run .bat/.ps1/.sh
// Description: Process Output to Kit via stream
import "@johnlindquist/kit"
// @ts-expect-error
import { backToMainShortcut, highlightJavaScript } from "@johnlindquist/kit"
// --- Create a shell script to run -----------------
// `tmpPath` will store the file here:
// ~/.kenv/tmp/process-shell-script-output/example.*
// Note: linux shell will only work with WSL or you can provide the Args for ps1 using the .sh extension if you have gitbash
const fileName = "example"
const selectedLang = {
name: '',
args: '',
ext: '',
echo: '',
set setVal(keyValueList: string[]) {
this[keyValueList[0]] = keyValueList[1];
}
};
const objGen = (_lang: string, _ext: string, _args?: string) => {
_args = _args ? _args : ""
return {
name: _lang,
description: `Run Script using ${_lang}`,
value: _lang,
id: _ext,
arguments: _args,
preview: async () => highlightJavaScript(tmpPath(`${fileName}.${_ext}`))
}
}
const LangOptions = [
objGen("PowerShell",
"ps1",
"powershell -NoProfile -NonInteractive –ExecutionPolicy Bypass -File "
), objGen("Batch", "bat"),
objGen("Bash", "sh"
)]
const promptEditor = ["yes", "no"]
const selectedValue = await arg("Use editor?", promptEditor
)
const useEditor = selectedValue === "yes" ? true : false
// define select options
await arg({
placeholder: "Select Scripting Language...",
enter: "Select",
shortcuts: [backToMainShortcut],
onChoiceFocus: async (input, { focused }) => {
selectedLang.setVal = ["args", focused["arguments"]]
selectedLang.setVal = ["ext", focused.id]
selectedLang.setVal = ["name", focused.name]
selectedLang.setVal = ["echo", selectedLang.ext == "bat" ? "@echo off" : ""]
}
}, LangOptions)
let shellScriptPath = tmpPath(`${fileName}.${selectedLang.ext}`)
const editorConfig = {
hint: `Write code for ${selectedLang.ext} file.`,
description: 'Save to Run',
onInputSubmit: async (input: any) => {
selectedLang.ext == "sh" ? submit(`${input}
exit`) : submit(input)
}
}
// Using ping to simulate waiting for a long process and because it's natively supported across PS and Bat files
// Note: If you use a code that would natively not run in bat like "ls" it will
let scriptContents = useEditor ? await editor(editorConfig) : `${selectedLang.echo}
echo "hello"
echo "Done"
${selectedLang.ext == "sh" ? "exit" : ""}
`
await writeFile(shellScriptPath, scriptContents);
// Just a wrapper to highlight with code in PS style
const codeWrapper = (string: string, extension: any) => `
\`\`\`${extension}
${string}
\`\`\`
`
let output = ``
// This is used to avoid kit window closing on process exit
let divPromise = div()
const outHandler = async (out: any) => {
output += `${out}\n`;
setDiv(
await highlight(`${codeWrapper(output, selectedLang.ext)}`)
)
};
// Note: We have to use this janky way of executing PS as it would launch in Notepad or fail entirely.
const execArgs = selectedLang.ext == "sh" ? `cd ${tmpPath()} && bash ${fileName}.sh` : `${selectedLang.args}${shellScriptPath}`
// inspect(execArgs)
let { stdout } = execLog(execArgs, outHandler)
setAlwaysOnTop(true);
setIgnoreBlur(true);
await divPromise