UNPKG

@odata2ts/odata2ts

Version:

Flexible generator to produce various TypeScript artefacts (from simple model interfaces to complete odata clients) from OData metadata files

77 lines 3.08 kB
import { __awaiter } from "tslib"; import { access, readFile } from "node:fs/promises"; import { mkdirp } from "mkdirp"; import { rimraf } from "rimraf"; import { parseStringPromise } from "xml2js"; import { runApp } from "../app.js"; import { downloadMetadata, storeMetadata } from "../download/index.js"; import { Modes } from "../OptionModel.js"; export function startServiceGenerationRun(options) { return __awaiter(this, void 0, void 0, function* () { const { source, output, sourceUrl, refreshFile, sourceUrlConfig, debug, mode, emitMode, prettier, serviceName } = options; console.log("---------------------------"); console.log(`Starting generation process. Service name ${serviceName ? `"${serviceName}"` : "will be detected automatically!"}`); if (debug) { console.log("Resolved config:", { source, output, sourceUrl, refreshFile, debug, mode: Modes[mode], emitMode, prettier, serviceName, }); } // evaluate source const exists = yield access(source) .then(() => true) .catch(() => false); console.log(`${exists ? "Found" : "Didn't find"} metadata file at: `, source); let metadataXml; // download metadata and store on disk if (sourceUrl && (!exists || refreshFile)) { try { metadataXml = yield downloadMetadata(sourceUrl, sourceUrlConfig, debug); } catch (e) { console.error(`Failed to load metadata! Message: ${e === null || e === void 0 ? void 0 : e.message}`); process.exit(10); } metadataXml = yield storeMetadata(source, metadataXml, prettier); } // otherwise file must exist else if (!exists) { console.error(`Input source [${source}] doesn't exist!`); process.exit(2); } // read the metadata from file else { console.log("Reading metadata from file:", source); metadataXml = yield readFile(source); } const metadataJson = (yield parseStringPromise(metadataXml)); // TODO find out if "1.0" and "4.0" are really correct // TODO exit here if no version not suitable version was detected // console.log(`OData version detected: ${metadataJson["edmx:Edmx"].$.Version}`); // ensure that output directory exists try { yield rimraf(output); yield mkdirp(output); } catch (error) { console.error(`Output path [${output}] couldn't be created!`, error); process.exit(3); } // run the app try { yield runApp(metadataJson, options); } catch (err) { console.error("Error while running the program", err); process.exit(99); } }); } //# sourceMappingURL=serviceGenerationRun.js.map