1
- import { promises as fs } from 'fs' ;
2
1
import path from 'path' ;
2
+ import pLimit from 'p-limit' ;
3
+ import { promises as fs } from 'fs' ;
3
4
4
5
const PACKAGE_FOLDER = process . env . PACKAGE_FOLDER || 'package' ;
5
6
const CWD = process . cwd ( ) ;
6
7
8
+ const limit = pLimit ( 10 ) ;
9
+
7
10
const getFolder = async ( folder , depth = 0 ) => {
8
11
const contents = await fs . readdir ( path . join ( CWD , folder ) , {
9
12
withFileTypes : true
@@ -14,31 +17,28 @@ const getFolder = async (folder, depth = 0) => {
14
17
folders : await Promise . all (
15
18
contents
16
19
. filter ( ( f ) => f . isDirectory ( ) )
17
- . map ( ( dir ) => getFolder ( `${ folder } /${ dir . name } ` , depth + 1 ) )
20
+ . map ( ( dir ) => limit ( ( ) => getFolder ( `${ folder } /${ dir . name } ` , depth + 1 ) ) )
18
21
) ,
19
22
files : contents . filter ( ( f ) => ! f . isDirectory ( ) ) . map ( ( { name } ) => `${ folder } /${ name } ` )
20
23
} ;
21
24
} ;
22
25
23
- const transformFolder = async ( { folders, files, depth } ) => {
24
- await Promise . all (
25
- files . map ( async ( f ) => {
26
- const filepath = path . join ( CWD , f ) ;
27
- const contents = await fs . readFile ( filepath , 'utf-8' ) ;
28
- const base = depth ? [ ...Array ( depth ) . fill ( '..' ) ] . join ( '/' ) : './' ;
29
- const updated = contents
30
- . replace ( / f r o m ' \$ l i b ( .* ) ' / g, `from '${ base } $1'` )
31
- . replace ( / s r c \/ l i b \/ / g, '' ) ;
32
- await Promise . all (
33
- [
34
- updated === contents ? null : fs . writeFile ( filepath , updated ) ,
35
- ...folders . map ( transformFolder )
36
- ] . filter ( Boolean )
37
- ) ;
38
- } )
39
- ) ;
26
+ const transformFile = async ( file , depth ) => {
27
+ const filepath = path . join ( CWD , file ) ;
28
+ const contents = await fs . readFile ( filepath , 'utf-8' ) ;
29
+ const base = depth ? [ ...Array ( depth ) . fill ( '..' ) ] . join ( '/' ) : './' ;
30
+ const updated = contents
31
+ . replace ( / f r o m ' \$ l i b ( .* ) ' / g, `from '${ base } $1'` )
32
+ . replace ( / s r c \/ l i b \/ / g, '' ) ;
33
+ await fs . writeFile ( filepath , updated ) ;
40
34
} ;
41
35
36
+ const transformFolder = ( { folders, files, depth } ) =>
37
+ Promise . all ( [
38
+ ...files . map ( ( f ) => limit ( ( ) => transformFile ( f , depth ) ) ) ,
39
+ ...folders . map ( ( f ) => limit ( ( ) => transformFolder ( f ) ) )
40
+ ] ) ;
41
+
42
42
getFolder ( PACKAGE_FOLDER )
43
43
. then ( transformFolder )
44
44
. catch ( console . error )
0 commit comments