UNPKG

5.42 kBJavaScriptView Raw
1'use strict';
2var path = require('path');
3var dependentFilesUtils = require('./get-dependent-files');
4var change_1 = require('./change');
5var ast_tools_1 = require('@angular-cli/ast-tools');
6/**
7 * Rewrites import module of dependent files when the file is moved.
8 * Also, rewrites export module of related index file of the given file.
9 */
10var ModuleResolver = (function () {
11 function ModuleResolver(oldFilePath, newFilePath, rootPath) {
12 this.oldFilePath = oldFilePath;
13 this.newFilePath = newFilePath;
14 this.rootPath = rootPath;
15 }
16 /**
17 * Changes are applied from the bottom of a file to the top.
18 * An array of Change instances are sorted based upon the order,
19 * then apply() method is called sequentially.
20 *
21 * @param changes {Change []}
22 * @param host {Host}
23 * @return Promise after all apply() method of Change class is called
24 * to all Change instances sequentially.
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 * Assesses the import specifier and determines if it is a relative import.
34 *
35 * @return {boolean} boolean value if the import specifier is a relative import.
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 * Rewrites the import specifiers of all the dependent files (cases for no index file).
45 *
46 * @todo Implement the logic for rewriting imports of the dependent files when the file
47 * being moved has index file in its old path and/or in its new path.
48 *
49 * @return {Promise<Change[]>}
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 // Filter out the spec file associated with to-be-promoted component unit.
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 * Rewrites the file's own relative imports after it has been moved to new path.
86 *
87 * @return {Promise<Change[]>}
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}());
111exports.ModuleResolver = ModuleResolver;
112//# sourceMappingURL=/Users/hansl/Sources/angular-cli/packages/@angular/cli/utilities/module-resolver.js.map
\No newline at end of file