Skip to content

Commit b3857b4

Browse files
hanslmgechev
authored andcommitted
build: add build-schema script to build schemas
1 parent ac77a5e commit b3857b4

File tree

2 files changed

+63
-33
lines changed

2 files changed

+63
-33
lines changed

scripts/build-schema.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
// tslint:disable:no-implicit-dependencies
9+
import { logging } from '@angular-devkit/core';
10+
import * as fs from 'fs';
11+
import * as glob from 'glob';
12+
import * as path from 'path';
13+
14+
15+
function _mkdirp(p: string) {
16+
// Create parent folder if necessary.
17+
if (!fs.existsSync(path.dirname(p))) {
18+
_mkdirp(path.dirname(p));
19+
}
20+
if (!fs.existsSync(p)) {
21+
fs.mkdirSync(p);
22+
}
23+
}
24+
25+
26+
export default async function(
27+
argv: { },
28+
logger: logging.Logger,
29+
) {
30+
const allJsonFiles = glob.sync('packages/**/*.json', {
31+
ignore: [
32+
'**/node_modules/**',
33+
'**/files/**',
34+
'**/*-files/**',
35+
'**/package.json',
36+
],
37+
});
38+
39+
const quicktypeRunner = require('../tools/quicktype_runner');
40+
logger.info('Generating JSON Schema....');
41+
42+
for (const fileName of allJsonFiles) {
43+
if (fs.existsSync(fileName.replace(/\.json$/, '.ts'))
44+
|| fs.existsSync(fileName.replace(/\.json$/, '.d.ts'))) {
45+
// Skip files that already exist.
46+
continue;
47+
}
48+
const content = fs.readFileSync(fileName, 'utf-8');
49+
50+
const json = JSON.parse(content);
51+
if (!json.$schema) {
52+
// Skip non-schema files.
53+
continue;
54+
}
55+
const tsContent = await quicktypeRunner.generate(fileName);
56+
const tsPath = path.join(__dirname, '../dist-schema', fileName.replace(/\.json$/, '.ts'));
57+
58+
_mkdirp(path.dirname(tsPath));
59+
fs.writeFileSync(tsPath, tsContent, 'utf-8');
60+
}
61+
}

scripts/build.ts

+2-33
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as fs from 'fs';
1212
import * as glob from 'glob';
1313
import * as path from 'path';
1414
import { packages } from '../lib/packages';
15+
import buildSchema from './build-schema';
1516

1617
const minimatch = require('minimatch');
1718
const tar = require('tar');
@@ -200,41 +201,8 @@ async function _bazel(logger: logging.Logger) {
200201
// TODO: undo this when we fully support bazel on windows.
201202
// logger.info('Bazel build...');
202203
// _exec('bazel', ['build', '//packages/...'], {}, logger);
203-
204-
const allJsonFiles = glob.sync('packages/**/*.json', {
205-
ignore: [
206-
'**/node_modules/**',
207-
'**/files/**',
208-
'**/*-files/**',
209-
'**/package.json',
210-
],
211-
});
212-
213-
const quicktypeRunner = require('../tools/quicktype_runner');
214-
logger.info('Generating JSON Schema....');
215-
216-
for (const fileName of allJsonFiles) {
217-
if (fs.existsSync(fileName.replace(/\.json$/, '.ts'))
218-
|| fs.existsSync(fileName.replace(/\.json$/, '.d.ts'))) {
219-
// Skip files that already exist.
220-
continue;
221-
}
222-
const content = fs.readFileSync(fileName, 'utf-8');
223-
224-
const json = JSON.parse(content);
225-
if (!json.$schema) {
226-
// Skip non-schema files.
227-
continue;
228-
}
229-
const tsContent = await quicktypeRunner.generate(fileName);
230-
const tsPath = path.join(__dirname, '../dist-schema', fileName.replace(/\.json$/, '.ts'));
231-
232-
_mkdirp(path.dirname(tsPath));
233-
fs.writeFileSync(tsPath, tsContent, 'utf-8');
234-
}
235204
}
236205

237-
238206
export default async function(
239207
argv: { local?: boolean, snapshot?: boolean },
240208
logger: logging.Logger,
@@ -243,6 +211,7 @@ export default async function(
243211

244212
const sortedPackages = _sortPackages();
245213
await _bazel(logger);
214+
await buildSchema({}, logger);
246215
_build(logger);
247216

248217
logger.info('Moving packages to dist/');

0 commit comments

Comments
 (0)