Skip to content

refactor-generate-merged-json #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 4 additions & 17 deletions tools/download-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -49,21 +46,11 @@ const download = async (schemaUrl: URL, destDir: string): Promise<void> => {
await writeFile(path.resolve(destDir, externalSchemaFileName), JSON.stringify(externalSchema, null, 2));
}
});
const known$Refs = new Map<string, string>();
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));
48 changes: 48 additions & 0 deletions tools/generate-merged_json.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
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<string, string>();
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));
14 changes: 14 additions & 0 deletions tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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`
);