1 | 'use strict';
|
2 | var path = require('path');
|
3 | var dependentFilesUtils = require('./get-dependent-files');
|
4 | var change_1 = require('./change');
|
5 | var ast_tools_1 = require('@angular-cli/ast-tools');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var ModuleResolver = (function () {
|
11 | function ModuleResolver(oldFilePath, newFilePath, rootPath) {
|
12 | this.oldFilePath = oldFilePath;
|
13 | this.newFilePath = newFilePath;
|
14 | this.rootPath = rootPath;
|
15 | }
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | ModuleResolver.prototype.applySortedChangePromise = function (changes, host) {
|
27 | if (host === void 0) { host = ast_tools_1.NodeHost; }
|
28 | return changes
|
29 | .sort(function (currentChange, nextChange) { return nextChange.order - currentChange.order; })
|
30 | .reduce(function (newChange, change) { return newChange.then(function () { return change.apply(host); }); }, Promise.resolve());
|
31 | };
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 | ModuleResolver.prototype.isRelativeImport = function (importClause) {
|
38 | var singleSlash = importClause.specifierText.charAt(0) === '/';
|
39 | var currentDirSyntax = importClause.specifierText.slice(0, 2) === './';
|
40 | var parentDirSyntax = importClause.specifierText.slice(0, 3) === '../';
|
41 | return singleSlash || currentDirSyntax || parentDirSyntax;
|
42 | };
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | ModuleResolver.prototype.resolveDependentFiles = function () {
|
52 | var _this = this;
|
53 | return dependentFilesUtils.getDependentFiles(this.oldFilePath, this.rootPath)
|
54 | .then(function (files) {
|
55 | var changes = [];
|
56 | var fileBaseName = path.basename(_this.oldFilePath, '.ts');
|
57 |
|
58 | var relavantFiles = Object.keys(files).filter(function (file) {
|
59 | if (path.extname(path.basename(file, '.ts')) === '.spec') {
|
60 | return path.basename(path.basename(file, '.ts'), '.spec') !== fileBaseName;
|
61 | }
|
62 | else {
|
63 | return true;
|
64 | }
|
65 | });
|
66 | relavantFiles.forEach(function (file) {
|
67 | var tempChanges = files[file]
|
68 | .map(function (specifier) {
|
69 | var componentName = path.basename(_this.oldFilePath, '.ts');
|
70 | var fileDir = path.dirname(file);
|
71 | var changeText = path.relative(fileDir, path.join(_this.newFilePath, componentName));
|
72 | if (changeText.length > 0 && changeText.charAt(0) !== '.') {
|
73 | changeText = "." + path.sep + changeText;
|
74 | }
|
75 | ;
|
76 | var position = specifier.end - specifier.specifierText.length;
|
77 | return new change_1.ReplaceChange(file, position - 1, specifier.specifierText, changeText);
|
78 | });
|
79 | changes = changes.concat(tempChanges);
|
80 | });
|
81 | return changes;
|
82 | });
|
83 | };
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 | ModuleResolver.prototype.resolveOwnImports = function () {
|
90 | var _this = this;
|
91 | return dependentFilesUtils.createTsSourceFile(this.oldFilePath)
|
92 | .then(function (tsFile) { return dependentFilesUtils.getImportClauses(tsFile); })
|
93 | .then(function (moduleSpecifiers) {
|
94 | var changes = moduleSpecifiers
|
95 | .filter(function (importClause) { return _this.isRelativeImport(importClause); })
|
96 | .map(function (specifier) {
|
97 | var specifierText = specifier.specifierText;
|
98 | var moduleAbsolutePath = path.resolve(path.dirname(_this.oldFilePath), specifierText);
|
99 | var changeText = path.relative(_this.newFilePath, moduleAbsolutePath);
|
100 | if (changeText.length > 0 && changeText.charAt(0) !== '.') {
|
101 | changeText = "." + path.sep + changeText;
|
102 | }
|
103 | var position = specifier.end - specifier.specifierText.length;
|
104 | return new change_1.ReplaceChange(_this.oldFilePath, position - 1, specifierText, changeText);
|
105 | });
|
106 | return changes;
|
107 | });
|
108 | };
|
109 | return ModuleResolver;
|
110 | }());
|
111 | exports.ModuleResolver = ModuleResolver;
|
112 |
|
\ | No newline at end of file |