MCPcopy
hub / github.com/angular/angular-cli / generate

Function generate

tools/ng_cli_schema_generator.js:15–89  ·  view source on GitHub ↗

* Generator the Angular CLI workspace schema file.

(inPath, outPath)

Source from the content-addressed store, hash-verified

13 * Generator the Angular CLI workspace schema file.
14 */
15function generate(inPath, outPath) {
16 // While on paper we could use quicktype for this.
17 // Quicktype doesn't handle `patternProperties` and `oneOf` that well.
18
19 const jsonSchema = readFileSync(inPath, 'utf8');
20 const nestedDefinitions = {};
21 const schemaParsed = JSON.parse(jsonSchema, (key, value) => {
22 if (key === '$ref' && typeof value === 'string' && !value.startsWith('#')) {
23 // Resolve $ref and camelize key
24 const definitionKey = value
25 .replace(/(\.json|src)/g, '')
26 .split(/\\|\/|_|-|\./)
27 .filter((p) => !!p)
28 .map((s) => s.charAt(0).toUpperCase() + s.slice(1))
29 .join('');
30
31 const nestedSchemaPath = resolve(dirname(inPath), value);
32 const nestedSchema = readFileSync(nestedSchemaPath, 'utf8');
33 const nestedSchemaJson = JSON.parse(nestedSchema, (key, value) => {
34 switch (key) {
35 case '$ref':
36 if (value.startsWith('#/definitions/')) {
37 return value.replace('#/definitions/', `#/definitions/${definitionKey}/definitions/`);
38 } else {
39 throw new Error(`Error while resolving $ref ${value} in ${nestedSchemaPath}.`);
40 }
41 case '$id':
42 case 'id':
43 case '$schema':
44 case 'required':
45 case 'x-prompt':
46 case 'x-user-analytics':
47 return undefined;
48 default:
49 return value;
50 }
51 });
52
53 nestedDefinitions[definitionKey] = nestedSchemaJson;
54
55 return `#/definitions/${definitionKey}`;
56 }
57
58 return key === ''
59 ? {
60 ...value,
61 definitions: {
62 ...value.definitions,
63 ...nestedDefinitions,
64 },
65 }
66 : value;
67 });
68
69 const buildWorkspaceDirectory = process.env['BUILD_WORKSPACE_DIRECTORY'] || '.';
70 outPath = resolve(buildWorkspaceDirectory, outPath);
71
72 mkdirSync(dirname(outPath), { recursive: true });

Callers 1

Calls 2

resolveFunction · 0.50
dirnameFunction · 0.50

Tested by

no test coverage detected