UNPKG

8.3 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [0, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36};
37Object.defineProperty(exports, "__esModule", { value: true });
38var path = require("path");
39var utils_1 = require("./utils");
40var FileHandler = /** @class */ (function () {
41 function FileHandler(filePath) {
42 this.filePath = filePath;
43 this.importModuleRE = /(?:import|export)\s*(?:(?:\{[^}]*\}|\*|\w+)(?:\s*as\s+\w+)?(?:\s+from)?\s*)?([`'"])((?:\\[\s\S]|(?!\1)[^\\])*?)\1/ig;
44 this.dirPath = path.dirname(filePath);
45 }
46 Object.defineProperty(FileHandler.prototype, "content", {
47 get: function () {
48 if (utils_1.isNil(this._content)) {
49 this._content = utils_1.readFile(this.filePath);
50 }
51 return this._content;
52 },
53 enumerable: true,
54 configurable: true
55 });
56 FileHandler.prototype.resolveToFile = function (validPath) {
57 if (utils_1.isDirectory(validPath)) {
58 validPath = path.join(validPath, 'index.ts');
59 }
60 else if (path.extname(validPath) !== '.ts') {
61 validPath += '.ts';
62 }
63 return validPath;
64 };
65 FileHandler.prototype.getModuleNameFromImportStatement = function (statement) {
66 var parts = statement.split('/');
67 var moduleName = parts.shift();
68 if (moduleName.charAt(0) === '@' && parts.length > 0) {
69 moduleName = moduleName + "/" + parts.shift(); // `;
70 }
71 return moduleName;
72 };
73 FileHandler.prototype.getImporteeFiles = function (excludeFrom) {
74 return __awaiter(this, void 0, void 0, function () {
75 var _this = this;
76 return __generator(this, function (_a) {
77 switch (_a.label) {
78 case 0: return [4 /*yield*/, this.getImporteeNames()
79 .map(function (moduleName) { return path.resolve(_this.dirPath, moduleName); })
80 .map(function (resolvedPath) { return _this.resolveToFile(resolvedPath); })
81 .filter(function (resolvedFilePath) { return utils_1.isFile(resolvedFilePath); })
82 .filter(function (resolvedFilePath) { return (utils_1.isEmpty(excludeFrom) || excludeFrom.indexOf(resolvedFilePath) < 0); })
83 .map(function (resolvedFilePath) { return new FileHandler(resolvedFilePath); })];
84 case 1: return [2 /*return*/, _a.sent()];
85 }
86 });
87 });
88 };
89 FileHandler.prototype.getExternalModules = function () {
90 var _this = this;
91 return this.getImporteeNames()
92 .map(function (statement) { return _this.getModuleNameFromImportStatement(statement); })
93 .filter(function (moduleName) { return moduleName.charAt(0) !== '.'; });
94 };
95 FileHandler.prototype.getImporteeNames = function () {
96 var allModules = [];
97 var importModuleMatchFileIndex = 2;
98 var importModuleMatch;
99 // Reset current index to 0
100 // RegEx.test moves this into 1
101 this.importModuleRE.lastIndex = 0;
102 var content = utils_1.removeComments(this.content);
103 importModuleMatch = this.importModuleRE.exec(content);
104 while (!utils_1.isNil(importModuleMatch)) {
105 /**
106 * Adds the file from match to result
107 * import * as x from './a-module'; => will get "./a-module"
108 */
109 allModules.push(importModuleMatch[importModuleMatchFileIndex]);
110 importModuleMatch = this.importModuleRE.exec(content);
111 }
112 return allModules;
113 };
114 FileHandler.prototype.setContent = function (content) {
115 this._content = content;
116 };
117 FileHandler.prototype.copyTo = function (destFilePath) {
118 var dirPath = path.dirname(destFilePath);
119 utils_1.ensureMakeDir(dirPath);
120 utils_1.writeFile(destFilePath, this.content);
121 };
122 FileHandler.prototype.hasImporters = function () {
123 this.importModuleRE.lastIndex = 0;
124 return this.importModuleRE.test(utils_1.removeComments(this.content));
125 };
126 FileHandler.prototype.getImportees = function (excludeFrom) {
127 return __awaiter(this, void 0, void 0, function () {
128 var importeeFiles, allImportees, _i, importeeFiles_1, file, currentFileImportees;
129 return __generator(this, function (_a) {
130 switch (_a.label) {
131 case 0: return [4 /*yield*/, this.getImporteeFiles(excludeFrom)];
132 case 1:
133 importeeFiles = _a.sent();
134 if (utils_1.isEmpty(importeeFiles)) {
135 return [2 /*return*/, []];
136 }
137 excludeFrom.push.apply(excludeFrom, importeeFiles.map(function (file) { return file.filePath; }));
138 allImportees = importeeFiles.slice();
139 _i = 0, importeeFiles_1 = importeeFiles;
140 _a.label = 2;
141 case 2:
142 if (!(_i < importeeFiles_1.length)) return [3 /*break*/, 5];
143 file = importeeFiles_1[_i];
144 return [4 /*yield*/, file.getImportees(excludeFrom)];
145 case 3:
146 currentFileImportees = _a.sent();
147 if (!utils_1.isEmpty(currentFileImportees)) {
148 allImportees.push.apply(allImportees, currentFileImportees);
149 excludeFrom.push.apply(excludeFrom, currentFileImportees.map(function (moduleFile) { return moduleFile.filePath; }));
150 }
151 _a.label = 4;
152 case 4:
153 _i++;
154 return [3 /*break*/, 2];
155 case 5: return [2 /*return*/, allImportees];
156 }
157 });
158 });
159 };
160 return FileHandler;
161}());
162exports.FileHandler = FileHandler;