UNPKG

@rxap/schematics-ts-morph

Version:

This package provides utilities for manipulating TypeScript code using ts-morph, particularly for Angular and NestJS projects. It offers functions to add, coerce, and modify code elements like classes, methods, decorators, and imports. The package also in

63 lines 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FixMissingImports = FixMissingImports; const ts_morph_1 = require("@rxap/ts-morph"); const path_1 = require("path"); /** * * @param basePath (optional) if definition all files from the base path are included and not only the changed or created files * @constructor */ function FixMissingImports(basePath) { return (tree) => { const project = (0, ts_morph_1.CreateProject)(); console.debug('fix missing imports for ts files'); function AddFiles(dir) { for (const file of dir.subfiles) { if (file.match(/\.ts$/) && !file.match(/\.spec\.ts$/)) { project.createSourceFile((0, path_1.join)(dir.path, file), dir.file(file).content.toString('utf-8'), { overwrite: true }); } } for (const subDir of dir.subdirs) { AddFiles(dir.dir(subDir)); } } for (const action of tree.actions.slice()) { switch (action.kind) { case 'c': case 'o': if (action.path.match(/\.ts$/)) { project.createSourceFile(action.path, action.content.toString('utf-8'), { overwrite: true }); } break; } } if (basePath) { AddFiles(tree.getDir(basePath)); for (const action of tree.actions.slice()) { switch (action.kind) { case 'c': case 'o': if (action.path.match(/\.ts$/)) { const sourceFile = project.getSourceFile(action.path); if (sourceFile) { sourceFile.fixMissingImports(); tree.overwrite(action.path, sourceFile.getFullText()); } else { console.warn(`Could not find the changed/created file '${action.path}'.`); } } break; } } } else { project.getSourceFiles().forEach(sourceFile => { sourceFile.fixMissingImports(); tree.overwrite(sourceFile.getFilePath(), sourceFile.getFullText()); }); } }; } //# sourceMappingURL=fix-missing-imports.js.map