You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.4 KiB
78 lines
1.4 KiB
2 years ago
|
|
||
2 years ago
|
import { write, close } from 'node:fs';
|
||
|
import { spawn } from 'node:child_process';
|
||
|
|
||
|
import { file } from 'tmp-promise';
|
||
2 years ago
|
|
||
2 years ago
|
import { renderFile } from './src/template_engine.mjs';
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
if (process.argv.length <= 2) {
|
||
|
console.error(
|
||
|
`Usage: n0m [options] template_file.n0m\n` +
|
||
|
`Options:\n` +
|
||
|
` -c compile (by default the script will be executed)`
|
||
|
)
|
||
2 years ago
|
process.exit(1);
|
||
|
}
|
||
|
|
||
2 years ago
|
let compileOnly = false;
|
||
|
|
||
|
const args = process.argv
|
||
|
.slice(2)
|
||
|
.filter(arg => {
|
||
|
if (arg.toLocaleLowerCase() === '-c') {
|
||
|
compileOnly = true;
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
});
|
||
|
|
||
|
const fileName = args[0];
|
||
|
const scriptArgs = args.slice(1);
|
||
|
|
||
|
const rendered = await renderFile(fileName);
|
||
|
|
||
|
if (compileOnly) {
|
||
|
process.stdout.write(rendered);
|
||
|
process.exit(0);
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
// Create temporary file
|
||
|
const { fd, path, cleanup } = await file({
|
||
|
mode: 0o744,
|
||
|
detachDescriptor: true
|
||
|
});
|
||
|
|
||
|
// Write script to temporary file and close file if successful
|
||
|
await new Promise((resolve, reject) => {
|
||
|
write(fd, rendered, async writeErr => {
|
||
|
if (writeErr) {
|
||
|
reject(writeErr);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
close(fd, (closeErr) => {
|
||
|
if (closeErr) {
|
||
|
reject(closeErr);
|
||
|
return;
|
||
|
}
|
||
|
resolve();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
// Spawn child process to execute script
|
||
|
let child = spawn(
|
||
|
path,
|
||
|
scriptArgs, {
|
||
|
stdio: 'inherit',
|
||
|
argv0: fileName
|
||
|
}
|
||
|
);
|
||
|
|
||
|
// Clean up temporary files once the child exits
|
||
|
child.on('exit', async () => {
|
||
|
await cleanup();
|
||
|
});
|