55 * Use of this source code is governed by an MIT-style license that can be
66 * found in the LICENSE file at https://angular.io/license
77 */
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.
228const fs = require ( 'fs' ) ;
239const 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' ) ;
2619
27- // Header to add to all files.
28- const header = `
2920/**
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.
3225 *
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.
3532 */
3633
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
3941
40- ` . replace ( / ^ \n / m , '' ) ; // Remove the first \n, it's in the constant because formatting is 👍.
42+ ` ;
4143
4244// Footer to add to all files.
4345const footer = `` ;
@@ -46,7 +48,7 @@ const footer = ``;
4648 * The simplest Node JSONSchemaStore implementation we can build which supports our custom protocol.
4749 * Supports reading from ng-cli addresses, valid URLs and files (absolute).
4850 */
49- class FetchingJSONSchemaStore extends qtCore . JSONSchemaStore {
51+ class FetchingJSONSchemaStore extends JSONSchemaStore {
5052 constructor ( inPath ) {
5153 super ( ) ;
5254 this . _inPath = inPath ;
@@ -83,10 +85,10 @@ class FetchingJSONSchemaStore extends qtCore.JSONSchemaStore {
8385 }
8486
8587 if ( content == null ) {
86- throw new Error ( `Address ${ JSON . stringify ( address ) } cannot be resolved.` ) ;
88+ return undefined ;
8789 }
8890
89- return qtCore . parseJSON ( content , "JSON Schema" , address ) ;
91+ return parseJSON ( content , "JSON Schema" , address ) ;
9092 }
9193}
9294
@@ -113,40 +115,43 @@ async function main(inPath, outPath) {
113115async function generate ( inPath ) {
114116 // Best description of how to use the API was found at
115117 // https://blog.quicktype.io/customizing-quicktype/
116- const inputData = new qtCore . InputData ( ) ;
118+ const inputData = new InputData ( ) ;
117119 const source = { name : 'Schema' , schema : fs . readFileSync ( inPath , 'utf-8' ) } ;
118120
119121 await inputData . addSource ( 'schema' , source , ( ) => {
120- return new qtCore . JSONSchemaInput ( new FetchingJSONSchemaStore ( inPath ) ) ;
122+ return new JSONSchemaInput ( new FetchingJSONSchemaStore ( inPath ) ) ;
121123 } ) ;
122124
123- const lang = new qtCore . TypeScriptTargetLanguage ( ) ;
125+ const lang = new TypeScriptTargetLanguage ( ) ;
124126
125- const { lines } = await qtCore . quicktype ( {
127+ const { lines } = await quicktype ( {
126128 lang,
127129 inputData,
128130 alphabetizeProperties : true ,
129- src : [ inPath ] ,
130131 rendererOptions : {
131- 'just-types' : true ,
132- 'explicit-unions' : true ,
133- }
132+ 'just-types' : ' true' ,
133+ 'explicit-unions' : ' true' ,
134+ } ,
134135 } ) ;
135136
136137 return header + lines . join ( '\n' ) + footer ;
137138}
138139
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+ } ) ;
144155}
145156
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