@nestia/sdk
Version:
Nestia SDK and Swagger generator
123 lines • 5.52 kB
JavaScript
;
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 ImportAnalyzer_1 = require("../../analyses/ImportAnalyzer");
const FilePrinter_1 = require("./FilePrinter");
class ImportDictionary {
constructor(file) {
this.file = file;
this.components_ = new tstl_1.HashMap((key) => (0, tstl_1.hash)(key.file, key.declaration, key.asterisk, key.default), (a, b) => a.file === b.file &&
a.declaration === b.declaration &&
a.asterisk === b.asterisk &&
a.default === b.default);
}
empty() {
return this.components_.empty();
}
declarations(imports) {
imports = ImportAnalyzer_1.ImportAnalyzer.merge(imports);
for (const imp of imports) {
if (imp.asterisk !== null)
this.internal({
type: "asterisk",
file: imp.file,
name: imp.asterisk,
declaration: true,
});
if (imp.default !== null)
this.internal({
type: "default",
file: imp.file,
name: imp.default,
declaration: true,
});
for (const elem of imp.elements)
this.internal({
type: "element",
file: imp.file,
name: elem,
declaration: true,
});
}
}
external(props) {
const file = `node_modules/${props.file}`;
return this.internal(Object.assign(Object.assign({}, props), { file }));
}
internal(props) {
const file = normalize((() => {
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 key = {
file: file,
declaration: props.declaration,
asterisk: props.type === "asterisk" ? props.name : null,
default: props.type === "default" ? props.name : null,
};
const value = this.components_.take(key, () => (Object.assign(Object.assign({}, key), { elements: new tstl_1.TreeSet() })));
if (props.type === "element")
value.elements.insert(props.name);
return props.name;
}
toStatements(outDir) {
outDir = path_1.default.resolve(outDir);
const external = [];
const internal = [];
const locator = (str) => {
const location = path_1.default
.relative(outDir, str)
.split(path_1.default.sep)
.join("/");
const index = location.lastIndexOf(NODE_MODULES);
return index === -1
? location.startsWith("..")
? location
: `./${location}`
: location.substring(index + NODE_MODULES.length);
};
const enroll = (filter) => (container) => {
const compositions = this.components_
.toJSON()
.filter((c) => filter(c.second.file))
.map((e) => (Object.assign(Object.assign({}, e.second), { file: locator(e.second.file) })))
.sort((a, b) => a.file.localeCompare(b.file));
for (const c of compositions)
container.push(typescript_1.default.factory.createImportDeclaration(undefined, this.toImportClaude(c), typescript_1.default.factory.createStringLiteral(c.file), undefined));
};
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,
];
}
toImportClaude(c) {
if (c.asterisk !== null)
return typescript_1.default.factory.createImportClause(c.declaration, undefined, typescript_1.default.factory.createNamespaceImport(typescript_1.default.factory.createIdentifier(c.asterisk)));
return typescript_1.default.factory.createImportClause(c.declaration, c.default !== null ? typescript_1.default.factory.createIdentifier(c.default) : undefined, c.elements.size() !== 0
? typescript_1.default.factory.createNamedImports(Array.from(c.elements).map((elem) => typescript_1.default.factory.createImportSpecifier(false, undefined, typescript_1.default.factory.createIdentifier(elem))))
: undefined);
}
}
exports.ImportDictionary = ImportDictionary;
const NODE_MODULES = "node_modules/";
const normalize = (file) => {
file = path_1.default.resolve(file);
if (file.includes(`node_modules${path_1.default.sep}`))
file =
"node_modules/" +
file.split(`node_modules${path_1.default.sep}`).at(-1).split(path_1.default.sep).join("/");
return file;
};
//# sourceMappingURL=ImportDictionary.js.map