99 * I just made some modifications and added some new functions.
1010 * **/
1111
12+ const { execSync } = require ( 'child_process' ) ;
13+ const {
14+ rmdirSync,
15+ rmSync,
16+ existsSync,
17+ lstatSync,
18+ readdirSync,
19+ unlinkSync,
20+ } = require ( 'fs' ) ;
21+
1222/* eslint-disable @typescript-eslint/no-var-requires */
1323/* eslint-disable @typescript-eslint/no-explicit-any */
1424
@@ -23,7 +33,23 @@ const replace = require('replace-in-file');
2333const { readFileSync, writeFileSync } = require ( 'fs' ) ;
2434const { cyan, green, red, underline, yellow } = require ( 'colors' ) ;
2535const _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+ }
2753
2854// Note: These should all be relative to the project root directory
2955const rmDirs = [ '.git' , 'tools' ] ;
@@ -45,12 +71,11 @@ function removeItems() {
4571 // The directories and files are combined here, to simplify the function,
4672 // as the 'rm' command checks the item type before attempting to remove it
4773 const rmItems = rmDirs . concat ( rmFiles ) ;
48- rm (
49- '-rf' ,
50- rmItems . map ( ( f ) => resolve ( __dirname , '..' , f ) )
51- ) ;
52- console . log ( red ( rmItems . join ( '\n' ) ) ) ;
5374
75+ rmItems
76+ . map ( ( f ) => resolve ( __dirname , '..' , f ) )
77+ . forEach ( ( p ) => deleteFolderRecursive ( p ) ) ;
78+ console . log ( red ( rmItems . join ( '\n' ) ) ) ;
5479 console . log ( '\n' ) ;
5580}
5681
@@ -103,9 +128,10 @@ function finalize() {
103128 console . log ( underline . white ( 'Finalizing' ) ) ;
104129
105130 // 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 ( ) ;
109135 console . log ( green ( gitInitOutput . replace ( / ( \n | \r ) + / g, '' ) ) ) ;
110136
111137 // Remove post-install command
@@ -126,11 +152,11 @@ function finalize() {
126152 console . log ( green ( 'Postinstall script has been removed' ) ) ;
127153
128154 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" ) ;
134160 console . log ( '\n' ) ;
135161}
136162/**
@@ -146,23 +172,23 @@ function setupLibrary(libraryName: string, generateExample = false) {
146172 ) ;
147173
148174 // 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 ( ) ;
151177
152178 if ( generateExample ) {
153179 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' ) ;
156182 }
157- mv ( ' tools/README.md' , ' README.md') ;
183+ execSync ( 'mv tools/README.md README.md') ;
158184 removeItems ( ) ;
159185
160186 modifyGitignoreFile ( ) ;
161187 modifyContents ( libraryName , username , usermail ) ;
162188
163189 if ( generateExample ) {
164190 console . log ( yellow ( 'Linking packages to the example app...' ) ) ;
165- exec (
191+ execSync (
166192 'cd example && yarn add link:.. link:../node_modules/react link:../node_modules/react-dom && cd ..'
167193 ) ;
168194 }
@@ -297,7 +323,9 @@ _prompt.message = '';
297323// Clear console
298324process . stdout . write ( '\x1B[2J\x1B[0f' ) ;
299325
300- if ( ! which ( 'git' ) ) {
326+ try {
327+ execSync ( 'git --version' ) ;
328+ } catch {
301329 console . log ( red ( 'Sorry, this script requires git' ) ) ;
302330 removeItems ( ) ;
303331 process . exit ( 1 ) ;
0 commit comments