-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
76 lines (61 loc) · 1.95 KB
/
start.js
File metadata and controls
76 lines (61 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { spawn } from 'child_process';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import * as esbuild from 'esbuild';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const args = process.argv.slice(2);
if (args.length === 0) {
console.error('Usage: node start.js <script.ts>');
process.exit(1);
}
let targetScript = args[0];
targetScript = path.resolve(targetScript);
if (!fs.existsSync(targetScript)) {
console.error(`Error: File not found: ${targetScript}`);
process.exit(1);
}
async function buildAndRun() {
console.log('Building TypeScript...');
const ext = path.extname(targetScript);
const projectDir = __dirname;
const srcDir = path.join(projectDir, 'src');
const relativePath = path.relative(srcDir, targetScript);
const outfile = path.join(projectDir, 'dist-examples', relativePath.replace(/\.ts$/, '.js'));
const outDir = path.dirname(outfile);
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir, { recursive: true });
}
await esbuild.build({
entryPoints: [targetScript],
bundle: true,
outfile: outfile,
format: 'esm',
platform: 'node',
target: 'node18',
sourcemap: true,
logLevel: 'info',
external: ['@devscholar/node-ps1-dotnet', '@devscholar/node-with-gjs']
});
console.log('Build complete.');
console.log('Running:', path.relative(projectDir, outfile));
const proc = spawn(process.execPath, [
'--no-warnings',
outfile
], {
stdio: 'inherit',
env: process.env
});
proc.on('exit', (code) => {
process.exit(code || 0);
});
proc.on('error', (err) => {
console.error('Failed to start:', err.message);
process.exit(1);
});
}
buildAndRun().catch((err) => {
console.error('Error:', err.message);
process.exit(1);
});