diff --git a/package.json b/package.json index 471782b6..e5f7e1e0 100644 --- a/package.json +++ b/package.json @@ -17,9 +17,10 @@ "lint-staged": "npx lint-staged", "clean": "npx rimraf dist && rimraf out-tsc", "tools:download-schemas": "npx ts-node --project ./tools/tsconfig.json ./tools/download-schemas.ts", + "tools:generate-merged_json": "npx ts-node --project ./tools/tsconfig.json ./tools/generate-merged_json.ts", "tools:generate-definitions": "npx ts-node --project ./tools/tsconfig.json ./tools/generate-definitions.ts", "tools:generate-builders": "npx ts-node --project ./tools/tsconfig.json ./tools/generate-builders.ts", - "update-code-base": "npm run tools:download-schemas && npm run tools:generate-definitions && npm run tools:generate-builders && npm run format && npm run test", + "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", "format": "npx prettier --write \"**/*.ts\"", "lint": "npx eslint . --ext .ts && npx prettier --check \"**/*.ts\"", "pretest": "npx rimraf out-tsc", diff --git a/tools/download-schemas.ts b/tools/download-schemas.ts index 15a3fad3..cf3f430e 100644 --- a/tools/download-schemas.ts +++ b/tools/download-schemas.ts @@ -14,18 +14,15 @@ * limitations under the License. */ import $RefParser from '@apidevtools/json-schema-ref-parser'; -import { promises as fsPromises } from 'fs'; +import {promises as fsPromises} from 'fs'; import * as path from 'path'; -import { URL } from 'url'; -import yargs from 'yargs'; -import { schemaVersion } from '../package.json'; -import { mergeDefinitions, mergeSchemas, reset } from './utils'; +import {URL} from 'url'; +import {schemaDir, reset, schemaUrl} from './utils'; const { writeFile, mkdir } = fsPromises; /** * Downloads the given schema (and referenced sub-schema) and save them to disk - * A merged schema is also saved as `__merged.json` * @param schemaUrl {string} The URL to download the schema from * @param destDir {string} The destination path to save the schema to * @returns {void} @@ -49,21 +46,11 @@ const download = async (schemaUrl: URL, destDir: string): Promise => { await writeFile(path.resolve(destDir, externalSchemaFileName), JSON.stringify(externalSchema, null, 2)); } }); - const known$Refs = new Map(); - await mergeDefinitions($refParser, externalSchemas, known$Refs); - mergeSchemas($refParser, known$Refs, $refParser.schema, '#/'); - await writeFile(path.resolve(destDir, '__merged.json'), JSON.stringify($refParser.schema, null, 2)); return Promise.resolve(); } catch (ex) { return Promise.reject(ex); } }; -const argv = yargs(process.argv.slice(2)).argv; -const schemaUrl: URL = new URL( - (argv.url as string) || - `https://raw.githubusercontent.com/serverlessworkflow/specification/${schemaVersion}.x/schema/workflow.json` -); -const destDir = path.resolve(process.cwd(), 'src/lib/schema'); -download(schemaUrl, destDir).then(console.log.bind(console)).catch(console.error.bind(console)); +download(schemaUrl, schemaDir).then(console.log.bind(console)).catch(console.error.bind(console)); diff --git a/tools/generate-merged_json.ts b/tools/generate-merged_json.ts new file mode 100644 index 00000000..9f494014 --- /dev/null +++ b/tools/generate-merged_json.ts @@ -0,0 +1,48 @@ +/* + * Copyright 2021-Present The Serverless Workflow Specification Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import $RefParser from '@apidevtools/json-schema-ref-parser'; +import { promises as fsPromises } from 'fs'; +import * as path from 'path'; +import { URL } from 'url'; +import {schemaDir, mergeDefinitions, mergeSchemas, schemaUrl} from './utils'; + +const { writeFile} = fsPromises; + +/** + * Generate a merged schema `__merged.json` from schemas in schemas directory + * @param schemaUrl {string} The URL to download the schema from //TODO this does not like right, revisit mergeDefinitions + * @param destDir {string} The destination path to save the schema to + * @returns {void} + */ +const execute = async (schemaUrl: URL, destDir: string): Promise => { + try { + const $refParser = new $RefParser(); + await $refParser.resolve(schemaUrl.href); + const externalSchemas = $refParser.$refs + .paths() + .filter((p, index, arr) => arr.indexOf(p) === index && p !== schemaUrl.href); + + const known$Refs = new Map(); + await mergeDefinitions($refParser, externalSchemas, known$Refs); + mergeSchemas($refParser, known$Refs, $refParser.schema, '#/'); + await writeFile(path.resolve(destDir, '__merged.json'), JSON.stringify($refParser.schema, null, 2)); + return Promise.resolve(); + } catch (ex) { + return Promise.reject(ex); + } +}; + +execute(schemaUrl, schemaDir).then(console.log.bind(console)).catch(console.error.bind(console)); diff --git a/tools/utils.ts b/tools/utils.ts index af6abdf0..d287e5ad 100644 --- a/tools/utils.ts +++ b/tools/utils.ts @@ -19,6 +19,9 @@ import { promises as fsPromises } from 'fs'; import * as path from 'path'; import rimraf from 'rimraf'; import { readMeDisclaimer } from './consts'; +import {URL} from "url"; +import yargs from "yargs"; +import {schemaVersion} from "../package.json"; const { writeFile, mkdir } = fsPromises; @@ -175,3 +178,14 @@ export const reset = async (destDir: string) => rimrafP(destDir) .then(() => mkdir(destDir, { recursive: true })) .then(() => writeFile(path.resolve(destDir, 'README.md'), readMeDisclaimer)); + + +/** Schemas directory */ +export const schemaDir = path.resolve(process.cwd(), 'src/lib/schema'); + + +/** The URL to download the schema from */ +export const schemaUrl: URL = new URL( + (yargs(process.argv.slice(2)).argv.url as string) || + `https://raw.githubusercontent.com/serverlessworkflow/specification/${schemaVersion}.x/schema/workflow.json` +);