UNPKG

@nestia/sdk

Version:

Nestia SDK and Swagger generator

103 lines 4.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ImportDictionary = void 0; const path_1 = __importDefault(require("path")); const tstl_1 = require("tstl"); const typescript_1 = __importDefault(require("typescript")); const FilePrinter_1 = require("./FilePrinter"); class ImportDictionary { constructor(file) { this.file = file; this.components_ = new tstl_1.HashMap(); } empty() { return this.components_.empty(); } external(props) { var _a; const composition = this.components_.take(new tstl_1.Pair(`node_modules/${props.library}`, props.type), () => ({ location: `node_modules/${props.library}`, elements: new tstl_1.HashSet(), default: false, type: props.type, })); if (props.instance === null) composition.default = true; else composition.elements.insert(props.instance); return (_a = props.instance) !== null && _a !== void 0 ? _a : props.library; } internal(props) { var _a; const file = (() => { if (props.file.substring(props.file.length - 5) === ".d.ts") return props.file.substring(0, props.file.length - 5); else if (props.file.substring(props.file.length - 3) === ".ts") return props.file.substring(0, props.file.length - 3); return props.file; })(); const composition = this.components_.take(new tstl_1.Pair(file, props.type), () => ({ location: file, elements: new tstl_1.HashSet(), default: false, type: props.type, })); if (props.instance === null) { composition.default = true; if (props.name) composition.name = props.name; } else composition.elements.insert(props.instance); return (_a = props.instance) !== null && _a !== void 0 ? _a : file; } toStatements(outDir) { const external = []; const internal = []; const locator = (str) => { const location = path_1.default.relative(outDir, str).split("\\").join("/"); const index = location.lastIndexOf(NODE_MODULES); return index === -1 ? location.startsWith("..") ? location : `./${location}` : location.substring(index + NODE_MODULES.length); }; const enroll = (filter) => (container) => { var _a, _b; const compositions = this.components_ .toJSON() .filter((c) => filter(c.second.location)) .map((e) => (Object.assign(Object.assign({}, e.second), { location: locator(e.second.location) }))) .sort((a, b) => a.location.localeCompare(b.location)); for (const c of compositions) { const brackets = []; if (c.default) brackets.push((_a = c.name) !== null && _a !== void 0 ? _a : c.location); if (c.elements.empty() === false) brackets.push(`{ ${c.elements .toJSON() .sort((a, b) => a.localeCompare(b)) .join(", ")} }`); container.push(typescript_1.default.factory.createImportDeclaration(undefined, typescript_1.default.factory.createImportClause(c.type, c.default ? typescript_1.default.factory.createIdentifier((_b = c.name) !== null && _b !== void 0 ? _b : c.location) : undefined, c.elements.empty() === false ? typescript_1.default.factory.createNamedImports([...c.elements].map((elem) => typescript_1.default.factory.createImportSpecifier(false, undefined, typescript_1.default.factory.createIdentifier(elem)))) : undefined), typescript_1.default.factory.createStringLiteral(c.location))); } }; enroll((str) => str.indexOf(NODE_MODULES) !== -1)(external); enroll((str) => str.indexOf(NODE_MODULES) === -1)(internal); return [ ...external, ...(external.length && internal.length ? [FilePrinter_1.FilePrinter.enter()] : []), ...internal, ]; } } exports.ImportDictionary = ImportDictionary; const NODE_MODULES = "node_modules/"; //# sourceMappingURL=ImportDictionary.js.map