schema-dts-gen
Version:
Generate TypeScript Definitions for Schema.org Schema
79 lines • 3.78 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import ts from 'typescript';
const { createPrinter, createSourceFile, EmitHint, NewLineKind, ScriptKind, ScriptTarget, } = ts;
import { asTopicArray } from '../triples/operators.js';
import { Sort } from '../ts/class.js';
import { ProcessClasses } from './toClass.js';
import { ProcessEnums } from './toEnum.js';
import { ProcessProperties } from './toProperty.js';
import { HelperTypes } from '../ts/helper_types.js';
/**
* Writes TypeScript declarations for all Classes, Typedefs, and Enums
* representing the ontology passed in the 'triples' parameter.
*
* @param triples Observable emitting all Triples found in an ontology.
* @param includeDeprecated True if classes and properties marked with
* 'supersededBy' should still be included (as 'deprecated') in the final
* TypeScript output.
* @param context Context for the transformation.
* @param write Callback function to write a portion of the file. Will be called
* sequentially on separate chunks within a file.
*
* @returns Promise indicating completion.
*/
export function WriteDeclarations(graph, includeDeprecated, context, write) {
return __awaiter(this, void 0, void 0, function* () {
const topics = asTopicArray(graph);
const classes = ProcessClasses(topics);
ProcessProperties(topics, classes);
ProcessEnums(topics, classes);
const sorted = Array.from(classes.values()).sort(Sort);
const properties = {
hasRole: !!(classes.get('http://schema.org/Role') ||
classes.get('https://schema.org/Role')),
skipDeprecatedProperties: !includeDeprecated,
};
const source = createSourceFile('result.ts', '', ScriptTarget.ES2015,
/*setParentNodes=*/ false, ScriptKind.TS);
const printer = createPrinter({ newLine: NewLineKind.LineFeed });
for (const helperType of HelperTypes(context, properties)) {
yield write(printer.printNode(EmitHint.Unspecified, helperType, source));
yield write('\n');
}
yield write('\n');
for (const cls of sorted) {
if (cls.deprecated && !includeDeprecated)
continue;
for (const node of cls.toNode(context, properties)) {
const result = printer.printNode(EmitHint.Unspecified, node, source);
yield write(result);
yield write('\n');
}
yield write('\n');
}
});
}
//# sourceMappingURL=transform.js.map