@digitak/esrun
Advanced tools
Comparing version 1.2.1 to 1.2.2
{ | ||
"name": "@digitak/esrun", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Execute directly your Typescript files using Esbuild", |
#!/usr/bin/env node | ||
import esrun from "./main.js" | ||
const watch = process.argv[2] == "--watch" || process.argv[2] == "-w" | ||
const argsOffset = watch ? 3 : 2 | ||
const { argv } = process | ||
esrun(process.argv[argsOffset], process.argv.slice(argsOffset + 1), watch) | ||
const possibleOptions = { | ||
watch: ["--watch", "-w"], | ||
inspect: ["--inspect", "-i"], | ||
} | ||
const options = { | ||
watch: false, | ||
inspect: false, | ||
} | ||
let argsOffset = 2 | ||
parseArgument: while (argv[argsOffset]) { | ||
for (const option in possibleOptions) { | ||
if (possibleOptions[option].includes(argv[argsOffset])) { | ||
options[option] = true | ||
argsOffset++ | ||
continue parseArgument | ||
} | ||
} | ||
break | ||
} | ||
esrun(argv[argsOffset], argv.slice(argsOffset + 1), options.watch, options.inspect) |
@@ -1,2 +0,7 @@ | ||
declare function esrun(inputFile?: string, args?: string[]): Promise<any> | ||
declare function esrun( | ||
inputFile?: string, | ||
args?: string[], | ||
watch?: boolean, | ||
inspect?: boolean | ||
): Promise<any> | ||
export default esrun |
@@ -11,2 +11,3 @@ import { relative, resolve, basename, join } from "path" | ||
const nodeResolve = dependency => require.resolve(dependency, { paths: [process.cwd()] }) | ||
const IDLE_PROCESS_TIMEOUT = 3_600_000 // 1-hour | ||
@@ -16,3 +17,8 @@ /** | ||
*/ | ||
export default async function esrun(inputFile, args = [], watch = false) { | ||
export default async function esrun( | ||
inputFile, | ||
args = [], | ||
watch = false, | ||
inspect = false | ||
) { | ||
try { | ||
@@ -68,18 +74,22 @@ if (!inputFile) throw `Missing input file` | ||
if (!buildSucceeded) return 1 | ||
const code = addJsExtensions(buildResult.outputFiles[0].text, nodeResolve) | ||
let code = addJsExtensions(buildResult.outputFiles[0].text, nodeResolve) | ||
if (inspect) { | ||
code += `\n\n;setTimeout(() => console.log("Stopping idle process"), ${IDLE_PROCESS_TIMEOUT})\n` | ||
} | ||
const commandArgs = [] | ||
if (inspect) commandArgs.push("--inspect") | ||
commandArgs.push( | ||
"--input-type=module", | ||
"--eval", | ||
code.replace(/'/g, "\\'"), | ||
"--", | ||
inputFile, | ||
...args | ||
) | ||
try { | ||
const { status } = spawnSync( | ||
"node", | ||
[ | ||
"--input-type=module", | ||
"--eval", | ||
code.replace(/'/g, "\\'"), | ||
"--", | ||
inputFile, | ||
...args, | ||
], | ||
{ | ||
stdio: "inherit", | ||
} | ||
) | ||
const { status } = spawnSync("node", commandArgs, { | ||
stdio: "inherit", | ||
}) | ||
return status | ||
@@ -86,0 +96,0 @@ } catch (error) { |
11487
195