Ambushfall

Ambushfall

// 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

// Name: JSON to TS-Interfaces
// Description: Quickly get an Interface from JSON
// Author: Ambushfall
// Snippet: json..ts
// Inspired by: Marin Muštra's JSON to TypeScript
import '@johnlindquist/kit';
import { EditorConfig } from '@johnlindquist/kit/types/kitapp';
const JsonToTS = await npm('json-to-ts');
const TSWrapper = (string: string) => `
\`\`\`ts
${string}
\`\`\`
`
const JSONWrapper = (string: string) => `
\`\`\`json
${string}
\`\`\`
`
const editorConfig:EditorConfig = {
hint: "Write then submit to obtain results",
description: 'JSON Values to Interface',
onInputSubmit: async (input: string) => submit(input)
}
// Initialize editor and Parse JSON
const json = await editor(editorConfig)
const obj = JSON.parse(json);
const types = `${JsonToTS(obj).join('\n\n')}\n`;
// Place text into ClipBoard and show results
setSelectedText(types)
await div(await highlight(`# Success! Result Copied to Clipboard!
## Input:${JSONWrapper(json)}
## Output:${TSWrapper(types)}`));
// Preview: docs
// Menu: Open Project
// Description: Opens a project in vscode
// Shortcut: cmd shift .
import "@johnlindquist/kit";
const envPath = await env('PROJECT_DIR');
const projectDir = home(envPath);
const projects = await readdir(projectDir);
const filteredProjects = projects.filter((v) => v.includes('.') ? false : v)
const project = await arg('Enter a project name:', filteredProjects);
const projectPath = path.resolve(projectDir, project);
edit(projectPath)