@cosmology/ast
Version:
Cosmos TypeScript AST generation
77 lines (76 loc) • 3.67 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.importNamespace = exports.recursiveOtherNameSpaces = exports.recursiveModuleBundle = void 0;
const path_1 = require("path");
const t = __importStar(require("@babel/types"));
const recursiveModuleBundle = (options, obj) => {
return Object.keys(obj).map(key => {
if (obj[key]?.__export) {
// get exported parts inside the package.
const properties = Object.keys(obj[key])
.filter(a => a !== '__export')
.filter(a => a.startsWith('_'))
.map(a => t.spreadElement(t.identifier(a)));
// see if there's recursive package inside.
const others = Object.keys(obj[key])
.filter(a => a !== '__export')
.filter(a => !a.startsWith('_')).map(otherKey => ({ key: otherKey, value: obj[key][otherKey] }));
if (others.length) {
// export recursive package through object properties
properties.push(...(0, exports.recursiveOtherNameSpaces)(others));
}
const nmspc = t.variableDeclaration('const', [t.variableDeclarator(t.identifier(key), t.objectExpression(properties))]);
// return nmspc;
return t.exportNamedDeclaration(nmspc, []);
}
else {
// you can make a namespace for obj[key]
// e.g. libs
return t.exportNamedDeclaration(t.tsModuleDeclaration(t.identifier(key), t.tsModuleBlock((0, exports.recursiveModuleBundle)(options, obj[key]))));
}
});
};
exports.recursiveModuleBundle = recursiveModuleBundle;
const recursiveOtherNameSpaces = (objs) => {
return objs.map(obj => {
const properties = Object.keys(obj.value)
.filter(a => a !== '__export')
.filter(a => a.startsWith('_'))
.map(a => t.spreadElement(t.identifier(a)));
const others = Object.keys(obj.value)
.filter(a => a !== '__export')
.filter(a => !a.startsWith('_')).map(otherKey => ({ key: otherKey, value: obj.value[otherKey] }));
if (others.length) {
properties.push(...(0, exports.recursiveOtherNameSpaces)(others));
}
return t.objectProperty(t.identifier(obj.key), t.objectExpression(properties));
});
};
exports.recursiveOtherNameSpaces = recursiveOtherNameSpaces;
const importNamespace = (ident, path) => t.importDeclaration([
t.importNamespaceSpecifier(t.identifier(ident))
], t.stringLiteral(path.replace((0, path_1.extname)(path), '')));
exports.importNamespace = importNamespace;
;