|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | import { logging } from '@angular-devkit/core';
|
10 |
| -import * as fs from 'fs'; |
11 |
| -import glob from 'glob'; |
12 |
| -import * as path from 'path'; |
| 10 | +import { spawn } from 'child_process'; |
| 11 | +import fs from 'fs'; |
| 12 | +import { join, resolve } from 'path'; |
13 | 13 |
|
14 |
| -export default async function (argv: {}, logger: logging.Logger) { |
15 |
| - const allJsonFiles = glob.sync('packages/**/*.json', { |
16 |
| - ignore: ['**/node_modules/**', '**/files/**', '**/*-files/**', '**/package.json'], |
17 |
| - }); |
18 |
| - const dist = path.join(__dirname, '../dist-schema'); |
19 |
| - |
20 |
| - const quicktypeRunner = require('../tools/quicktype_runner'); |
21 |
| - logger.info('Removing dist-schema/...'); |
22 |
| - fs.rmSync(dist, { force: true, recursive: true, maxRetries: 3 }); |
23 |
| - |
24 |
| - logger.info('Generating JSON Schema....'); |
25 |
| - |
26 |
| - for (const fileName of allJsonFiles) { |
27 |
| - if ( |
28 |
| - fs.existsSync(fileName.replace(/\.json$/, '.ts')) || |
29 |
| - fs.existsSync(fileName.replace(/\.json$/, '.d.ts')) |
30 |
| - ) { |
31 |
| - // Skip files that already exist. |
32 |
| - continue; |
33 |
| - } |
34 |
| - const content = fs.readFileSync(fileName, 'utf-8'); |
35 |
| - |
36 |
| - let json; |
37 |
| - try { |
38 |
| - json = JSON.parse(content); |
39 |
| - if (typeof json.$schema !== 'string' || !json.$schema.startsWith('http://json-schema.org/')) { |
40 |
| - // Skip non-schema files. |
41 |
| - continue; |
| 14 | +const baseDir = resolve(`${__dirname}/..`); |
| 15 | +const bazelCmd = process.env.BAZEL ?? `yarn --silent bazel`; |
| 16 | +const distRoot = join(baseDir, '/dist-schema/'); |
| 17 | + |
| 18 | +function rimraf(location: string) { |
| 19 | + fs.rmSync(location, { force: true, recursive: true, maxRetries: 3 }); |
| 20 | +} |
| 21 | + |
| 22 | +function _clean(logger: logging.Logger) { |
| 23 | + logger.info('Cleaning...'); |
| 24 | + logger.info(' Removing dist-schema/...'); |
| 25 | + rimraf(join(__dirname, '../dist-schema')); |
| 26 | +} |
| 27 | + |
| 28 | +function _exec(cmd: string, captureStdout: boolean, logger: logging.Logger): Promise<string> { |
| 29 | + return new Promise((resolve, reject) => { |
| 30 | + const proc = spawn(cmd, { |
| 31 | + stdio: 'pipe', |
| 32 | + shell: true, |
| 33 | + env: { |
| 34 | + HOME: process.env.HOME, |
| 35 | + PATH: process.env.PATH, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + let output = ''; |
| 40 | + proc.stdout.on('data', (data) => { |
| 41 | + logger.info(data.toString().trim()); |
| 42 | + if (captureStdout) { |
| 43 | + output += data.toString().trim(); |
| 44 | + } |
| 45 | + }); |
| 46 | + proc.stderr.on('data', (data) => logger.info(data.toString().trim())); |
| 47 | + |
| 48 | + proc.on('error', (error) => { |
| 49 | + logger.error(error.message); |
| 50 | + }); |
| 51 | + |
| 52 | + proc.on('exit', (status) => { |
| 53 | + if (status !== 0) { |
| 54 | + reject(`Command failed: ${cmd}`); |
| 55 | + } else { |
| 56 | + resolve(output); |
42 | 57 | }
|
43 |
| - } catch { |
44 |
| - // malformed or JSON5 |
45 |
| - continue; |
46 |
| - } |
47 |
| - const tsContent = await quicktypeRunner.generate(fileName); |
48 |
| - const tsPath = path.join(dist, fileName.replace(/\.json$/, '.ts')); |
49 |
| - |
50 |
| - fs.mkdirSync(path.dirname(tsPath), { recursive: true }); |
51 |
| - fs.writeFileSync(tsPath, tsContent, 'utf-8'); |
52 |
| - } |
53 |
| - |
54 |
| - // Angular CLI config schema |
55 |
| - const cliJsonSchema = require('../tools/ng_cli_schema_generator'); |
56 |
| - const inputPath = 'packages/angular/cli/lib/config/workspace-schema.json'; |
57 |
| - const outputPath = path.join(dist, inputPath.replace('workspace-schema.json', 'schema.json')); |
58 |
| - cliJsonSchema.generate(inputPath, outputPath); |
| 58 | + }); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +async function _buildSchemas(logger: logging.Logger): Promise<void> { |
| 63 | + logger.info(`Building schemas...`); |
| 64 | + |
| 65 | + const queryLogger = logger.createChild('query'); |
| 66 | + const queryTargetsCmd = `${bazelCmd} query --output=label "attr(name, .*_schema, //packages/...)"`; |
| 67 | + const targets = (await _exec(queryTargetsCmd, true, queryLogger)).split(/\r?\n/); |
| 68 | + const buildLogger = logger.createChild('build'); |
| 69 | + |
| 70 | + await _exec( |
| 71 | + `${bazelCmd} build ${targets.join(' ')} --symlink_prefix=${distRoot}`, |
| 72 | + false, |
| 73 | + buildLogger, |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +export default async function ( |
| 78 | + argv: {}, |
| 79 | + logger: logging.Logger = new logging.Logger('build-schema-logger'), |
| 80 | +): Promise<void> { |
| 81 | + _clean(logger); |
| 82 | + |
| 83 | + await _buildSchemas(logger); |
59 | 84 | }
|
0 commit comments