|
| 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 | + |
| 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 | +const fs = require('fs'); |
| 23 | +const path = require('path'); |
| 24 | +const qtCore = require('quicktype-core'); |
| 25 | +const tempRoot = process.env['BAZEL_TMPDIR'] || require('os').tmpdir(); |
| 26 | + |
| 27 | +// Header to add to all files. |
| 28 | +const header = ` |
| 29 | +/** |
| 30 | + * @license |
| 31 | + * Copyright Google Inc. All Rights Reserved. |
| 32 | + * |
| 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 |
| 35 | + */ |
| 36 | +
|
| 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. |
| 39 | +
|
| 40 | +`.replace(/^\n/m, ''); // Remove the first \n, it's in the constant because formatting is 👍. |
| 41 | + |
| 42 | +// Footer to add to all files. |
| 43 | +const footer = ``; |
| 44 | + |
| 45 | +/** |
| 46 | + * The simplest Node JSONSchemaStore implementation we can build which supports our custom protocol. |
| 47 | + * Supports reading from ng-cli addresses, valid URLs and files (absolute). |
| 48 | + */ |
| 49 | +class FetchingJSONSchemaStore extends qtCore.JSONSchemaStore { |
| 50 | + constructor(inPath) { |
| 51 | + super(); |
| 52 | + this._inPath = inPath; |
| 53 | + } |
| 54 | + |
| 55 | + async fetch(address) { |
| 56 | + const URL = require("url"); |
| 57 | + const url = URL.parse(address); |
| 58 | + let content = null; |
| 59 | + if (url.protocol === 'ng-cli:') { |
| 60 | + let filePath = path.join(__dirname, '../packages/angular/cli', url.hostname, url.path); |
| 61 | + content = fs.readFileSync(filePath, 'utf-8').trim(); |
| 62 | + } else if (url.hostname) { |
| 63 | + try { |
| 64 | + const fetch = require("node-fetch"); |
| 65 | + const response = await fetch(address); |
| 66 | + content = response.text(); |
| 67 | + } catch (e) { |
| 68 | + content = null; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (content === null && !path.isAbsolute(address)) { |
| 73 | + const resolvedPath = path.join(path.dirname(this._inPath), address); |
| 74 | + |
| 75 | + // Check relative to inPath |
| 76 | + if (fs.existsSync(resolvedPath)) { |
| 77 | + content = fs.readFileSync(resolvedPath, 'utf-8'); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (content === null && fs.existsSync(address)) { |
| 82 | + content = fs.readFileSync(address, 'utf-8').trim(); |
| 83 | + } |
| 84 | + |
| 85 | + if (content == null) { |
| 86 | + throw new Error(`Address ${JSON.stringify(address)} cannot be resolved.`); |
| 87 | + } |
| 88 | + |
| 89 | + return qtCore.parseJSON(content, "JSON Schema", address); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +/** |
| 95 | + * Create the TS file from the schema, and overwrite the outPath (or log). |
| 96 | + * @param {string} inPath |
| 97 | + * @param {string} outPath |
| 98 | + */ |
| 99 | +async function main(inPath, outPath) { |
| 100 | + const content = await generate(inPath); |
| 101 | + |
| 102 | + if (outPath === '-') { |
| 103 | + console.log(content); |
| 104 | + process.exit(0); |
| 105 | + } |
| 106 | + |
| 107 | + const buildWorkspaceDirectory = process.env['BUILD_WORKSPACE_DIRECTORY'] || '.'; |
| 108 | + outPath = path.resolve(buildWorkspaceDirectory, outPath); |
| 109 | + fs.writeFileSync(outPath, content, 'utf-8'); |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +async function generate(inPath) { |
| 114 | + // Best description of how to use the API was found at |
| 115 | + // https://blog.quicktype.io/customizing-quicktype/ |
| 116 | + const inputData = new qtCore.InputData(); |
| 117 | + const source = { name: 'Schema', schema: fs.readFileSync(inPath, 'utf-8') }; |
| 118 | + |
| 119 | + await inputData.addSource('schema', source, () => { |
| 120 | + return new qtCore.JSONSchemaInput(new FetchingJSONSchemaStore(inPath)); |
| 121 | + }); |
| 122 | + |
| 123 | + const lang = new qtCore.TypeScriptTargetLanguage(); |
| 124 | + |
| 125 | + const { lines } = await qtCore.quicktype({ |
| 126 | + lang, |
| 127 | + inputData, |
| 128 | + alphabetizeProperties: true, |
| 129 | + src: [inPath], |
| 130 | + rendererOptions: { |
| 131 | + 'just-types': true, |
| 132 | + 'explicit-unions': true, |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + return header + lines.join('\n') + footer; |
| 137 | +} |
| 138 | + |
| 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); |
| 144 | +} |
| 145 | + |
| 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 | + }); |
0 commit comments