Skip to content

Commit 0e40bef

Browse files
authored
refactor-generate-merged-json (#177)
1 parent 641a329 commit 0e40bef

File tree

4 files changed

+68
-18
lines changed

4 files changed

+68
-18
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
"lint-staged": "npx lint-staged",
1818
"clean": "npx rimraf dist && rimraf out-tsc",
1919
"tools:download-schemas": "npx ts-node --project ./tools/tsconfig.json ./tools/download-schemas.ts",
20+
"tools:generate-merged_json": "npx ts-node --project ./tools/tsconfig.json ./tools/generate-merged_json.ts",
2021
"tools:generate-definitions": "npx ts-node --project ./tools/tsconfig.json ./tools/generate-definitions.ts",
2122
"tools:generate-builders": "npx ts-node --project ./tools/tsconfig.json ./tools/generate-builders.ts",
22-
"update-code-base": "npm run tools:download-schemas && npm run tools:generate-definitions && npm run tools:generate-builders && npm run format && npm run test",
23+
"update-code-base": "npm run tools:download-schemas && tools:generate-merged_json && npm run tools:generate-definitions && npm run tools:generate-builders && npm run format && npm run test",
2324
"format": "npx prettier --write \"**/*.ts\"",
2425
"lint": "npx eslint . --ext .ts && npx prettier --check \"**/*.ts\"",
2526
"pretest": "npx rimraf out-tsc",

tools/download-schemas.ts

+4-17
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@
1414
* limitations under the License.
1515
*/
1616
import $RefParser from '@apidevtools/json-schema-ref-parser';
17-
import { promises as fsPromises } from 'fs';
17+
import {promises as fsPromises} from 'fs';
1818
import * as path from 'path';
19-
import { URL } from 'url';
20-
import yargs from 'yargs';
21-
import { schemaVersion } from '../package.json';
22-
import { mergeDefinitions, mergeSchemas, reset } from './utils';
19+
import {URL} from 'url';
20+
import {schemaDir, reset, schemaUrl} from './utils';
2321

2422
const { writeFile, mkdir } = fsPromises;
2523

2624
/**
2725
* Downloads the given schema (and referenced sub-schema) and save them to disk
28-
* A merged schema is also saved as `__merged.json`
2926
* @param schemaUrl {string} The URL to download the schema from
3027
* @param destDir {string} The destination path to save the schema to
3128
* @returns {void}
@@ -49,21 +46,11 @@ const download = async (schemaUrl: URL, destDir: string): Promise<void> => {
4946
await writeFile(path.resolve(destDir, externalSchemaFileName), JSON.stringify(externalSchema, null, 2));
5047
}
5148
});
52-
const known$Refs = new Map<string, string>();
53-
await mergeDefinitions($refParser, externalSchemas, known$Refs);
54-
mergeSchemas($refParser, known$Refs, $refParser.schema, '#/');
55-
await writeFile(path.resolve(destDir, '__merged.json'), JSON.stringify($refParser.schema, null, 2));
5649
return Promise.resolve();
5750
} catch (ex) {
5851
return Promise.reject(ex);
5952
}
6053
};
6154

62-
const argv = yargs(process.argv.slice(2)).argv;
63-
const schemaUrl: URL = new URL(
64-
(argv.url as string) ||
65-
`https://raw.githubusercontent.com/serverlessworkflow/specification/${schemaVersion}.x/schema/workflow.json`
66-
);
67-
const destDir = path.resolve(process.cwd(), 'src/lib/schema');
6855

69-
download(schemaUrl, destDir).then(console.log.bind(console)).catch(console.error.bind(console));
56+
download(schemaUrl, schemaDir).then(console.log.bind(console)).catch(console.error.bind(console));

tools/generate-merged_json.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2021-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import $RefParser from '@apidevtools/json-schema-ref-parser';
17+
import { promises as fsPromises } from 'fs';
18+
import * as path from 'path';
19+
import { URL } from 'url';
20+
import {schemaDir, mergeDefinitions, mergeSchemas, schemaUrl} from './utils';
21+
22+
const { writeFile} = fsPromises;
23+
24+
/**
25+
* Generate a merged schema `__merged.json` from schemas in schemas directory
26+
* @param schemaUrl {string} The URL to download the schema from //TODO this does not like right, revisit mergeDefinitions
27+
* @param destDir {string} The destination path to save the schema to
28+
* @returns {void}
29+
*/
30+
const execute = async (schemaUrl: URL, destDir: string): Promise<void> => {
31+
try {
32+
const $refParser = new $RefParser();
33+
await $refParser.resolve(schemaUrl.href);
34+
const externalSchemas = $refParser.$refs
35+
.paths()
36+
.filter((p, index, arr) => arr.indexOf(p) === index && p !== schemaUrl.href);
37+
38+
const known$Refs = new Map<string, string>();
39+
await mergeDefinitions($refParser, externalSchemas, known$Refs);
40+
mergeSchemas($refParser, known$Refs, $refParser.schema, '#/');
41+
await writeFile(path.resolve(destDir, '__merged.json'), JSON.stringify($refParser.schema, null, 2));
42+
return Promise.resolve();
43+
} catch (ex) {
44+
return Promise.reject(ex);
45+
}
46+
};
47+
48+
execute(schemaUrl, schemaDir).then(console.log.bind(console)).catch(console.error.bind(console));

tools/utils.ts

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import { promises as fsPromises } from 'fs';
1919
import * as path from 'path';
2020
import rimraf from 'rimraf';
2121
import { readMeDisclaimer } from './consts';
22+
import {URL} from "url";
23+
import yargs from "yargs";
24+
import {schemaVersion} from "../package.json";
2225

2326
const { writeFile, mkdir } = fsPromises;
2427

@@ -175,3 +178,14 @@ export const reset = async (destDir: string) =>
175178
rimrafP(destDir)
176179
.then(() => mkdir(destDir, { recursive: true }))
177180
.then(() => writeFile(path.resolve(destDir, 'README.md'), readMeDisclaimer));
181+
182+
183+
/** Schemas directory */
184+
export const schemaDir = path.resolve(process.cwd(), 'src/lib/schema');
185+
186+
187+
/** The URL to download the schema from */
188+
export const schemaUrl: URL = new URL(
189+
(yargs(process.argv.slice(2)).argv.url as string) ||
190+
`https://raw.githubusercontent.com/serverlessworkflow/specification/${schemaVersion}.x/schema/workflow.json`
191+
);

0 commit comments

Comments
 (0)