-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
copy-ast-spec.ts
62 lines (51 loc) · 1.66 KB
/
copy-ast-spec.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
56
57
58
59
60
61
62
import chlidProcess from 'child_process';
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const execAsync = promisify(chlidProcess.exec);
const AST_SPEC_PATH = path.resolve(__dirname, '../../ast-spec');
const OUTPUT_PATH = path.join(path.resolve(__dirname, '../src/generated'));
if (!fs.existsSync(OUTPUT_PATH)) {
fs.mkdirSync(OUTPUT_PATH);
}
const HEADER = `\
/**********************************************
* DO NOT MODIFY THIS FILE MANUALLY *
* *
* THIS FILE HAS BEEN COPIED FROM ast-spec. *
* ANY CHANGES WILL BE LOST ON THE NEXT BUILD *
* *
* MAKE CHANGES TO ast-spec AND THEN RUN *
* yarn build *
**********************************************/
`;
async function copyFile(
folderName: string,
fileName: string,
transformer: (code: string) => string = (s): string => s,
): Promise<void> {
const code = await readFile(
path.join(AST_SPEC_PATH, folderName, fileName),
'utf-8',
);
const transformedCode = transformer(code);
const outpath = path.join(OUTPUT_PATH, fileName);
await writeFile(outpath, HEADER + transformedCode, {
encoding: 'utf-8',
});
await execAsync(`yarn prettier --write ${outpath}`);
console.log('Copied', fileName);
}
async function main(): Promise<void> {
await Promise.all([
copyFile('dist', 'ast-spec.ts', code =>
code.replace(/export declare enum/g, 'export enum'),
),
]);
}
main().catch(error => {
console.error(error);
process.exitCode = 1;
});