|
1 | | -import FileReport = vueComponentAnalyzer.FileReport; |
| 1 | +import {FileReport} from '../../types'; |
2 | 2 | import {resolveFile} from './utils'; |
3 | 3 | import {readFileSync, statSync} from 'fs'; |
4 | 4 | import {resolve, extname} from 'path'; |
5 | | -import {FileCounter} from './FileCounter'; |
| 5 | +import {fileCounter} from './FileCounter'; |
6 | 6 | import {VueComponent} from './VueComponent'; |
7 | 7 | import {model} from './Model'; |
8 | 8 | const cwd = process.cwd(); |
9 | 9 |
|
10 | | -class Analyzer { |
11 | | - private readonly _counter: FileCounter; |
| 10 | +/** |
| 11 | + * get import tree. |
| 12 | + * @param {string} fileName entry file name |
| 13 | + * @param {string[]} parents entries of file name |
| 14 | + * @param {boolean} isTest whether it through the test. if this doesn't exist lastModifiedTime will always fail on the GitHub Actions. |
| 15 | + */ |
| 16 | +export const getImportDeclarationTree = (fileName: string, parents: string[] = [], isTest = false): FileReport => { |
| 17 | + const filename = resolve(cwd, fileName); |
| 18 | + // get filename without current working directory. |
| 19 | + const shortFilename = filename.replace(cwd, ''); |
| 20 | + // get file statistic |
| 21 | + const stat = statSync(filename); |
| 22 | + // make a filename list for detecting circular dependency. |
| 23 | + const ancestorList: string[] = parents.concat(); |
12 | 24 |
|
13 | | - constructor() { |
14 | | - this._counter = new FileCounter(); |
15 | | - } |
16 | | - |
17 | | - /** |
18 | | - * get import tree. |
19 | | - * @param {string} fileName entry file name |
20 | | - * @param {string[]} parents entries of file name |
21 | | - * @param {boolean} isTest whether it through the test. if this doesn't exist lastModifiedTime will always fail on the GitHub Actions. |
22 | | - */ |
23 | | - public getImportDeclarationTree = (fileName: string, parents: string[], isTest = false): FileReport => { |
24 | | - const filename = resolve(cwd, fileName); |
25 | | - // get filename without current working directory. |
26 | | - const shortFilename = filename.replace(cwd, ''); |
27 | | - // get file statistic |
28 | | - const stat = statSync(filename); |
29 | | - // make a filename list for detecting circular dependency. |
30 | | - const ancestorList: string[] = parents.concat(); |
31 | | - |
32 | | - ancestorList.push(fileName); |
| 25 | + ancestorList.push(fileName); |
33 | 26 |
|
34 | | - if (!model.isSilentMode) { |
35 | | - console.log(`read ${filename}`); |
36 | | - } |
| 27 | + if (!model.isSilentMode) { |
| 28 | + console.log(`read ${filename}`); |
| 29 | + } |
37 | 30 |
|
38 | | - // increment count of this file. |
39 | | - this._counter.add(shortFilename); |
| 31 | + // increment count of this file. |
| 32 | + fileCounter.add(shortFilename); |
40 | 33 |
|
41 | | - // if this file is not Vue Component file, return only filename and stat. |
42 | | - if (extname(filename) === '' || extname(filename) !== '.vue') { |
43 | | - return { |
44 | | - name: shortFilename, |
45 | | - props: '', |
46 | | - size: stat.size, |
47 | | - lastModifiedTime: isTest ? 0 : Number(stat.mtimeMs.toFixed(0)), |
48 | | - children: [], |
49 | | - }; |
50 | | - } |
| 34 | + // if this file is not Vue Component file, return only filename and stat. |
| 35 | + if (extname(filename) === '' || extname(filename) !== '.vue') { |
| 36 | + return { |
| 37 | + name: shortFilename, |
| 38 | + props: '', |
| 39 | + size: stat.size, |
| 40 | + lastModifiedTime: isTest ? 0 : Number(stat.mtimeMs.toFixed(0)), |
| 41 | + children: [], |
| 42 | + }; |
| 43 | + } |
51 | 44 |
|
52 | | - const contents = readFileSync(filename, 'utf-8'); |
53 | | - const component = new VueComponent(shortFilename, contents, stat); |
| 45 | + const contents = readFileSync(filename, 'utf-8'); |
| 46 | + const component = new VueComponent(shortFilename, contents, stat); |
54 | 47 |
|
55 | | - try { |
56 | | - // if we get, read imported file recursive. |
57 | | - for (let i = 0, len = component.importDeclaration.length; i < len; i++) { |
58 | | - // TODO: support other types? (value might be RegExp, or number, boolean.) |
59 | | - const source = String(component.importDeclaration[i].source.value); |
| 48 | + try { |
| 49 | + // if we get, read imported file recursive. |
| 50 | + for (let i = 0, len = component.importDeclaration.length; i < len; i++) { |
| 51 | + // TODO: support other types? (value might be RegExp, or number, boolean.) |
| 52 | + const source = String(component.importDeclaration[i].source.value); |
60 | 53 |
|
61 | | - if (source) { |
62 | | - const nextFilename = resolveFile(source, filename); |
| 54 | + if (source) { |
| 55 | + const nextFilename = resolveFile(source, filename); |
63 | 56 |
|
64 | | - if (nextFilename) { |
65 | | - if (parents.includes(nextFilename)) { |
66 | | - console.warn(`Circular dependency detected between ${nextFilename} and ${filename}`); |
67 | | - } else { |
68 | | - component.addChildReport(this.getImportDeclarationTree(nextFilename, ancestorList, isTest)); |
69 | | - } |
| 57 | + if (nextFilename) { |
| 58 | + if (parents.includes(nextFilename)) { |
| 59 | + console.warn(`Circular dependency detected between ${nextFilename} and ${filename}`); |
| 60 | + } else { |
| 61 | + component.addChildReport(getImportDeclarationTree(nextFilename, ancestorList, isTest)); |
70 | 62 | } |
71 | 63 | } |
72 | 64 | } |
73 | | - } catch (err) { |
74 | | - console.error(`Something went wrong with reading ${filename}`); |
75 | | - if (err instanceof Error) { |
76 | | - console.error(err.message); |
77 | | - } |
78 | 65 | } |
79 | | - |
80 | | - return component.getFileReport(isTest); |
81 | | - }; |
82 | | - |
83 | | - get counter(): FileCounter { |
84 | | - return this._counter; |
| 66 | + } catch (err) { |
| 67 | + console.error(`Something went wrong with reading ${filename}`); |
| 68 | + if (err instanceof Error) { |
| 69 | + console.error(err.message); |
| 70 | + } |
85 | 71 | } |
86 | | -} |
87 | 72 |
|
88 | | -export const analyzer = new Analyzer(); |
| 73 | + return component.getFileReport(isTest); |
| 74 | +}; |
0 commit comments