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