feat: add core tools

This commit is contained in:
Karl-Philipp Wulfert
2018-12-18 17:41:03 +01:00
commit 1ac90ef633
23 changed files with 3788 additions and 0 deletions

150
src/cli.ts Normal file
View File

@@ -0,0 +1,150 @@
/*
* Copyright (C) 2018 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as commander from 'commander';
import {existsSync, readFileSync, writeFileSync} from 'fs';
import {join, resolve} from 'path';
import {getProjectReflection, logger, mkdirPromisified, readFilePromisifed} from './common';
import {gatherRouteInformation, generateDocumentationForRoute, getNodeMetaInformationMap} from './routes';
import {Converter, getValidatableTypesFromReflection} from './schema';
import {validateFiles, writeReport} from './validate';
// handle unhandled promise rejections
process.on('unhandledRejection', (error: Error) => {
logger.error(error.message);
logger.info(error.stack);
process.exit(1);
});
commander
.version(JSON.parse(readFileSync(resolve(__dirname, '..', 'package.json')).toString()).version);
commander
.command('routes <srcPath> <mdPath>')
.action(async (relativeSrcPath, relativeMdPath) => {
// get absolute paths
const srcPath = resolve(relativeSrcPath);
const mdPath = resolve(relativeMdPath);
// get project reflection
const projectReflection = getProjectReflection(srcPath);
// get information about routes
const routes = await gatherRouteInformation(projectReflection);
// initialize markdown output
let output: string = '# Routes\n\n';
// generate documentation for all routes
routes.forEach((routeWithMetaInformation) => {
output += generateDocumentationForRoute(routeWithMetaInformation, getNodeMetaInformationMap(projectReflection));
});
// write documentation to file
writeFileSync(mdPath, output);
logger.ok(`Route documentation written to ${mdPath}.`);
});
commander
.command('schema <srcPath> <schemaPath>')
.action(async (relativeSrcPath, relativeSchemaPath) => {
// get absolute paths
const srcPath = resolve(relativeSrcPath);
const schemaPath = resolve(relativeSchemaPath);
// initialize new core converter
const coreConverter = new Converter(srcPath);
// get project reflection
const projectReflection = getProjectReflection(srcPath);
// get validatable types
const validatableTypes = getValidatableTypesFromReflection(projectReflection);
logger.info(`Found ${validatableTypes.length} type(s) to generate schemas for.`);
await mkdirPromisified(schemaPath, {
recursive: true,
});
logger.info(`Trying to find a package.json for ${srcPath}.`);
let path = srcPath;
// TODO: this check should be less ugly!
while (!existsSync(join(path, 'package.json')) && path.length > 5) {
path = resolve(path, '..');
}
const corePackageJsonPath = join(path, 'package.json');
logger.info(`Using ${corePackageJsonPath} to determine version for schemas.`);
const buffer = await readFilePromisifed(corePackageJsonPath);
const corePackageJson = JSON.parse(buffer.toString());
const coreVersion = corePackageJson.version;
logger.log(`Using ${coreVersion} as version for schemas.`);
// generate and write JSONSchema files for validatable types
validatableTypes.forEach((type) => {
const schema = coreConverter.getSchema(type, coreVersion);
const stringifiedSchema = JSON.stringify(schema, null, 2);
const file = join(schemaPath, type + '.json');
// write schema to file
writeFileSync(file, stringifiedSchema);
logger.info(`Generated schema for ${type} and saved to ${file}.`);
});
logger.ok(`Generated schemas for ${validatableTypes.length} type(s).`);
});
commander
.command('validate <schemaPath> <testPath> [reportPath]')
.action(async (relativeSchemaPath, relativeTestPath, relativeReportPath) => {
// get absolute paths
const schemaPath = resolve(relativeSchemaPath);
const testPath = resolve(relativeTestPath);
const errorsPerFile = await validateFiles(schemaPath, testPath);
let unexpected = false;
Object.keys(errorsPerFile).forEach((file) => {
unexpected = unexpected || errorsPerFile[file].some((error) => !error.expected);
});
if (typeof relativeReportPath !== 'undefined') {
const reportPath = resolve(relativeReportPath);
await writeReport(reportPath, errorsPerFile);
}
if (!unexpected) {
logger.ok('Successfully finished validation.');
} else {
logger.error('Unexpected errors occurred during validation');
process.exit(1);
}
});
commander
.parse(process.argv);
if (commander.args.length < 1) {
commander.outputHelp();
process.exit(1);
}