9
9
* I just made some modifications and added some new functions.
10
10
* **/
11
11
12
+ const { execSync } = require ( 'child_process' ) ;
13
+ const {
14
+ rmdirSync,
15
+ rmSync,
16
+ existsSync,
17
+ lstatSync,
18
+ readdirSync,
19
+ unlinkSync,
20
+ } = require ( 'fs' ) ;
21
+
12
22
/* eslint-disable @typescript-eslint/no-var-requires */
13
23
/* eslint-disable @typescript-eslint/no-explicit-any */
14
24
@@ -23,7 +33,23 @@ const replace = require('replace-in-file');
23
33
const { readFileSync, writeFileSync } = require ( 'fs' ) ;
24
34
const { cyan, green, red, underline, yellow } = require ( 'colors' ) ;
25
35
const _prompt = require ( 'prompt' ) ;
26
- const { mv, rm, which, exec } = require ( 'shelljs' ) ;
36
+
37
+ function deleteFolderRecursive ( path : any ) {
38
+ if ( existsSync ( path ) ) {
39
+ const files = readdirSync ( path ) ;
40
+ files . forEach ( function ( file : any ) {
41
+ const curPath = path + '/' + file ;
42
+ if ( lstatSync ( curPath ) . isDirectory ( ) ) {
43
+ // recurse
44
+ deleteFolderRecursive ( curPath ) ;
45
+ } else {
46
+ // delete file
47
+ unlinkSync ( curPath ) ;
48
+ }
49
+ } ) ;
50
+ rmdirSync ( path ) ;
51
+ }
52
+ }
27
53
28
54
// Note: These should all be relative to the project root directory
29
55
const rmDirs = [ '.git' , 'tools' ] ;
@@ -45,12 +71,11 @@ function removeItems() {
45
71
// The directories and files are combined here, to simplify the function,
46
72
// as the 'rm' command checks the item type before attempting to remove it
47
73
const rmItems = rmDirs . concat ( rmFiles ) ;
48
- rm (
49
- '-rf' ,
50
- rmItems . map ( ( f ) => resolve ( __dirname , '..' , f ) )
51
- ) ;
52
- console . log ( red ( rmItems . join ( '\n' ) ) ) ;
53
74
75
+ rmItems
76
+ . map ( ( f ) => resolve ( __dirname , '..' , f ) )
77
+ . forEach ( ( p ) => deleteFolderRecursive ( p ) ) ;
78
+ console . log ( red ( rmItems . join ( '\n' ) ) ) ;
54
79
console . log ( '\n' ) ;
55
80
}
56
81
@@ -103,9 +128,10 @@ function finalize() {
103
128
console . log ( underline . white ( 'Finalizing' ) ) ;
104
129
105
130
// Recreate Git folder
106
- const gitInitOutput = exec ( 'git init "' + resolve ( __dirname , '..' ) + '"' , {
107
- silent : true ,
108
- } ) . stdout ;
131
+ const gitInitOutput = execSync (
132
+ 'git init "' + resolve ( __dirname , '..' ) + '"' ,
133
+ { }
134
+ ) . toString ( ) ;
109
135
console . log ( green ( gitInitOutput . replace ( / ( \n | \r ) + / g, '' ) ) ) ;
110
136
111
137
// Remove post-install command
@@ -126,11 +152,11 @@ function finalize() {
126
152
console . log ( green ( 'Postinstall script has been removed' ) ) ;
127
153
128
154
console . log ( yellow ( 'Removing yarn.lock and performing a clean install...' ) ) ;
129
- rm ( 'yarn.lock' ) ;
130
- rm ( '-rf' , 'node_modules' ) ;
131
- exec ( 'yarn install' ) ;
132
- exec ( 'yarn build' ) ;
133
- exec ( "git add . && git commit -am 'chore: init' --no-verify" ) ;
155
+ rmSync ( 'yarn.lock' ) ;
156
+ deleteFolderRecursive ( 'node_modules' ) ;
157
+ execSync ( 'yarn install' ) ;
158
+ execSync ( 'yarn build' ) ;
159
+ execSync ( "git add . && git commit -am 'chore: init' --no-verify" ) ;
134
160
console . log ( '\n' ) ;
135
161
}
136
162
/**
@@ -146,23 +172,23 @@ function setupLibrary(libraryName: string, generateExample = false) {
146
172
) ;
147
173
148
174
// Get the Git username and email before the .git directory is removed
149
- const username = exec ( 'git config user.name' ) . stdout . trim ( ) ;
150
- const usermail = exec ( 'git config user.email' ) . stdout . trim ( ) ;
175
+ const username = execSync ( 'git config user.name' ) . toString ( ) . trim ( ) ;
176
+ const usermail = execSync ( 'git config user.email' ) . toString ( ) . trim ( ) ;
151
177
152
178
if ( generateExample ) {
153
179
yellow ( 'Installing the test application into example/ directory...' ) ;
154
- exec ( 'npx create-react-app example' ) ;
155
- exec ( 'echo "SKIP_PREFLIGHT_CHECK=true" >> example/.env' ) ;
180
+ execSync ( 'npx create-react-app example' ) ;
181
+ execSync ( 'echo "SKIP_PREFLIGHT_CHECK=true" >> example/.env' ) ;
156
182
}
157
- mv ( ' tools/README.md' , ' README.md') ;
183
+ execSync ( 'mv tools/README.md README.md') ;
158
184
removeItems ( ) ;
159
185
160
186
modifyGitignoreFile ( ) ;
161
187
modifyContents ( libraryName , username , usermail ) ;
162
188
163
189
if ( generateExample ) {
164
190
console . log ( yellow ( 'Linking packages to the example app...' ) ) ;
165
- exec (
191
+ execSync (
166
192
'cd example && yarn add link:.. link:../node_modules/react link:../node_modules/react-dom && cd ..'
167
193
) ;
168
194
}
@@ -297,7 +323,9 @@ _prompt.message = '';
297
323
// Clear console
298
324
process . stdout . write ( '\x1B[2J\x1B[0f' ) ;
299
325
300
- if ( ! which ( 'git' ) ) {
326
+ try {
327
+ execSync ( 'git --version' ) ;
328
+ } catch {
301
329
console . log ( red ( 'Sorry, this script requires git' ) ) ;
302
330
removeItems ( ) ;
303
331
process . exit ( 1 ) ;
0 commit comments