1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs = require("fs");
|
4 | const ts = require("typescript");
|
5 | const glob = require("glob");
|
6 | const path = require("path");
|
7 | const denodeify = require("denodeify");
|
8 | const readFile = denodeify(fs.readFile);
|
9 | const globSearch = denodeify(glob);
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function createTsSourceFile(fileName) {
|
17 | return readFile(fileName, 'utf8')
|
18 | .then((contents) => {
|
19 | return ts.createSourceFile(fileName, contents, ts.ScriptTarget.Latest, true);
|
20 | });
|
21 | }
|
22 | exports.createTsSourceFile = createTsSourceFile;
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function getImportClauses(node) {
|
33 | return node.statements
|
34 | .filter(node => node.kind === ts.SyntaxKind.ImportDeclaration)
|
35 | .map((node) => {
|
36 | let moduleSpecifier = node.moduleSpecifier;
|
37 | return {
|
38 | specifierText: moduleSpecifier.getText().slice(1, -1),
|
39 | pos: moduleSpecifier.pos,
|
40 | end: moduleSpecifier.end
|
41 | };
|
42 | });
|
43 | }
|
44 | exports.getImportClauses = getImportClauses;
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function hasIndexFile(dirPath) {
|
54 | return globSearch(path.join(dirPath, 'index.ts'), { nodir: true })
|
55 | .then((indexFile) => {
|
56 | return indexFile.length > 0;
|
57 | });
|
58 | }
|
59 | exports.hasIndexFile = hasIndexFile;
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | function getAllAssociatedFiles(fileName) {
|
77 | let fileDirName = path.dirname(fileName);
|
78 | let componentName = path.basename(fileName, '.ts');
|
79 | return globSearch(path.join(fileDirName, `${componentName}.*`), { nodir: true })
|
80 | .then((files) => {
|
81 | return files.filter((file) => {
|
82 | return (path.basename(file) !== 'index.ts');
|
83 | });
|
84 | });
|
85 | }
|
86 | exports.getAllAssociatedFiles = getAllAssociatedFiles;
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | function getDependentFiles(fileName, rootPath) {
|
98 | return globSearch(path.join(rootPath, '**/*.ts'), { nodir: true })
|
99 | .then((files) => Promise.all(files.map(file => createTsSourceFile(file)))
|
100 | .then((tsFiles) => tsFiles.map(file => getImportClauses(file)))
|
101 | .then((moduleSpecifiers) => {
|
102 | let allFiles = {};
|
103 | files.forEach((file, index) => {
|
104 | let sourcePath = path.normalize(file);
|
105 | allFiles[sourcePath] = moduleSpecifiers[index];
|
106 | });
|
107 | return allFiles;
|
108 | })
|
109 | .then((allFiles) => {
|
110 | let relevantFiles = {};
|
111 | Object.keys(allFiles).forEach(filePath => {
|
112 | const tempModuleSpecifiers = allFiles[filePath]
|
113 | .filter(importClause => {
|
114 |
|
115 | let singleSlash = importClause.specifierText.charAt(0) === '/';
|
116 | let currentDirSyntax = importClause.specifierText.slice(0, 2) === './';
|
117 | let parentDirSyntax = importClause.specifierText.slice(0, 3) === '../';
|
118 | return singleSlash || currentDirSyntax || parentDirSyntax;
|
119 | })
|
120 | .filter(importClause => {
|
121 | let modulePath = path.resolve(path.dirname(filePath), importClause.specifierText);
|
122 | let resolvedFileName = path.resolve(fileName);
|
123 | let fileBaseName = path.basename(resolvedFileName, '.ts');
|
124 | let parsedFilePath = path.join(path.dirname(resolvedFileName), fileBaseName);
|
125 | return (parsedFilePath === modulePath) || (resolvedFileName === modulePath);
|
126 | });
|
127 | if (tempModuleSpecifiers.length > 0) {
|
128 | relevantFiles[filePath] = tempModuleSpecifiers;
|
129 | }
|
130 | ;
|
131 | });
|
132 | return relevantFiles;
|
133 | }));
|
134 | }
|
135 | exports.getDependentFiles = getDependentFiles;
|
136 |
|
\ | No newline at end of file |