-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathbuild-schema.mts
73 lines (60 loc) · 1.98 KB
/
build-schema.mts
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { spawn } from 'node:child_process';
import { rm } from 'node:fs/promises';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const baseDir = resolve(`${__dirname}/..`);
const bazelCmd = process.env.BAZEL ?? `yarn bazel`;
const distRoot = join(baseDir, '/dist-schema/');
function _clean() {
console.info('Cleaning...');
console.info(' Removing dist-schema/...');
return rm(join(__dirname, '../dist-schema'), { force: true, recursive: true, maxRetries: 3 });
}
function _exec(cmd: string, captureStdout: boolean): Promise<string> {
return new Promise((resolve, reject) => {
const proc = spawn(cmd, {
stdio: 'pipe',
shell: true,
env: {
HOME: process.env.HOME,
PATH: process.env.PATH,
},
});
let output = '';
proc.stdout.on('data', (data) => {
console.info(data.toString().trim());
if (captureStdout) {
output += data.toString();
}
});
proc.stderr.on('data', (data) => console.info(data.toString().trim()));
proc.on('error', (error) => {
console.error(error.message);
});
proc.on('exit', (status) => {
if (status !== 0) {
reject(`Command failed: ${cmd}`);
} else {
resolve(output);
}
});
});
}
async function _buildSchemas(): Promise<void> {
console.info(`Building schemas...`);
const queryTargetsCmd = `${bazelCmd} query --output=label "attr(name, .*_schema, //packages/...)"`;
const targets = (await _exec(queryTargetsCmd, true)).split(/\r?\n/);
await _exec(`${bazelCmd} build ${targets.join(' ')} --symlink_prefix=${distRoot}`, false);
}
export default async function (argv: {}): Promise<void> {
await _clean();
await _buildSchemas();
}