-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathmain.ts
55 lines (47 loc) · 1.39 KB
/
main.ts
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
/**
* A CLI script that runs PHP CLI via the WebAssembly build.
*/
import { writeFileSync, existsSync } from 'fs';
import { rootCertificates } from 'tls';
import {
LatestSupportedPHPVersion,
SupportedPHPVersion,
SupportedPHPVersionsList,
} from '@php-wasm/universal';
import { NodePHP } from '@php-wasm/node';
let args = process.argv.slice(2);
if (!args.length) {
args = ['--help'];
}
// Write the ca-bundle.crt file to disk so that PHP can find it.
const caBundlePath = new URL('ca-bundle.crt', (import.meta || {}).url).pathname;
if (!existsSync(caBundlePath)) {
writeFileSync(caBundlePath, rootCertificates.join('\n'));
}
args.unshift('-d', `openssl.cafile=${caBundlePath}`);
// @ts-ignore
const defaultPhpIniPath = await import('./php.ini');
const phpVersion = (process.env['PHP'] ||
LatestSupportedPHPVersion) as SupportedPHPVersion;
if (!SupportedPHPVersionsList.includes(phpVersion)) {
throw new Error(`Unsupported PHP version ${phpVersion}`);
}
const php = await NodePHP.load(phpVersion, {
emscriptenOptions: {
ENV: {
...process.env,
TERM: 'xterm',
},
},
});
php.useHostFilesystem();
const hasMinusCOption = args.some((arg) => arg.startsWith('-c'));
if (!hasMinusCOption) {
args.unshift('-c', defaultPhpIniPath);
}
php.cli(['php', ...args]).catch((result) => {
if (result.name === 'ExitStatus') {
process.exit(result.status === undefined ? 1 : result.status);
}
throw result;
});