mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-18 15:42:54 +00:00
110 lines
3.4 KiB
TypeScript
110 lines
3.4 KiB
TypeScript
/*
|
|
* 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 {Schema as JSONSchema} from 'jsonschema';
|
|
import {join} from 'path';
|
|
import {DEFAULT_CONFIG, SchemaGenerator} from 'ts-json-schema-generator';
|
|
import {createFormatter} from 'ts-json-schema-generator/dist/factory/formatter';
|
|
import {createParser} from 'ts-json-schema-generator/dist/factory/parser';
|
|
import {createProgram} from 'ts-json-schema-generator/dist/factory/program';
|
|
import {ProjectReflection} from 'typedoc';
|
|
import * as ts from 'typescript';
|
|
import {isSchemaWithDefinitions} from './common';
|
|
|
|
/**
|
|
* StAppsCore converter
|
|
*
|
|
* Converts TypeScript source files to JSON schema files
|
|
*/
|
|
export class Converter {
|
|
private generator: SchemaGenerator;
|
|
|
|
/**
|
|
* Create a new converter
|
|
*
|
|
* @param path Path to the project
|
|
*/
|
|
constructor(path: string) {
|
|
// set config for schema generator
|
|
const config = {
|
|
...DEFAULT_CONFIG,
|
|
// expose: 'exported' as any,
|
|
// jsDoc: 'extended' as any,
|
|
path: join(path, '**/*.ts'),
|
|
sortProps: true,
|
|
topRef: false,
|
|
type: 'SC',
|
|
};
|
|
|
|
// create TypeScript program from config
|
|
const program: ts.Program = createProgram(config);
|
|
|
|
// create generator
|
|
this.generator = new SchemaGenerator(
|
|
program,
|
|
createParser(program, config),
|
|
createFormatter(config),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get schema for specific StAppsCore type
|
|
*
|
|
* @param {string} type Type to get the schema for
|
|
* @param {string} version Version to set for the schema
|
|
* @returns {Schema} Generated schema
|
|
*/
|
|
getSchema(type: string, version: string): JSONSchema {
|
|
// generate schema for this file/type
|
|
const schema: JSONSchema = this.generator.createSchema(type);
|
|
|
|
// set id of schema
|
|
schema.id = 'https://core.stapps.tu-berlin.de/v' + version + '/lib/schema/' + type + '.json';
|
|
|
|
if (isSchemaWithDefinitions(schema)) {
|
|
// add self reference to definitions
|
|
schema.definitions['SC' + type] = Object.assign({}, schema.properties);
|
|
}
|
|
|
|
return schema;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get a list of validatable types from a reflection
|
|
*
|
|
* @param projectReflection Reflection to get validatable types from
|
|
*/
|
|
export function getValidatableTypesFromReflection(projectReflection: ProjectReflection): string[] {
|
|
const validatableTypes: string[] = [];
|
|
|
|
// iterate over modules
|
|
projectReflection.children.forEach((module) => {
|
|
if (Array.isArray(module.children) && module.children.length > 0) {
|
|
// iterate over types
|
|
module.children.forEach((type) => {
|
|
// check if type has annotation @validatable
|
|
if (typeof type.comment === 'object'
|
|
&& Array.isArray(type.comment.tags)
|
|
&& type.comment.tags.find((tag) => tag.tagName === 'validatable')) {
|
|
// add type to list
|
|
validatableTypes.push(type.name);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
return validatableTypes;
|
|
}
|