mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-11 12:12:55 +00:00
refactor: adjust code to updated dependencies
This commit is contained in:
25
src/cli.ts
25
src/cli.ts
@@ -12,10 +12,11 @@
|
|||||||
* You should have received a copy of the GNU General Public License along with
|
* You should have received a copy of the GNU General Public License along with
|
||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
import {Logger} from '@openstapps/logger';
|
||||||
import * as commander from 'commander';
|
import * as commander from 'commander';
|
||||||
import {existsSync, readFileSync, writeFileSync} from 'fs';
|
import {existsSync, readFileSync, writeFileSync} from 'fs';
|
||||||
import {join, resolve} from 'path';
|
import {join, resolve} from 'path';
|
||||||
import {getProjectReflection, logger, mkdirPromisified, readFilePromisified} from './common';
|
import {getProjectReflection, mkdirPromisified, readFilePromisified} from './common';
|
||||||
import {pack} from './pack';
|
import {pack} from './pack';
|
||||||
import {gatherRouteInformation, generateDocumentationForRoute, getNodeMetaInformationMap} from './routes';
|
import {gatherRouteInformation, generateDocumentationForRoute, getNodeMetaInformationMap} from './routes';
|
||||||
import {Converter, getValidatableTypesFromReflection} from './schema';
|
import {Converter, getValidatableTypesFromReflection} from './schema';
|
||||||
@@ -23,8 +24,8 @@ import {validateFiles, writeReport} from './validate';
|
|||||||
|
|
||||||
// handle unhandled promise rejections
|
// handle unhandled promise rejections
|
||||||
process.on('unhandledRejection', (error: Error) => {
|
process.on('unhandledRejection', (error: Error) => {
|
||||||
logger.error(error.message);
|
Logger.error(error.message);
|
||||||
logger.info(error.stack);
|
Logger.info(error.stack);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -55,7 +56,7 @@ commander
|
|||||||
// write documentation to file
|
// write documentation to file
|
||||||
writeFileSync(mdPath, output);
|
writeFileSync(mdPath, output);
|
||||||
|
|
||||||
logger.ok(`Route documentation written to ${mdPath}.`);
|
Logger.ok(`Route documentation written to ${mdPath}.`);
|
||||||
});
|
});
|
||||||
|
|
||||||
commander
|
commander
|
||||||
@@ -74,13 +75,13 @@ commander
|
|||||||
// get validatable types
|
// get validatable types
|
||||||
const validatableTypes = getValidatableTypesFromReflection(projectReflection);
|
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, {
|
await mkdirPromisified(schemaPath, {
|
||||||
recursive: true,
|
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;
|
let path = srcPath;
|
||||||
// TODO: this check should be less ugly!
|
// TODO: this check should be less ugly!
|
||||||
@@ -90,13 +91,13 @@ commander
|
|||||||
|
|
||||||
const corePackageJsonPath = join(path, 'package.json');
|
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 buffer = await readFilePromisified(corePackageJsonPath);
|
||||||
const corePackageJson = JSON.parse(buffer.toString());
|
const corePackageJson = JSON.parse(buffer.toString());
|
||||||
const coreVersion = corePackageJson.version;
|
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
|
// generate and write JSONSchema files for validatable types
|
||||||
validatableTypes.forEach((type) => {
|
validatableTypes.forEach((type) => {
|
||||||
@@ -109,10 +110,10 @@ commander
|
|||||||
// write schema to file
|
// write schema to file
|
||||||
writeFileSync(file, stringifiedSchema);
|
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
|
commander
|
||||||
@@ -135,9 +136,9 @@ commander
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!unexpected) {
|
if (!unexpected) {
|
||||||
logger.ok('Successfully finished validation.');
|
Logger.ok('Successfully finished validation.');
|
||||||
} else {
|
} else {
|
||||||
logger.error('Unexpected errors occurred during validation');
|
Logger.error('Unexpected errors occurred during validation');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,8 +22,6 @@ import {Definition} from 'ts-json-schema-generator';
|
|||||||
import {Application, ProjectReflection} from 'typedoc';
|
import {Application, ProjectReflection} from 'typedoc';
|
||||||
import {promisify} from 'util';
|
import {promisify} from 'util';
|
||||||
|
|
||||||
export const logger = new Logger();
|
|
||||||
|
|
||||||
export const globPromisified = promisify(glob);
|
export const globPromisified = promisify(glob);
|
||||||
export const mkdirPromisified = promisify(mkdir);
|
export const mkdirPromisified = promisify(mkdir);
|
||||||
export const readFilePromisified = promisify(readFile);
|
export const readFilePromisified = promisify(readFile);
|
||||||
@@ -116,7 +114,7 @@ export interface ExpectableValidationErrors {
|
|||||||
* @param srcPath Path to get reflection from
|
* @param srcPath Path to get reflection from
|
||||||
*/
|
*/
|
||||||
export function getProjectReflection(srcPath: PathLike): ProjectReflection {
|
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());
|
const tsconfigPath = getTsconfigPath(srcPath.toString());
|
||||||
|
|
||||||
@@ -178,7 +176,7 @@ export function getTsconfigPath(startPath: string): string {
|
|||||||
tsconfigPath = tsconfigPathParts.join(sep);
|
tsconfigPath = tsconfigPathParts.join(sep);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`Using 'tsconfig.json' from ${tsconfigPath}.`);
|
Logger.info(`Using 'tsconfig.json' from ${tsconfigPath}.`);
|
||||||
|
|
||||||
return tsconfigPath;
|
return tsconfigPath;
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/pack.ts
17
src/pack.ts
@@ -12,11 +12,12 @@
|
|||||||
* You should have received a copy of the GNU General Public License along with
|
* You should have received a copy of the GNU General Public License along with
|
||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
import {Logger} from '@openstapps/logger';
|
||||||
import * as del from 'del';
|
import * as del from 'del';
|
||||||
import {existsSync} from 'fs';
|
import {existsSync} from 'fs';
|
||||||
import {basename, dirname, join} from 'path';
|
import {basename, dirname, join} from 'path';
|
||||||
import {cwd} from 'process';
|
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 */';
|
const PACK_IDENTIFIER = '/* PACKED BY @openstapps/pack */';
|
||||||
|
|
||||||
@@ -60,7 +61,7 @@ async function packCliJs(): Promise<void> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info('Adjusting JavaScript CLI...');
|
Logger.info('Adjusting JavaScript CLI...');
|
||||||
|
|
||||||
const buffer = await readFilePromisified(path);
|
const buffer = await readFilePromisified(path);
|
||||||
const content = buffer.toString();
|
const content = buffer.toString();
|
||||||
@@ -137,7 +138,7 @@ async function getAllTypeDefinitions(): Promise<string[]> {
|
|||||||
* Pack a list of type definitions into one file
|
* Pack a list of type definitions into one file
|
||||||
*/
|
*/
|
||||||
async function packTypeDefinitions(): Promise<void> {
|
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');
|
const path = join(cwd(), 'lib', 'index.d.ts');
|
||||||
|
|
||||||
@@ -251,7 +252,7 @@ async function getAllJavaScriptModules(): Promise<JavaScriptModule[]> {
|
|||||||
async function packJavaScriptFiles(): Promise<void> {
|
async function packJavaScriptFiles(): Promise<void> {
|
||||||
const path = join(cwd(), 'lib', 'index.js');
|
const path = join(cwd(), 'lib', 'index.js');
|
||||||
|
|
||||||
logger.info('Packing JavaScript files...');
|
Logger.info('Packing JavaScript files...');
|
||||||
|
|
||||||
await deleteFileIfExistingAndPacked(path);
|
await deleteFileIfExistingAndPacked(path);
|
||||||
|
|
||||||
@@ -293,7 +294,7 @@ async function packJavaScriptFiles(): Promise<void> {
|
|||||||
return whiteSpace + 'const ' + importedName + ' = require(\'../src/' + modulePath + '\');';
|
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;
|
return line;
|
||||||
@@ -350,7 +351,7 @@ async function deleteFileIfExistingAndPacked(path: string): Promise<void> {
|
|||||||
|
|
||||||
// check if packed by this script
|
// check if packed by this script
|
||||||
if (content.indexOf(PACK_IDENTIFIER) === 0) {
|
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);
|
return await unlinkPromisified(path);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -427,7 +428,7 @@ function topologicalSort(modules: JavaScriptModule[]): JavaScriptModule[] {
|
|||||||
* Pack
|
* Pack
|
||||||
*/
|
*/
|
||||||
export async function pack() {
|
export async function pack() {
|
||||||
logger.log(`Packing project in ${process.cwd()}...`);
|
Logger.log(`Packing project in ${process.cwd()}...`);
|
||||||
|
|
||||||
// run all tasks in parallel
|
// run all tasks in parallel
|
||||||
const promises: Array<Promise<void>> = [
|
const promises: Array<Promise<void>> = [
|
||||||
@@ -439,7 +440,7 @@ export async function pack() {
|
|||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
|
|
||||||
// clean up afterwards
|
// clean up afterwards
|
||||||
logger.info('Deleting extraneous files...');
|
Logger.info('Deleting extraneous files...');
|
||||||
|
|
||||||
await del([
|
await del([
|
||||||
// delete all transpiled files
|
// delete all transpiled files
|
||||||
|
|||||||
@@ -13,8 +13,9 @@
|
|||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
import {asyncPool} from '@krlwlfrt/async-pool';
|
import {asyncPool} from '@krlwlfrt/async-pool';
|
||||||
|
import {Logger} from '@openstapps/logger';
|
||||||
import {ProjectReflection} from 'typedoc';
|
import {ProjectReflection} from 'typedoc';
|
||||||
import {logger, NodesWithMetaInformation, NodeWithMetaInformation, RouteWithMetaInformation} from './common';
|
import {NodesWithMetaInformation, NodeWithMetaInformation, RouteWithMetaInformation} from './common';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gather relevant information of routes
|
* Gather relevant information of routes
|
||||||
@@ -38,11 +39,11 @@ export async function gatherRouteInformation(reflection: ProjectReflection): Pro
|
|||||||
if (node.extendedTypes.some((extendedType) => {
|
if (node.extendedTypes.some((extendedType) => {
|
||||||
return (extendedType as any).name === 'SCAbstractRoute';
|
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$/)) {
|
if (module.originalName.match(/\.d\.ts$/)) {
|
||||||
module.originalName = module.originalName.substr(0, module.originalName.length - 5);
|
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);
|
const importedModule = await import(module.originalName);
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
import {asyncPool} from '@krlwlfrt/async-pool';
|
import {asyncPool} from '@krlwlfrt/async-pool';
|
||||||
|
import {Logger} from '@openstapps/logger';
|
||||||
import {PathLike} from 'fs';
|
import {PathLike} from 'fs';
|
||||||
import {Schema, Validator as JSONSchemaValidator, ValidatorResult} from 'jsonschema';
|
import {Schema, Validator as JSONSchemaValidator, ValidatorResult} from 'jsonschema';
|
||||||
import * as mustache from 'mustache';
|
import * as mustache from 'mustache';
|
||||||
@@ -20,7 +21,6 @@ import {basename, join, resolve} from 'path';
|
|||||||
import {
|
import {
|
||||||
ExpectableValidationErrors,
|
ExpectableValidationErrors,
|
||||||
globPromisified,
|
globPromisified,
|
||||||
logger,
|
|
||||||
readFilePromisified,
|
readFilePromisified,
|
||||||
writeFilePromisified,
|
writeFilePromisified,
|
||||||
} from './common';
|
} from './common';
|
||||||
@@ -58,7 +58,7 @@ export class Validator {
|
|||||||
throw new Error(`No schema files in ${schemaDir.toString()}!`);
|
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
|
// Iterate over schema files
|
||||||
await asyncPool(2, schemaFiles, async (file) => {
|
await asyncPool(2, schemaFiles, async (file) => {
|
||||||
@@ -72,7 +72,7 @@ export class Validator {
|
|||||||
// add schema to map
|
// add schema to map
|
||||||
this.schemas[basename(file, '.json')] = schema;
|
this.schemas[basename(file, '.json')] = schema;
|
||||||
|
|
||||||
logger.info(`Added ${file} to validator.`);
|
Logger.info(`Added ${file} to validator.`);
|
||||||
});
|
});
|
||||||
|
|
||||||
return schemaFiles;
|
return schemaFiles;
|
||||||
@@ -117,7 +117,7 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
|
|||||||
throw new Error(`No test files in ${resourcesDir}!`);
|
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
|
// map of errors per file
|
||||||
const errors: ExpectableValidationErrors = {};
|
const errors: ExpectableValidationErrors = {};
|
||||||
@@ -156,7 +156,7 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
|
|||||||
expected = true;
|
expected = true;
|
||||||
} else {
|
} else {
|
||||||
unexpectedErrors++;
|
unexpectedErrors++;
|
||||||
logger.error(`Unexpected error ${error.name} in ${testFile}`);
|
Logger.error(`Unexpected error ${error.name} in ${testFile}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// add error to list of errors
|
// add error to list of errors
|
||||||
@@ -169,7 +169,7 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
|
|||||||
|
|
||||||
if (expectedErrors.length > 0) {
|
if (expectedErrors.length > 0) {
|
||||||
expectedErrors.forEach((error) => {
|
expectedErrors.forEach((error) => {
|
||||||
logger.error(`Extraneous expected error '${error}' in ${testFile}.`);
|
Logger.error(`Extraneous expected error '${error}' in ${testFile}.`);
|
||||||
errors[testFileName].push({
|
errors[testFileName].push({
|
||||||
argument: false,
|
argument: false,
|
||||||
expected: false,
|
expected: false,
|
||||||
@@ -181,7 +181,7 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else if (unexpectedErrors === 0) {
|
} 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(),
|
timestamp: (new Date()).toISOString(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
logger.ok(`Wrote report to ${reportPath}.`);
|
Logger.ok(`Wrote report to ${reportPath}.`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,13 +12,14 @@
|
|||||||
* You should have received a copy of the GNU General Public License along with
|
* You should have received a copy of the GNU General Public License along with
|
||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
import {Logger} from '@openstapps/logger';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
import {slow, suite, test, timeout} from 'mocha-typescript';
|
import {slow, suite, test, timeout} from 'mocha-typescript';
|
||||||
import {cwd} from 'process';
|
import {cwd} from 'process';
|
||||||
import {getTsconfigPath, logger} from '../src/common';
|
import {getTsconfigPath} from '../src/common';
|
||||||
|
|
||||||
process.on('unhandledRejection', (err) => {
|
process.on('unhandledRejection', (err) => {
|
||||||
logger.error('UNHANDLED REJECTION', err.stack);
|
Logger.error('UNHANDLED REJECTION', err.stack);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -18,10 +18,8 @@ import {slow, suite, test, timeout} from 'mocha-typescript';
|
|||||||
import {join} from 'path';
|
import {join} from 'path';
|
||||||
import {Converter, getValidatableTypesFromReflection} from '../src/schema';
|
import {Converter, getValidatableTypesFromReflection} from '../src/schema';
|
||||||
|
|
||||||
const logger = new Logger();
|
|
||||||
|
|
||||||
process.on('unhandledRejection', (err) => {
|
process.on('unhandledRejection', (err) => {
|
||||||
logger.error('UNHANDLED REJECTION', err.stack);
|
Logger.error('UNHANDLED REJECTION', err.stack);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user