UNPKG

@ng-doc/builder

Version:

<!-- PROJECT LOGO --> <br /> <div align="center"> <a href="https://github.com/ng-doc/ng-doc"> <img src="https://ng-doc.com/assets/images/ng-doc.svg?raw=true" alt="Logo" height="150px"> </a>

111 lines 4.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.migrate = migrate; const tslib_1 = require("tslib"); const ng_morph_1 = require("ng-morph"); const path = tslib_1.__importStar(require("path")); /** * Moves all dependencies from `ng-doc.dependencies.ts` to `ng-doc.page.ts` and removes the `ng-doc.dependencies.ts` file. * @param options */ function migrate(options) { return (tree) => { (0, ng_morph_1.setActiveProject)((0, ng_morph_1.createProject)(tree, options.path, ['**/**/ng-doc.dependencies.ts', '**/**/ng-doc.page.ts'])); const sourceFiles = (0, ng_morph_1.getSourceFiles)('**/**/ng-doc.dependencies.ts'); for (const sourceFile of sourceFiles) { const pageSourceFile = (0, ng_morph_1.getSourceFiles)(path.join(sourceFile.getDirectoryPath(), 'ng-doc.page.ts'))[0]; if (!pageSourceFile) { continue; } const objectExpression = getObjectExpressionFromDefault(sourceFile); const pageObjectExpression = getObjectExpressionFromDefault(pageSourceFile); if (!objectExpression || !pageObjectExpression) { continue; } migrateProperty(objectExpression, pageObjectExpression, 'module', 'imports', (initializer) => `[${initializer}]`); migrateProperty(objectExpression, pageObjectExpression, 'demo', 'demos'); migrateProperty(objectExpression, pageObjectExpression, 'playgrounds'); sourceFile.delete(); } (0, ng_morph_1.saveActiveProject)(); }; } /** * * @param sourceFile */ function getObjectExpressionFromDefault(sourceFile) { const defaultExport = sourceFile .getExportedDeclarations() ?.get('default')?.[0]; if (ng_morph_1.Node.isVariableDeclaration(defaultExport)) { return defaultExport?.getFirstChildByKindOrThrow(ng_morph_1.SyntaxKind.ObjectLiteralExpression); } return undefined; } /** * * @param original * @param destination * @param propertyName * @param newPropertyName * @param initializer */ function migrateProperty(original, destination, propertyName, newPropertyName = propertyName, initializer = (initializer) => initializer) { const sourceFile = original.getSourceFile(); const property = original.getProperty(propertyName); sourceFile.getProject().manipulationSettings.set({ quoteKind: ng_morph_1.QuoteKind.Single, }); if (property) { if (ng_morph_1.Node.isPropertyAssignment(property) || ng_morph_1.Node.isShorthandPropertyAssignment(property)) { if (ng_morph_1.Node.isPropertyAssignment(property)) { const structure = property.getStructure(); destination.addProperty({ ...structure, name: newPropertyName, initializer: initializer(structure.initializer), }); } if (ng_morph_1.Node.isShorthandPropertyAssignment(property)) { const structure = property.getStructure(); destination.addProperty({ ...structure, name: newPropertyName, }); } const imports = sourceFile .getImportDeclarations() .filter((importDeclaration) => { return (importDeclaration.getNamedImports().filter((importSpecifier) => { return (importSpecifier .getNameNode() .findReferencesAsNodes() .filter((node) => { return isChildOf(node, property); }).length > 0); }).length > 0); }) .flat(); destination .getSourceFile() .addImportDeclarations(imports.map((importDeclaration) => importDeclaration.getStructure())); } } } /** * * @param node * @param parent */ function isChildOf(node, parent) { if (node === parent) { return true; } const p = node.getParent(); if (p) { return isChildOf(p, parent); } return false; } //# sourceMappingURL=index.js.map