diff --git a/dist/index.js b/dist/index.js index 2599b17..30f57a1 100755 --- a/dist/index.js +++ b/dist/index.js @@ -11,12 +11,16 @@ const json_schema_to_typescript_1 = require("json-schema-to-typescript"); const get_all_files_1 = __importDefault(require("./scripts/get-all-files")); const generate_json_schema_validators_js_1 = __importDefault(require("./scripts/generate-json-schema-validators.js")); const commandLineArgs = yargs_1.default + .string('source') .alias('s', 'source') .describe('s', 'the directory for your json schema files') + .string('interface-target') .alias('i', 'interface-target') .describe('i', 'the output location for TypeScript interfaces') + .string('validator-target') .alias('v', 'validator-target') .describe('v', 'the output location for json schema validators') + .string('patterns') .alias('p', 'patterns') .describe('p', 'the location of a regex patterns module (optional)') .demandOption(['source'], 'The source (s) parameter is required.') @@ -59,7 +63,7 @@ const updateSchemaFile = (filePath) => { if (!patterns[matchText]) { throw new Error(`updateSchemaFile:RegexPattern - pattern not found '${matchText}'`); } - // remove start and end / for JSON SCHEMA purposes and also escape characers to make regex expression valid in JSON + // remove start and end / for JSON SCHEMA purposes and also escape characters to make regex expression valid in JSON return patterns[matchText].toString() .replace(/^\//, '') .replace(/\/$/, '') @@ -88,8 +92,7 @@ get_all_files_1.default(TEMPORARY_SCHEMA_DIR).then((fileInfoList) => { return schemaFileList; // run typescript to json schema }).then((schemaFileList) => { - const allSchemaPromises = []; - schemaFileList.forEach((schemaFilePath) => { + const allSchemaPromises = schemaFileList.map((schemaFilePath) => { const schemaPromise = json_schema_to_typescript_1.compileFromFile(schemaFilePath, {}).then((ts) => { const relativeTypeScriptFilePath = path_1.default.relative(TEMPORARY_SCHEMA_DIR, schemaFilePath).replace(/.json$/, '.ts'); const targetTypeScriptFilePath = path_1.default.resolve(TARGET_TYPESCRIPT_INTERFACE_DIR, relativeTypeScriptFilePath); @@ -98,7 +101,7 @@ get_all_files_1.default(TEMPORARY_SCHEMA_DIR).then((fileInfoList) => { fs_extra_1.default.mkdirSync(targetTypeScriptFileFolderPath, { recursive: true }); return fs_extra_1.default.writeFileSync(targetTypeScriptFilePath, ts); }); - allSchemaPromises.push(schemaPromise); + return schemaPromise; }); return Promise.all(allSchemaPromises); }).then(() => { diff --git a/dist/index.js.map b/dist/index.js.map index 2e973b5..379a02d 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AACA,wDAA0B;AAC1B,gDAAwB;AACxB,kDAA0B;AAC1B,yEAA4D;AAC5D,4EAAkD;AAClD,sHAAwF;AAExF,MAAM,eAAe,GAAG,eAAK;KAC1B,KAAK,CAAC,GAAG,EAAE,QAAQ,CAAC;KACpB,QAAQ,CAAC,GAAG,EAAE,0CAA0C,CAAC;KACzD,KAAK,CAAC,GAAG,EAAE,kBAAkB,CAAC;KAC9B,QAAQ,CAAC,GAAG,EAAE,+CAA+C,CAAC;KAC9D,KAAK,CAAC,GAAG,EAAE,kBAAkB,CAAC;KAC9B,QAAQ,CAAC,GAAG,EAAE,gDAAgD,CAAC;KAC/D,KAAK,CAAC,GAAG,EAAE,UAAU,CAAC;KACtB,QAAQ,CAAC,GAAG,EAAE,oDAAoD,CAAC;KACnE,YAAY,CAAC,CAAC,QAAQ,CAAC,EAAE,uCAAuC,CAAC;KACjE,IAAI,CAAC,MAAM,CAAC;KACZ,IAAI,CAAA;AAEP,MAAM,yBAAyB,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;AAEtF,IAAI,eAAe,CAAC;AACpB,IAAI,eAAe,CAAC,CAAC,EAAE;IACrB,eAAe,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;CACvE;KAAM;IACL,eAAe,GAAG,cAAI,CAAC,IAAI,CAAC,yBAAyB,EAAE,IAAI,EAAE,wBAAwB,CAAC,CAAC;CACxF;AAED,IAAI,eAAe,CAAC;AACpB,IA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EH,OAAO,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AAExC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;IAEX,OAAO,4CAA4B,CAAC,oBAAoB,EAAE,qBAAqB,CAAC,CAAC;AAEnF,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;IAEX,oCAAoC;IACpC,kBAAE,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;AAE/B,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;IACX,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;AAC7C,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IACf,OAAO,CAAC,KAAK,CAAC,4BAA4B,EAAE,GAAG,CAAC,CAAC;AACnD,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/dist/scripts/generate-json-schema-validators.js b/dist/scripts/generate-json-schema-validators.js index 3ead8a5..32f3c87 100644 --- a/dist/scripts/generate-json-schema-validators.js +++ b/dist/scripts/generate-json-schema-validators.js @@ -26,10 +26,10 @@ const generateJsonSchemaValidators = (ROOT_SCHEMA_PATH, VALIDATOR_TARGET_PATH) = schemaList.forEach((schema) => { const schemaValidatorCode = ajv_pack_1.default(ajv, ajv.getSchema(schema.$id)); const relativeSchemaFilePath = path_1.default.relative(ROOT_SCHEMA_PATH, schema._filePath).replace(/.json$/, '.js'); - const targetScemaValidtorFilePath = path_1.default.resolve(VALIDATOR_TARGET_PATH, relativeSchemaFilePath); - const targetScemaValidtorFolderPath = path_1.default.dirname(targetScemaValidtorFilePath); - fs_extra_1.default.mkdirSync(targetScemaValidtorFolderPath, { recursive: true }); - fs_extra_1.default.writeFileSync(targetScemaValidtorFilePath, schemaValidatorCode); + const targetSchemaValidtorFilePath = path_1.default.resolve(VALIDATOR_TARGET_PATH, relativeSchemaFilePath); + const targetSchemaValidtorFolderPath = path_1.default.dirname(targetSchemaValidtorFilePath); + fs_extra_1.default.mkdirSync(targetSchemaValidtorFolderPath, { recursive: true }); + fs_extra_1.default.writeFileSync(targetSchemaValidtorFilePath, schemaValidatorCode); }); }); }; diff --git a/dist/scripts/generate-json-schema-validators.js.map b/dist/scripts/generate-json-schema-validators.js.map index 182b67a..dc1313e 100644 --- a/dist/scripts/generate-json-schema-validators.js.map +++ b/dist/scripts/generate-json-schema-validators.js.map @@ -1 +1 @@ -{"version":3,"file":"generate-json-schema-validators.js","sourceRoot":"","sources":["../../src/scripts/generate-json-schema-validators.ts"],"names":[],"mappings":";;;;;AAAA,wDAA0B;AAC1B,gDAAwB;AACxB,8CAAsB;AACtB,wDAA+B;AAE/B,oEAA0C;AAE1C,MAAM,4BAA4B,GAAG,CAAC,gBAAwB,EAAE,qBAA6B,EAAE,EAAE;IAE/F,wCAAwC;IACxC,kBAAE,CAAC,SAAS,CAAC,qBAAqB,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAEzD,OAAO,uBAAW,CAAC,gBAAgB,CAAC,CAAC,IAAI,CAAC,CAAC,kBAAkB,EAAE,EAAE;QAE/D,MAAM,cAAc,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC,cAAc,EAAE,EAAE;YAC/D,OAAO,cAAc,CAAC,QAAQ,CAAC;QACjC,CAAC,CAAC,CAAC;QAEH,MAAM,UAAU,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE;YACnD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,kBAAE,CAAC,YAAY,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC;YAC/D,MAAM,QAAQ,GAAG,cAAI,CAAC,QAAQ,CAAC,gBAAgB,EAAE,UAAU,CAAC,CAAC;YAC7D,MAAM,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC;YACzB,MAAM,CAAC,SAAS,GAAG,UAAU,CAAC;YAC9B,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC,CAAC;QAEH,MAAM,GAAG,GAAG,IAAI,aAAG,CAAC,EAAC,UAAU,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAC,CAAC,CAAC;QAE7D,UAAU,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE;YAE5B,MAAM,mBAAmB,GAAG,kBAAO,CAAC,GAAG,EAAE,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;YAEpE,MAAM,sBAAsB,GAAG,cAAI,CAAC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;YAC1G,MAAM,2BAA2B,GAAG,cAAI,CAAC,OAAO,CAAC,qBAAqB,EAAE,sBAAsB,CAAC,CAAC;YAChG,MAAM,6BAA6B,GAAG,cAAI,CAAC,OAAO,CAAC,2BAA2B,CAAC,CAAC;YAEhF,kBAAE,CAAC,SAAS,CAAC,6BAA6B,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;YACjE,kBAAE,CAAC,aAAa,CAAC,2BAA2B,EAAE,mBAAmB,CAAC,CAAC;QACrE,CAAC,CAAC,CAAC;IAEL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,kBAAe,4BAA4B,CAAC"} \ No newline at end of file +{"version":3,"file":"generate-json-schema-validators.js","sourceRoot":"","sources":["../../src/scripts/generate-json-schema-validators.ts"],"names":[],"mappings":";;;;;AAAA,wDAA0B;AAC1B,gDAAwB;AACxB,8CAAsB;AACtB,wDAA+B;AAE/B,oEAA0C;AAE1C,MAAM,4BAA4B,GAAG,CAAC,gBAAwB,EAAE,qBAA6B,EAAE,EAAE;IAE/F,wCAAwC;IACxC,kBAAE,CAAC,SAAS,CAAC,qBAAqB,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAEzD,OAAO,uBAAW,CAAC,gBAAgB,CAAC,CAAC,IAAI,CAAC,CAAC,kBAAkB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AE,CAAC,aAAa,CAAC,4BAA4B,EAAE,mBAAmB,CAAC,CAAC;QACtE,CAAC,CAAC,CAAC;IAEL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,kBAAe,4BAA4B,CAAC"} \ No newline at end of file diff --git a/dist/scripts/get-all-files.js.map b/dist/scripts/get-all-files.js.map index 022433c..1006d5e 100644 --- a/dist/scripts/get-all-files.js.map +++ b/dist/scripts/get-all-files.js.map @@ -1 +1 @@ -{"version":3,"file":"get-all-files.js","sourceRoot":"","sources":["../../src/scripts/get-all-files.ts"],"names":[],"mappings":";;;;;AAAA,wDAAgC;AAEhC,MAAM,WAAW,GAAG,CAAC,QAAQ,EAAE,EAAE;IAC/B,OAAO,kBAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AACpC,CAAC,CAAC;AAEF,kBAAe,WAAW,CAAC"} \ No newline at end of file +{"version":3,"file":"get-all-files.js","sourceRoot":"","sources":["../../src/scripts/get-all-files.ts"],"names":[],"mappings":";;;;;AAAA,wDAAgC;AAEhC,MAAM,WAAW,GAAG,CAAC,QAAgB,EAAE,EAAE;IACvC,OAAO,kBAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AACpC,CAAC,CAAC;AAEF,kBAAe,WAAW,CAAC"} \ No newline at end of file diff --git a/package.json b/package.json index b258cc5..5e6d7ad 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,9 @@ "yargs": "^14.0.0" }, "devDependencies": { + "@types/fs-extra": "^8.0.0", "@types/jest": "^24.0.18", + "@types/yargs": "^13.0.2", "husky": "^3.0.5", "jasmine": "^3.4.0", "jasmine-terminal-reporter": "^1.0.3", diff --git a/src/@types/json-schema-to-typescript/index.d.ts b/src/@types/json-schema-to-typescript/index.d.ts deleted file mode 100644 index 397aac7..0000000 --- a/src/@types/json-schema-to-typescript/index.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -/// - -declare module 'json-schema-to-typescript' { - export function compileFromFile (arg0: any, arg1: any): PromiseLike; -} \ No newline at end of file diff --git a/src/ajv-pack.d.ts b/src/ajv-pack.d.ts new file mode 100644 index 0000000..bf1a123 --- /dev/null +++ b/src/ajv-pack.d.ts @@ -0,0 +1,5 @@ +declare module "ajv-pack" { + import { Ajv, ValidateFunction } from "ajv"; + function ajvPack(instance: Ajv, validator: ValidateFunction): string; + export default ajvPack; +} diff --git a/src/index.ts b/src/index.ts index 2bfba26..7e72f9d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,13 +6,21 @@ import { compileFromFile } from 'json-schema-to-typescript'; import getAllFiles from './scripts/get-all-files'; import generateJsonSchemaValidators from './scripts/generate-json-schema-validators.js'; +interface Options { + source: string; +} + const commandLineArgs = yargs + .string('source') .alias('s', 'source') .describe('s', 'the directory for your json schema files') + .string('interface-target') .alias('i', 'interface-target') .describe('i', 'the output location for TypeScript interfaces') + .string('validator-target') .alias('v', 'validator-target') .describe('v', 'the output location for json schema validators') + .string('patterns') .alias('p', 'patterns') .describe('p', 'the location of a regex patterns module (optional)') .demandOption(['source'], 'The source (s) parameter is required.') @@ -35,7 +43,7 @@ if (commandLineArgs.v) { validatorTarget = path.join(jsonSchemaSourceDirectory, '..', 'json-schema-validators'); } -let patterns = {}; +let patterns: {[key: string]: JSON} = {}; if (commandLineArgs.patterns) { patterns = require(path.resolve(process.cwd(), commandLineArgs.patterns)); } @@ -55,7 +63,7 @@ console.log("REGEX PATTERNS:", patterns); const REGEX_IS_SCHEMA_FILE = /\.(json)$/i; const REGEX_PATTERN_TEMPLATE = /\$\{\s*PATTERN\s*([^\s]*)\s*\}/g; -const updateSchemaFile = (filePath) => { +const updateSchemaFile = (filePath: string) => { // read the file synchronously const rawFile = fs.readFileSync(filePath, 'utf8'); @@ -66,7 +74,7 @@ const updateSchemaFile = (filePath) => { throw new Error(`updateSchemaFile:RegexPattern - pattern not found '${matchText}'`); } - // remove start and end / for JSON SCHEMA purposes and also escape characers to make regex expression valid in JSON + // remove start and end / for JSON SCHEMA purposes and also escape characters to make regex expression valid in JSON return patterns[matchText].toString() .replace(/^\//, '') .replace(/\/$/, '') @@ -107,9 +115,7 @@ getAllFiles(TEMPORARY_SCHEMA_DIR).then((fileInfoList) => { // run typescript to json schema }).then((schemaFileList) => { - const allSchemaPromises = []; - - schemaFileList.forEach((schemaFilePath) => { + const allSchemaPromises = schemaFileList.map((schemaFilePath) => { const schemaPromise = compileFromFile(schemaFilePath, {}).then((ts) => { @@ -123,7 +129,7 @@ getAllFiles(TEMPORARY_SCHEMA_DIR).then((fileInfoList) => { return fs.writeFileSync(targetTypeScriptFilePath, ts); }); - allSchemaPromises.push(schemaPromise); + return schemaPromise; }); return Promise.all(allSchemaPromises); diff --git a/src/scripts/generate-json-schema-validators.ts b/src/scripts/generate-json-schema-validators.ts index 55f5c1d..c2a8507 100644 --- a/src/scripts/generate-json-schema-validators.ts +++ b/src/scripts/generate-json-schema-validators.ts @@ -31,11 +31,11 @@ const generateJsonSchemaValidators = (ROOT_SCHEMA_PATH: string, VALIDATOR_TARGET const schemaValidatorCode = AjvPack(ajv, ajv.getSchema(schema.$id)); const relativeSchemaFilePath = path.relative(ROOT_SCHEMA_PATH, schema._filePath).replace(/.json$/, '.js'); - const targetScemaValidtorFilePath = path.resolve(VALIDATOR_TARGET_PATH, relativeSchemaFilePath); - const targetScemaValidtorFolderPath = path.dirname(targetScemaValidtorFilePath); + const targetSchemaValidtorFilePath = path.resolve(VALIDATOR_TARGET_PATH, relativeSchemaFilePath); + const targetSchemaValidtorFolderPath = path.dirname(targetSchemaValidtorFilePath); - fs.mkdirSync(targetScemaValidtorFolderPath, { recursive: true }); - fs.writeFileSync(targetScemaValidtorFilePath, schemaValidatorCode); + fs.mkdirSync(targetSchemaValidtorFolderPath, { recursive: true }); + fs.writeFileSync(targetSchemaValidtorFilePath, schemaValidatorCode); }); }); diff --git a/src/scripts/get-all-files.ts b/src/scripts/get-all-files.ts index 064402d..9248916 100644 --- a/src/scripts/get-all-files.ts +++ b/src/scripts/get-all-files.ts @@ -1,6 +1,6 @@ import readdirp from 'readdirp'; -const getAllFiles = (rootPath) => { +const getAllFiles = (rootPath: string) => { return readdirp.promise(rootPath); }; diff --git a/tsconfig.json b/tsconfig.json index 218f651..66b8fa0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,13 @@ { - "compilerOptions": { - "module": "commonjs", - "esModuleInterop": true, - "target": "es6", - "moduleResolution": "node", - "sourceMap": true, - "outDir": "dist" - }, - "lib": ["es2015"], - "include": [ - "./src/**/*" - ] -} \ No newline at end of file + "compilerOptions": { + "module": "commonjs", + "esModuleInterop": true, + "strict": true, + "target": "es6", + "moduleResolution": "node", + "sourceMap": true, + "outDir": "dist" + }, + "lib": ["es2015"], + "include": ["./src/**/*"] +}