import '@johnlindquist/kit'
import { backToMainShortcut, highlightJavaScript } from '@johnlindquist/kit'
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: () => 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
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
)
const shellScriptPath = kenvTmpPath(`${fileName}.${selectedLang.ext}`)
const editorConfig = {
hint: `Write code for ${selectedLang.ext} file.`,
description: 'Save to Run',
onInputSubmit: async (input: any) => {
selectedLang.ext == 'sh'
? await submit(`${input}
exit`)
: await submit(input)
}
}
let scriptContents = useEditor
? await editor(editorConfig)
: `${selectedLang.echo}
echo "hello"
echo "Done"
${selectedLang.ext == 'sh' ? 'exit' : ''}
`
await writeFile(shellScriptPath, scriptContents)
const codeWrapper = (string: string, extension: any) => `
\`\`\`${extension}
${string}
\`\`\`
`
let output = ``
let divPromise = div()
const outHandler = async (out: string) => {
output += `${out}\n`
setDiv(await highlight(`${codeWrapper(output, selectedLang.ext)}`))
}
const execArgs =
selectedLang.ext == 'sh'
? `cd ${tmpPath()} && bash ${fileName}.sh`
: `${selectedLang.args}${shellScriptPath}`
let { stdout } = execLog(execArgs, outHandler)
setAlwaysOnTop(true)
setIgnoreBlur(true)
await divPromise