1 | import * as path from 'path';
|
2 |
|
3 | const glob = require('glob');
|
4 |
|
5 | export class ParserUtil {
|
6 | private _files;
|
7 | private _cwd;
|
8 | private _globFiles = [];
|
9 |
|
10 | public init(exclude: string[], cwd: string) {
|
11 | this._files = exclude;
|
12 | this._cwd = cwd;
|
13 | let i = 0;
|
14 | let len = exclude.length;
|
15 |
|
16 | for (i; i < len; i++) {
|
17 | this._globFiles = [...this._globFiles, ...glob.sync(exclude[i], { cwd: this._cwd })];
|
18 | }
|
19 | }
|
20 |
|
21 | public testFilesWithCwdDepth() {
|
22 | let i = 0;
|
23 | let len = this._files.length;
|
24 | let result = {
|
25 | status: true,
|
26 | level: 0
|
27 | };
|
28 | for (i; i < len; i++) {
|
29 | let elementPath = path.resolve(this._cwd + path.sep, this._files[i]);
|
30 | if (elementPath.indexOf(this._cwd) === -1) {
|
31 | result.status = false;
|
32 | let level = this._files[i].match(/\..\//g).length;
|
33 | if (level > result.level) {
|
34 | result.level = level;
|
35 | }
|
36 | }
|
37 | }
|
38 | return result;
|
39 | }
|
40 |
|
41 | public updateCwd(cwd, level) {
|
42 | let _cwd = cwd,
|
43 | _rewind = '';
|
44 | for (let i = 0; i < level; i++) {
|
45 | _rewind += '../';
|
46 | }
|
47 | _cwd = path.resolve(_cwd, _rewind);
|
48 | return _cwd;
|
49 | }
|
50 |
|
51 | public testFile(file: string): boolean {
|
52 | let i = 0;
|
53 | let len = this._files.length;
|
54 | let fileBasename = path.basename(file);
|
55 | let fileNameInCwd = file.replace(this._cwd + path.sep, '');
|
56 | let result = false;
|
57 |
|
58 | if (path.sep === '\\') {
|
59 | fileNameInCwd = fileNameInCwd.replace(new RegExp('\\' + path.sep, 'g'), '/');
|
60 | }
|
61 |
|
62 | for (i; i < len; i++) {
|
63 | if (glob.hasMagic(this._files[i]) && this._globFiles.length > 0) {
|
64 | let resultGlobSearch = this._globFiles.findIndex(element => {
|
65 | let elementPath = path.resolve(this._cwd + path.sep, element);
|
66 | let elementPathInCwd = elementPath.replace(this._cwd + path.sep, '');
|
67 | elementPathInCwd = elementPathInCwd.replace(
|
68 | new RegExp('\\' + path.sep, 'g'),
|
69 | '/'
|
70 | );
|
71 | return elementPathInCwd === fileNameInCwd;
|
72 | });
|
73 | result = resultGlobSearch !== -1;
|
74 | } else {
|
75 | result = fileNameInCwd === this._files[i];
|
76 | }
|
77 | if (result) {
|
78 | break;
|
79 | }
|
80 | }
|
81 | return result;
|
82 | }
|
83 | }
|