1
1
import fs from 'fs' ;
2
2
import path from 'path' ;
3
- import { glob } from 'glob' ;
4
3
import minimatch from 'minimatch' ;
4
+ import pkg from 'glob' ;
5
+ const { glob } = pkg ;
5
6
6
7
export interface FileData {
7
8
path : string ;
8
9
contents : string ;
9
10
}
10
11
11
12
const DEFAULT_PATTERNS = [
12
- '**/*.ts' , '**/*.tsx' , '**/*.js' , '**/*.jsx' ,
13
- '**/package.json' , '**/tsconfig*.json' , '**/*.config.*'
13
+ '**/*.ts' ,
14
+ '**/*.tsx' ,
15
+ '**/*.js' ,
16
+ '**/*.jsx' ,
17
+ '**/package.json' ,
18
+ '**/tsconfig*.json' ,
19
+ '**/*.config.*' ,
14
20
] ;
15
21
16
22
const DEFAULT_IGNORE = [
@@ -22,28 +28,28 @@ const DEFAULT_IGNORE = [
22
28
'.next/**' ,
23
29
'coverage/**' ,
24
30
'**/__tests__/**' ,
25
- '**/.env*'
31
+ '**/.env*' ,
26
32
] ;
27
33
28
34
export async function collectFiles (
29
35
basePath : string = process . cwd ( ) ,
30
36
patterns : string [ ] = DEFAULT_PATTERNS ,
31
- ignore : string [ ] = DEFAULT_IGNORE
37
+ ignore : string [ ] = DEFAULT_IGNORE ,
32
38
) : Promise < FileData [ ] > {
33
39
const originalCwd = process . cwd ( ) ;
34
-
40
+
35
41
try {
36
42
process . chdir ( basePath ) ;
37
-
43
+
38
44
const allFilePaths : string [ ] = [ ] ;
39
45
for ( const pattern of patterns ) {
40
46
const globResult = glob . sync ( pattern , { nodir : true } ) ;
41
- const filtered = globResult . filter ( ( match : string ) =>
42
- ! ignore . some ( ignorePattern => minimatch ( match , ignorePattern ) )
47
+ const filtered = globResult . filter (
48
+ ( match : string ) => ! ignore . some ( ( ignorePattern ) => minimatch ( match , ignorePattern ) ) ,
43
49
) ;
44
50
allFilePaths . push ( ...filtered ) ;
45
51
}
46
-
52
+
47
53
const filePaths = [ ...new Set ( allFilePaths ) ] ;
48
54
49
55
const files : FileData [ ] = [ ] ;
@@ -52,9 +58,9 @@ export async function collectFiles(
52
58
try {
53
59
const contents = fs . readFileSync ( path . resolve ( basePath , filePath ) , 'utf8' ) ;
54
60
if ( contents . trim ( ) ) {
55
- files . push ( {
56
- path : filePath ,
57
- contents
61
+ files . push ( {
62
+ path : filePath ,
63
+ contents,
58
64
} ) ;
59
65
}
60
66
} catch {
@@ -68,19 +74,16 @@ export async function collectFiles(
68
74
}
69
75
}
70
76
71
- export function applyChanges (
72
- changedFiles : FileData [ ] ,
73
- basePath : string = process . cwd ( )
74
- ) : void {
77
+ export function applyChanges ( changedFiles : FileData [ ] , basePath : string = process . cwd ( ) ) : void {
75
78
for ( const file of changedFiles ) {
76
79
try {
77
80
const fullPath = path . resolve ( basePath , file . path ) ;
78
81
const dirPath = path . dirname ( fullPath ) ;
79
-
82
+
80
83
fs . mkdirSync ( dirPath , { recursive : true } ) ;
81
84
fs . writeFileSync ( fullPath , file . contents , 'utf8' ) ;
82
85
} catch {
83
86
continue ;
84
87
}
85
88
}
86
- }
89
+ }
0 commit comments