5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
-
9
- /**
10
- * This file is pure JavaScript because we want to avoid any dependency or build step
11
- * to it. It's just simple (and zen-ier).
12
- *
13
- * This file wraps around quicktype and can do one of two things;
14
- *
15
- * `node quicktype_runner.js <in_path> <out_path>`
16
- * Reads the in path and outputs the TS file at the out_path.
17
- *
18
- * Using `-` as the out_path will output on STDOUT instead of a file.
19
- */
20
-
21
- // Imports.
22
8
const fs = require ( 'fs' ) ;
23
9
const path = require ( 'path' ) ;
24
- const qtCore = require ( 'quicktype-core' ) ;
25
- const tempRoot = process . env [ 'BAZEL_TMPDIR' ] || require ( 'os' ) . tmpdir ( ) ;
10
+ const {
11
+ InputData,
12
+ JSONSchema,
13
+ JSONSchemaInput,
14
+ JSONSchemaStore,
15
+ TypeScriptTargetLanguage,
16
+ parseJSON,
17
+ quicktype,
18
+ } = require ( 'quicktype-core' ) ;
26
19
27
- // Header to add to all files.
28
- const header = `
29
20
/**
30
- * @license
31
- * Copyright Google Inc. All Rights Reserved.
21
+ * This file is pure JavaScript because Bazel only support compiling to ES5, while quicktype is
22
+ * ES2015. This results in an incompatible call to `super()` in the FetchingJSONSchemaStore
23
+ * class as it tries to call JSONSchemaStore's constructor in ES5.
24
+ * TODO: move this file to typescript when Bazel supports ES2015 output.
32
25
*
33
- * Use of this source code is governed by an MIT-style license that can be
34
- * found in the LICENSE file at https://angular.io/license
26
+ * This file wraps around quicktype and can do one of two things;
27
+ *
28
+ * `node quicktype_runner.js <in_path> <out_path>`
29
+ * Reads the in path and outputs the TS file at the out_path.
30
+ *
31
+ * Using `-` as the out_path will output on STDOUT instead of a file.
35
32
*/
36
33
37
- // THIS FILE IS AUTOMATICALLY GENERATED IN BAZEL. TO UPDATE THIS FILE YOU NEED TO CHANGE THE
38
- // CORRESPONDING JSON SCHEMA FILE, THEN RUN BAZEL.
34
+ // Header to add to all files.
35
+ const header = `
36
+ // THIS FILE IS AUTOMATICALLY GENERATED. TO UPDATE THIS FILE YOU NEED TO CHANGE THE
37
+ // CORRESPONDING JSON SCHEMA FILE, THEN RUN devkit-admin build (or bazel build ...).
38
+
39
+ // tslint:disable:no-global-tslint-disable
40
+ // tslint:disable
39
41
40
- ` . replace ( / ^ \n / m , '' ) ; // Remove the first \n, it's in the constant because formatting is 👍.
42
+ ` ;
41
43
42
44
// Footer to add to all files.
43
45
const footer = `` ;
@@ -46,7 +48,7 @@ const footer = ``;
46
48
* The simplest Node JSONSchemaStore implementation we can build which supports our custom protocol.
47
49
* Supports reading from ng-cli addresses, valid URLs and files (absolute).
48
50
*/
49
- class FetchingJSONSchemaStore extends qtCore . JSONSchemaStore {
51
+ class FetchingJSONSchemaStore extends JSONSchemaStore {
50
52
constructor ( inPath ) {
51
53
super ( ) ;
52
54
this . _inPath = inPath ;
@@ -83,10 +85,10 @@ class FetchingJSONSchemaStore extends qtCore.JSONSchemaStore {
83
85
}
84
86
85
87
if ( content == null ) {
86
- throw new Error ( `Address ${ JSON . stringify ( address ) } cannot be resolved.` ) ;
88
+ return undefined ;
87
89
}
88
90
89
- return qtCore . parseJSON ( content , "JSON Schema" , address ) ;
91
+ return parseJSON ( content , "JSON Schema" , address ) ;
90
92
}
91
93
}
92
94
@@ -113,40 +115,43 @@ async function main(inPath, outPath) {
113
115
async function generate ( inPath ) {
114
116
// Best description of how to use the API was found at
115
117
// https://blog.quicktype.io/customizing-quicktype/
116
- const inputData = new qtCore . InputData ( ) ;
118
+ const inputData = new InputData ( ) ;
117
119
const source = { name : 'Schema' , schema : fs . readFileSync ( inPath , 'utf-8' ) } ;
118
120
119
121
await inputData . addSource ( 'schema' , source , ( ) => {
120
- return new qtCore . JSONSchemaInput ( new FetchingJSONSchemaStore ( inPath ) ) ;
122
+ return new JSONSchemaInput ( new FetchingJSONSchemaStore ( inPath ) ) ;
121
123
} ) ;
122
124
123
- const lang = new qtCore . TypeScriptTargetLanguage ( ) ;
125
+ const lang = new TypeScriptTargetLanguage ( ) ;
124
126
125
- const { lines } = await qtCore . quicktype ( {
127
+ const { lines } = await quicktype ( {
126
128
lang,
127
129
inputData,
128
130
alphabetizeProperties : true ,
129
- src : [ inPath ] ,
130
131
rendererOptions : {
131
- 'just-types' : true ,
132
- 'explicit-unions' : true ,
133
- }
132
+ 'just-types' : ' true' ,
133
+ 'explicit-unions' : ' true' ,
134
+ } ,
134
135
} ) ;
135
136
136
137
return header + lines . join ( '\n' ) + footer ;
137
138
}
138
139
139
- // Parse arguments and run main().
140
- const argv = process . argv . slice ( 2 ) ;
141
- if ( argv . length < 2 || argv . length > 3 ) {
142
- console . error ( 'Must include 2 or 3 arguments.' ) ;
143
- process . exit ( 1 ) ;
140
+ if ( require . main === module ) {
141
+ // Parse arguments and run main().
142
+ const argv = process . argv . slice ( 2 ) ;
143
+ if ( argv . length < 2 || argv . length > 3 ) {
144
+ console . error ( 'Must include 2 or 3 arguments.' ) ;
145
+ process . exit ( 1 ) ;
146
+ }
147
+
148
+ main ( argv [ 0 ] , argv [ 1 ] )
149
+ . then ( ( ) => process . exit ( 0 ) )
150
+ . catch ( err => {
151
+ console . error ( 'An error happened:' ) ;
152
+ console . error ( err ) ;
153
+ process . exit ( 127 ) ;
154
+ } ) ;
144
155
}
145
156
146
- main ( ...argv )
147
- . then ( ( ) => process . exit ( 0 ) )
148
- . catch ( err => {
149
- console . error ( 'An error happened:' ) ;
150
- console . error ( err ) ;
151
- process . exit ( 127 ) ;
152
- } ) ;
157
+ exports . generate = generate ;
0 commit comments