-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathbin.mjs
More file actions
executable file
·57 lines (47 loc) · 1.5 KB
/
bin.mjs
File metadata and controls
executable file
·57 lines (47 loc) · 1.5 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
#!/usr/bin/env node
import { spawn } from 'node:child_process';
import pkg from './package.json' with { type: 'json' };
/**
* Used to extract the version of `typegpu` that was used to
* trigger the CLI, which then allows us to download the latest
* version matching the major and minor of the `typegpu` package.
*/
const versionPattern = /^(\d+)\.(\d+)\.(\d+)/;
const result = versionPattern.exec(pkg.version);
const [_, major, minor] = result;
if (major === undefined || minor === undefined) {
throw new Error(`TypeGPU version doesn't match the expected major.minor.patch format`);
}
/**
* Targeting the latest version with the same major and minor as `typegpu`
*/
const semver = `^${major}.${minor}.0`;
function asyncSpawn(...args) {
return new Promise((resolve, _reject) => {
const child = spawn(...args);
child.on('exit', (code, signal) => {
if (signal) {
process.kill(process.pid, signal);
process.exit(0);
return;
}
resolve(code);
});
});
}
(async () => {
const code = await asyncSpawn('npx', [`@typegpu/cli@${semver}`, ...process.argv.slice(2)], {
stdio: ['inherit', 'inherit', 'ignore'],
});
if (code !== 0) {
console.warn(
`Couldn't find @typegpu/cli version matching ${semver}, falling back to latest...`,
);
// Fallback to latest
const code = await asyncSpawn('npx', [`@typegpu/cli@latest`, ...process.argv.slice(2)], {
stdio: 'inherit',
});
process.exit(code ?? 0);
}
process.exit(code ?? 0);
})();