Skip to content

Commit

Permalink
Cleanup, add CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian committed Jul 23, 2018
1 parent 04132ea commit c4b70c9
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 102 deletions.
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
{
"author": "Florian Keller <github@floriankeller.de>",
"bin": {
"schemastore-updater": "dist/cli.js"
},
"dependencies": {
"@types/fs-extra": "5.0.4",
"@types/rimraf": "2.0.2",
"commander": "2.16.0",
"fs-extra": "7.0.0",
"json-schema-to-typescript": "5.5.0",
"program": "1.0.0",
"rimraf": "2.6.2",
"simple-git": "1.96.0",
"typescript": "2.9.2"
},
"devDependencies": {
"cross-env": "5.2.0",
"logdown": "3.2.3",
"prettier": "1.13.7"
},
"files": [
"src",
"settings.json"
],
"license": "MIT",
"main": "index.js",
"name": "schemastore-updater",
Expand All @@ -22,7 +31,7 @@
"clear": "rimraf dist",
"dist": "yarn clear && yarn build",
"prettier": "prettier --single-quote --write src/*.ts",
"start": "node dist/index.js"
"start": "cross-env NODE_DEBUG='schemastore-updater/*' node dist/cli.js"
},
"version": "1.0.0"
}
File renamed without changes.
6 changes: 2 additions & 4 deletions settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
"vega-lite.json",
"vega.json"
],
"lockFile": "json-schemas.lock.json",
"schemaStoreDir": "./temp/schemastore",
"schemaStoreRepo": "https://github.com/SchemaStore/schemastore",
"tempDir": "./temp"
"lockFile": "schemas/json-schemas.lock",
"schemaStoreRepo": "https://github.com/SchemaStore/schemastore"
}
36 changes: 36 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { SchemaGenerator } from './';
import * as program from 'commander';
import * as path from 'path';

const {description, name, version}: {description: string, name: string, version: string} = require('../package.json');

program
.name(name)
.version(version)
.description(description)
.option(
'-s, --settings <file>',
'Specify a settings file',
path.resolve(__dirname, '..', 'settings.json')
)
.parse(process.argv);

const settingsFile = path.resolve(program.settings);

const {
disabledSchemas,
schemaStoreRepo
}: {
disabledSchemas: string[];
schemaStoreRepo: string;
} = require(settingsFile);

const generator = new SchemaGenerator(
disabledSchemas,
schemaStoreRepo
);

generator
.start()
.then(() => console.log('Done.'))
.catch(error => console.error(error));
148 changes: 83 additions & 65 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,55 @@ import * as simpleGit from 'simple-git/promise';
import * as fs from 'fs-extra';
import * as crypto from 'crypto';
import * as schemaGenerator from 'json-schema-to-typescript';
const logdown = require('logdown');

interface SchemaHashes {
[fileName: string]: string;
}

const git = simpleGit('.');
const {
disabledSchemas,
lockFile,
schemaStoreDir,
schemaStoreRepo
}: {
disabledSchemas: string;
lockFile: string;
schemaStoreDir: string;
schemaStoreRepo: string;
} = require('../settings.json');
const schemaStoreDirResolved = path.resolve(schemaStoreDir);
const jsonSchemasDir = path.join(schemaStoreDir, 'src', 'schemas', 'json');
const lockFileResolved = path.resolve(lockFile);

function generateLockFile(fileName: string, data: SchemaHashes) {
return fs.writeFile(path.resolve(fileName), JSON.stringify(data, null, 2), {
encoding: 'utf8'
});
}
class SchemaGenerator {
private git: simpleGit.SimpleGit;
private jsonSchemasDir: string;
private lockFile: string;
private schemaStoreDirResolved: string;
private logger: any;

constructor(
private disabledSchemas: string[],
private schemaStoreRepo: string
) {
this.git = simpleGit('.');
this.schemaStoreDirResolved = path.join('temp', 'schemastore');
this.jsonSchemasDir = path.join(this.schemaStoreDirResolved, 'src', 'schemas', 'json');
this.lockFile = path.join('schemas', 'json-schemas.lock');
this.logger = logdown('schemastore-updater', {
logger: console,
markdown: false,
});
this.logger.info(`Using lockfile "${this.lockFile}".`)
}

private generateLockFile(fileName: string, data: SchemaHashes) {
return fs.writeFile(path.resolve(fileName), JSON.stringify(data, null, 2), {
encoding: 'utf8'
});
}

async function generateSchemas(jsonData: SchemaHashes) {
const disabled = [];
for (const fileName in jsonData) {
const fileNameResolved = path.resolve(jsonSchemasDir, fileName);
console.log({ fileName });
try {
const newSchema = await schemaGenerator.compileFromFile(fileNameResolved);
private async generateSchemas(jsonData: SchemaHashes) {
const disabledSchemas = [];
for (const fileName in jsonData) {
const fileNameResolved = path.resolve(this.jsonSchemasDir, fileName);
this.logger.info(`Processing "${fileName}" ...`);
let newSchema = '';
try {
newSchema = await schemaGenerator.compileFromFile(
fileNameResolved
);
} catch (error) {
disabledSchemas.push(fileName);
break;
}
const schemaDirResolved = path.resolve(
'schemas',
fileName.replace('.json', '')
Expand All @@ -49,52 +64,55 @@ async function generateSchemas(jsonData: SchemaHashes) {
newSchema,
{ encoding: 'utf8' }
);
} catch (error) {
disabled.push(fileName);
}
return { disabledSchemas };
}
console.log({ disabled });
}

async function start() {
await promisify(rimraf)(schemaStoreDirResolved);
await git.clone(schemaStoreRepo, schemaStoreDirResolved, ['--depth=1']);
async start() {
await promisify(rimraf)(this.schemaStoreDirResolved);
await this.git.clone(this.schemaStoreRepo, this.schemaStoreDirResolved, [
'--depth=1'
]);

const jsonFiles = (await fs.readdir(jsonSchemasDir)).filter(
fileName =>
fileName.endsWith('.json') && !disabledSchemas.includes(fileName)
);
const jsonData: SchemaHashes = {};
for (const fileName of jsonFiles) {
const fileNameResolved = path.resolve(jsonSchemasDir, fileName);
const fileContent = await fs.readFile(fileNameResolved, {
encoding: 'utf8'
});
const sha256 = crypto
.createHash('sha256')
.update(fileContent)
.digest('hex');
const jsonFiles = (await fs.readdir(this.jsonSchemasDir)).filter(
fileName =>
fileName.endsWith('.json') && !this.disabledSchemas.includes(fileName)
);
this.logger.info(`Loaded ${jsonFiles.length} schemas.`);
const jsonData: SchemaHashes = {};
for (const fileName of jsonFiles) {
const fileNameResolved = path.resolve(this.jsonSchemasDir, fileName);
const fileContent = await fs.readFile(fileNameResolved, {
encoding: 'utf8'
});
const sha256 = crypto
.createHash('sha256')
.update(fileContent)
.digest('hex');

jsonData[fileName] = sha256;
}
jsonData[fileName] = sha256;
}

if (!fs.existsSync(lockFileResolved)) {
await generateSchemas(jsonData);
await generateLockFile(lockFileResolved, jsonData);
} else {
const lockFileData = await fs.readFile(lockFile, { encoding: 'utf8' });
const lockFileParsed = JSON.parse(lockFileData);
const failedHashes: SchemaHashes = {};
for (const fileName in jsonData) {
if (
!lockFileParsed[fileName] ||
jsonData[fileName] !== lockFileParsed[fileName]
) {
failedHashes[fileName] = jsonData[fileName];
if (!fs.existsSync(this.lockFile)) {
this.logger.info(`No lockfile exists yet. Writing lockfile to "${this.lockFile}" ..`)
await this.generateSchemas(jsonData);
await this.generateLockFile(this.lockFile, jsonData);
} else {
const lockFileData = await fs.readFile(this.lockFile, { encoding: 'utf8' });
const lockFileParsed = JSON.parse(lockFileData);
const updatedHashes: SchemaHashes = {};
for (const fileName in jsonData) {
if (
!lockFileParsed[fileName] ||
jsonData[fileName] !== lockFileParsed[fileName]
) {
this.logger.info(`Hash from "${fileName}" is outdated. Updating.`)
updatedHashes[fileName] = jsonData[fileName];
}
}
await this.generateSchemas(updatedHashes);
}
await generateSchemas(failedHashes);
}
}

start();
export { SchemaGenerator };
Loading

0 comments on commit c4b70c9

Please sign in to comment.