-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathoverride-php-exit-status.js
36 lines (30 loc) · 1.04 KB
/
override-php-exit-status.js
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
import * as path from 'path';
import * as fs from 'fs';
import { traverse } from 'estree-toolkit';
import { parseModule } from 'meriyah';
const thisScriptDir = path.dirname(process.argv[1]);
const phpJsPath = process.argv[2];
const phpJs = fs.readFileSync(phpJsPath, 'utf8');
const enhancedExitStatusJsPath = path.resolve(
thisScriptDir,
'esm-php-exit-status.js'
);
const enhancedExitStatusJs = fs.readFileSync(enhancedExitStatusJsPath, 'utf8');
const phpJsAst = parseModule(phpJs, { ranges: true });
console.log(phpJsAst);
traverse(phpJsAst, {
ClassDeclaration(path) {
console.log(path.node);
if (path.node?.id?.name === 'ExitStatus') {
const endOfClassDeclaration = path.node.range[1];
const head = phpJs.substring(0, endOfClassDeclaration);
const tail = phpJs.substring(endOfClassDeclaration);
const output = `${head}\n${enhancedExitStatusJs}\n${tail}`;
console.log(output);
fs.writeFileSync(phpJsPath, output);
process.exit(0);
}
}
});
console.error('Did not find ExitStatus declaration to override.');
process.exit(-1);