refactor: adjust code to updated dependencies

This commit is contained in:
Karl-Philipp Wulfert
2019-05-27 18:01:58 +02:00
parent 3fda81d279
commit 13b4d3d498
7 changed files with 40 additions and 40 deletions

View File

@@ -12,10 +12,11 @@
* 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 {Logger} from '@openstapps/logger';
import * as commander from 'commander';
import {existsSync, readFileSync, writeFileSync} from 'fs';
import {join, resolve} from 'path';
import {getProjectReflection, logger, mkdirPromisified, readFilePromisified} from './common';
import {getProjectReflection, mkdirPromisified, readFilePromisified} from './common';
import {pack} from './pack';
import {gatherRouteInformation, generateDocumentationForRoute, getNodeMetaInformationMap} from './routes';
import {Converter, getValidatableTypesFromReflection} from './schema';
@@ -23,8 +24,8 @@ import {validateFiles, writeReport} from './validate';
// handle unhandled promise rejections
process.on('unhandledRejection', (error: Error) => {
logger.error(error.message);
logger.info(error.stack);
Logger.error(error.message);
Logger.info(error.stack);
process.exit(1);
});
@@ -55,7 +56,7 @@ commander
// write documentation to file
writeFileSync(mdPath, output);
logger.ok(`Route documentation written to ${mdPath}.`);
Logger.ok(`Route documentation written to ${mdPath}.`);
});
commander
@@ -74,13 +75,13 @@ commander
// get validatable types
const validatableTypes = getValidatableTypesFromReflection(projectReflection);
logger.info(`Found ${validatableTypes.length} type(s) to generate schemas for.`);
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}.`);
Logger.info(`Trying to find a package.json for ${srcPath}.`);
let path = srcPath;
// TODO: this check should be less ugly!
@@ -90,13 +91,13 @@ commander
const corePackageJsonPath = join(path, 'package.json');
logger.info(`Using ${corePackageJsonPath} to determine version for schemas.`);
Logger.info(`Using ${corePackageJsonPath} to determine version for schemas.`);
const buffer = await readFilePromisified(corePackageJsonPath);
const corePackageJson = JSON.parse(buffer.toString());
const coreVersion = corePackageJson.version;
logger.log(`Using ${coreVersion} as version for schemas.`);
Logger.log(`Using ${coreVersion} as version for schemas.`);
// generate and write JSONSchema files for validatable types
validatableTypes.forEach((type) => {
@@ -109,10 +110,10 @@ commander
// write schema to file
writeFileSync(file, stringifiedSchema);
logger.info(`Generated schema for ${type} and saved to ${file}.`);
Logger.info(`Generated schema for ${type} and saved to ${file}.`);
});
logger.ok(`Generated schemas for ${validatableTypes.length} type(s).`);
Logger.ok(`Generated schemas for ${validatableTypes.length} type(s).`);
});
commander
@@ -135,9 +136,9 @@ commander
}
if (!unexpected) {
logger.ok('Successfully finished validation.');
Logger.ok('Successfully finished validation.');
} else {
logger.error('Unexpected errors occurred during validation');
Logger.error('Unexpected errors occurred during validation');
process.exit(1);
}
});

View File

@@ -22,8 +22,6 @@ import {Definition} from 'ts-json-schema-generator';
import {Application, ProjectReflection} from 'typedoc';
import {promisify} from 'util';
export const logger = new Logger();
export const globPromisified = promisify(glob);
export const mkdirPromisified = promisify(mkdir);
export const readFilePromisified = promisify(readFile);
@@ -116,7 +114,7 @@ export interface ExpectableValidationErrors {
* @param srcPath Path to get reflection from
*/
export function getProjectReflection(srcPath: PathLike): ProjectReflection {
logger.info(`Generating project reflection for ${srcPath.toString()}.`);
Logger.info(`Generating project reflection for ${srcPath.toString()}.`);
const tsconfigPath = getTsconfigPath(srcPath.toString());
@@ -178,7 +176,7 @@ export function getTsconfigPath(startPath: string): string {
tsconfigPath = tsconfigPathParts.join(sep);
}
logger.info(`Using 'tsconfig.json' from ${tsconfigPath}.`);
Logger.info(`Using 'tsconfig.json' from ${tsconfigPath}.`);
return tsconfigPath;
}

View File

@@ -12,11 +12,12 @@
* 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 {Logger} from '@openstapps/logger';
import * as del from 'del';
import {existsSync} from 'fs';
import {basename, dirname, join} from 'path';
import {cwd} from 'process';
import {globPromisified, logger, readFilePromisified, unlinkPromisified, writeFilePromisified} from './common';
import {globPromisified, readFilePromisified, unlinkPromisified, writeFilePromisified} from './common';
const PACK_IDENTIFIER = '/* PACKED BY @openstapps/pack */';
@@ -60,7 +61,7 @@ async function packCliJs(): Promise<void> {
return;
}
logger.info('Adjusting JavaScript CLI...');
Logger.info('Adjusting JavaScript CLI...');
const buffer = await readFilePromisified(path);
const content = buffer.toString();
@@ -137,7 +138,7 @@ async function getAllTypeDefinitions(): Promise<string[]> {
* Pack a list of type definitions into one file
*/
async function packTypeDefinitions(): Promise<void> {
logger.info('Packing TypeScript definition files...');
Logger.info('Packing TypeScript definition files...');
const path = join(cwd(), 'lib', 'index.d.ts');
@@ -251,7 +252,7 @@ async function getAllJavaScriptModules(): Promise<JavaScriptModule[]> {
async function packJavaScriptFiles(): Promise<void> {
const path = join(cwd(), 'lib', 'index.js');
logger.info('Packing JavaScript files...');
Logger.info('Packing JavaScript files...');
await deleteFileIfExistingAndPacked(path);
@@ -293,7 +294,7 @@ async function packJavaScriptFiles(): Promise<void> {
return whiteSpace + 'const ' + importedName + ' = require(\'../src/' + modulePath + '\');';
}
logger.warn('Import ' + importedName + ' could not be found in module.directory ' + modulePath);
Logger.warn('Import ' + importedName + ' could not be found in module.directory ' + modulePath);
}
return line;
@@ -350,7 +351,7 @@ async function deleteFileIfExistingAndPacked(path: string): Promise<void> {
// check if packed by this script
if (content.indexOf(PACK_IDENTIFIER) === 0) {
logger.log('Found `' + path + '` which is packed by this script. Deleting it...');
Logger.log('Found `' + path + '` which is packed by this script. Deleting it...');
return await unlinkPromisified(path);
}
} catch (err) {
@@ -427,7 +428,7 @@ function topologicalSort(modules: JavaScriptModule[]): JavaScriptModule[] {
* Pack
*/
export async function pack() {
logger.log(`Packing project in ${process.cwd()}...`);
Logger.log(`Packing project in ${process.cwd()}...`);
// run all tasks in parallel
const promises: Array<Promise<void>> = [
@@ -439,7 +440,7 @@ export async function pack() {
await Promise.all(promises);
// clean up afterwards
logger.info('Deleting extraneous files...');
Logger.info('Deleting extraneous files...');
await del([
// delete all transpiled files

View File

@@ -13,8 +13,9 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {asyncPool} from '@krlwlfrt/async-pool';
import {Logger} from '@openstapps/logger';
import {ProjectReflection} from 'typedoc';
import {logger, NodesWithMetaInformation, NodeWithMetaInformation, RouteWithMetaInformation} from './common';
import {NodesWithMetaInformation, NodeWithMetaInformation, RouteWithMetaInformation} from './common';
/**
* Gather relevant information of routes
@@ -38,11 +39,11 @@ export async function gatherRouteInformation(reflection: ProjectReflection): Pro
if (node.extendedTypes.some((extendedType) => {
return (extendedType as any).name === 'SCAbstractRoute';
})) {
logger.info(`Found ${node.name} in ${module.originalName}.`);
Logger.info(`Found ${node.name} in ${module.originalName}.`);
if (module.originalName.match(/\.d\.ts$/)) {
module.originalName = module.originalName.substr(0, module.originalName.length - 5);
logger.info(`Using compiled version of module in ${module.originalName}.`);
Logger.info(`Using compiled version of module in ${module.originalName}.`);
}
const importedModule = await import(module.originalName);

View File

@@ -13,6 +13,7 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {asyncPool} from '@krlwlfrt/async-pool';
import {Logger} from '@openstapps/logger';
import {PathLike} from 'fs';
import {Schema, Validator as JSONSchemaValidator, ValidatorResult} from 'jsonschema';
import * as mustache from 'mustache';
@@ -20,7 +21,6 @@ import {basename, join, resolve} from 'path';
import {
ExpectableValidationErrors,
globPromisified,
logger,
readFilePromisified,
writeFilePromisified,
} from './common';
@@ -58,7 +58,7 @@ export class Validator {
throw new Error(`No schema files in ${schemaDir.toString()}!`);
}
logger.log(`Adding schemas from ${schemaDir} to validator.`);
Logger.log(`Adding schemas from ${schemaDir} to validator.`);
// Iterate over schema files
await asyncPool(2, schemaFiles, async (file) => {
@@ -72,7 +72,7 @@ export class Validator {
// add schema to map
this.schemas[basename(file, '.json')] = schema;
logger.info(`Added ${file} to validator.`);
Logger.info(`Added ${file} to validator.`);
});
return schemaFiles;
@@ -117,7 +117,7 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
throw new Error(`No test files in ${resourcesDir}!`);
}
logger.log(`Found ${testFiles.length} file(s) to test.`);
Logger.log(`Found ${testFiles.length} file(s) to test.`);
// map of errors per file
const errors: ExpectableValidationErrors = {};
@@ -156,7 +156,7 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
expected = true;
} else {
unexpectedErrors++;
logger.error(`Unexpected error ${error.name} in ${testFile}`);
Logger.error(`Unexpected error ${error.name} in ${testFile}`);
}
// add error to list of errors
@@ -169,7 +169,7 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
if (expectedErrors.length > 0) {
expectedErrors.forEach((error) => {
logger.error(`Extraneous expected error '${error}' in ${testFile}.`);
Logger.error(`Extraneous expected error '${error}' in ${testFile}.`);
errors[testFileName].push({
argument: false,
expected: false,
@@ -181,7 +181,7 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
});
});
} else if (unexpectedErrors === 0) {
logger.info(`Successfully validated ${testFile}.`);
Logger.info(`Successfully validated ${testFile}.`);
}
});
@@ -231,5 +231,5 @@ export async function writeReport(reportPath: PathLike, errors: ExpectableValida
timestamp: (new Date()).toISOString(),
}));
logger.ok(`Wrote report to ${reportPath}.`);
Logger.ok(`Wrote report to ${reportPath}.`);
}

View File

@@ -12,13 +12,14 @@
* 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 {Logger} from '@openstapps/logger';
import {expect} from 'chai';
import {slow, suite, test, timeout} from 'mocha-typescript';
import {cwd} from 'process';
import {getTsconfigPath, logger} from '../src/common';
import {getTsconfigPath} from '../src/common';
process.on('unhandledRejection', (err) => {
logger.error('UNHANDLED REJECTION', err.stack);
Logger.error('UNHANDLED REJECTION', err.stack);
process.exit(1);
});

View File

@@ -18,10 +18,8 @@ import {slow, suite, test, timeout} from 'mocha-typescript';
import {join} from 'path';
import {Converter, getValidatableTypesFromReflection} from '../src/schema';
const logger = new Logger();
process.on('unhandledRejection', (err) => {
logger.error('UNHANDLED REJECTION', err.stack);
Logger.error('UNHANDLED REJECTION', err.stack);
process.exit(1);
});