1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ModuleImportDeclarator = void 0;
|
4 | const core_1 = require("@angular-devkit/core");
|
5 | const path_solver_1 = require("./path.solver");
|
6 | class ModuleImportDeclarator {
|
7 | constructor(solver = new path_solver_1.PathSolver()) {
|
8 | this.solver = solver;
|
9 | }
|
10 | declare(content, options) {
|
11 | const toInsert = this.buildLineToInsert(options);
|
12 | const contentLines = content.split('\n');
|
13 | const finalImportIndex = this.findImportsEndpoint(contentLines);
|
14 | contentLines.splice(finalImportIndex + 1, 0, toInsert);
|
15 | return contentLines.join('\n');
|
16 | }
|
17 | findImportsEndpoint(contentLines) {
|
18 | const reversedContent = Array.from(contentLines).reverse();
|
19 | const reverseImports = reversedContent.filter(line => line.match(/\} from ('|")/));
|
20 | if (reverseImports.length <= 0) {
|
21 | return 0;
|
22 | }
|
23 | return contentLines.indexOf(reverseImports[0]);
|
24 | }
|
25 | buildLineToInsert(options) {
|
26 | return `import { ${options.symbol} } from '${this.computeRelativePath(options)}';`;
|
27 | }
|
28 | computeRelativePath(options) {
|
29 | let importModulePath;
|
30 | if (options.type !== undefined) {
|
31 | importModulePath = (0, core_1.normalize)(`/${options.path}/${options.name}.${options.type}`);
|
32 | }
|
33 | else {
|
34 | importModulePath = (0, core_1.normalize)(`/${options.path}/${options.name}`);
|
35 | }
|
36 | return this.solver.relative(options.module, importModulePath);
|
37 | }
|
38 | }
|
39 | exports.ModuleImportDeclarator = ModuleImportDeclarator;
|