Skip to content

Commit

Permalink
Merge pull request gitsome#2 from ChristianMurphy/types/strict-typing
Browse files Browse the repository at this point in the history
types: strict typing
  • Loading branch information
gitsome authored Sep 16, 2019
2 parents 8841539 + 7e38576 commit 340e46b
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 41 deletions.
11 changes: 7 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions dist/scripts/generate-json-schema-validators.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/scripts/generate-json-schema-validators.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/scripts/get-all-files.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 0 additions & 5 deletions src/@types/json-schema-to-typescript/index.d.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/ajv-pack.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module "ajv-pack" {
import { Ajv, ValidateFunction } from "ajv";
function ajvPack(instance: Ajv, validator: ValidateFunction): string;
export default ajvPack;
}
20 changes: 13 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand All @@ -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));
}
Expand All @@ -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');
Expand All @@ -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(/\/$/, '')
Expand Down Expand Up @@ -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) => {

Expand All @@ -123,7 +129,7 @@ getAllFiles(TEMPORARY_SCHEMA_DIR).then((fileInfoList) => {
return fs.writeFileSync(targetTypeScriptFilePath, ts);
});

allSchemaPromises.push(schemaPromise);
return schemaPromise;
});

return Promise.all(allSchemaPromises);
Expand Down
Loading

0 comments on commit 340e46b

Please sign in to comment.