5
5
const fs = require ( 'fs' ) . promises ;
6
6
const path = require ( 'node:path' )
7
7
const util = require ( 'util' )
8
- const exec = util . promisify ( require ( 'child_process' ) . exec )
8
+ const loadingSpinner = require ( 'loading-spinner' )
9
+ const { spawn } = require ( 'node:child_process' )
10
+
9
11
const fsOptions = { encoding : 'utf-8' }
12
+ const spinnerOptions = { clearChar : true , hideCursor : true }
13
+ const SPINNER_SPEED = 400
10
14
11
15
if ( process . argv . length < 3 ) {
12
16
console . error ( 'Please speicify the project directory' )
@@ -20,28 +24,63 @@ fs.mkdir(sandboxDirectory).then(() => Promise.all([
20
24
// create the sandbox structure
21
25
fs . mkdir ( path . join ( sandboxDirectory , 'src' ) ) ,
22
26
fs . mkdir ( path . join ( sandboxDirectory , 'public' ) )
23
- ] ) ) . then ( ( ) => Promise . all ( [
27
+ ] ) ) . then ( ( ) => {
24
28
// get the contents of the files to be put into the sandbox
25
- fs . readFile ( path . join ( __dirname , './static/.babelrc' ) , fsOptions ) ,
26
- fs . readFile ( path . join ( __dirname , './static/index.html' ) , fsOptions ) ,
27
- fs . readFile ( path . join ( __dirname , './static/index.jsx' ) , fsOptions ) ,
28
- fs . readFile ( path . join ( __dirname , './static/webpack.config.js' ) , fsOptions ) ,
29
- fs . readFile ( path . join ( __dirname , './static/package.json' ) , fsOptions )
30
- ] ) ) . then ( ( fileTexts ) => {
29
+ process . stdout . write ( 'Retrieving sandbox configuration ' )
30
+ loadingSpinner . start ( SPINNER_SPEED , spinnerOptions )
31
+ return Promise . all ( [
32
+ fs . readFile ( path . join ( __dirname , './static/.babelrc' ) , fsOptions ) ,
33
+ fs . readFile ( path . join ( __dirname , './static/index.html' ) , fsOptions ) ,
34
+ fs . readFile ( path . join ( __dirname , './static/index.jsx' ) , fsOptions ) ,
35
+ fs . readFile ( path . join ( __dirname , './static/webpack.config.js' ) , fsOptions ) ,
36
+ fs . readFile ( path . join ( __dirname , './static/package.json' ) , fsOptions )
37
+ ] )
38
+ } ) . then ( ( fileTexts ) => {
31
39
// create the files in the sandbox
32
40
const [ babelrcText , indexHtmlText , indexJsxText , webpackConfigText , packageJsonText ] = fileTexts
33
41
const packageOptions = JSON . parse ( packageJsonText )
34
42
packageOptions . name = sandboxDirectory
35
43
44
+ loadingSpinner . stop ( )
45
+ process . stdout . write ( '\rRetrieved sandbox configuration!\n' )
46
+ process . stdout . write ( 'Initializing the sandbox ' )
47
+ loadingSpinner . start ( SPINNER_SPEED , spinnerOptions )
48
+
36
49
return Promise . all ( [
37
50
fs . writeFile ( path . join ( sandboxDirectory , './.babelrc' ) , babelrcText ) ,
38
51
fs . writeFile ( path . join ( sandboxDirectory , './public/index.html' ) , indexHtmlText ) ,
39
52
fs . writeFile ( path . join ( sandboxDirectory , './src/index.jsx' ) , indexJsxText ) ,
40
53
fs . writeFile ( path . join ( sandboxDirectory , './webpack.config.js' ) , webpackConfigText ) ,
41
54
fs . writeFile ( path . join ( sandboxDirectory , './package.json' ) , JSON . stringify ( packageOptions , null , 4 ) )
42
55
] )
43
- } ) . then ( ( ) => exec ( `cd ${ sandboxDirectory } && npm install` ) ) . then ( ( ) => { // initialize the project
44
- console . log ( `React sandbox is set up. Run \`cd ${ sandboxDirectory } && npm start\` to begin.` )
56
+ } ) . then ( ( ) => {
57
+ // install the package dependencies
58
+ loadingSpinner . stop ( )
59
+ process . stdout . write ( '\rInitialized the sandbox!\n' )
60
+ process . stdout . write ( 'Installing dependencies ' )
61
+ loadingSpinner . start ( SPINNER_SPEED , spinnerOptions )
62
+
63
+ // https://stackoverflow.com/a/43285131
64
+ const npmCommand = / ^ w i n / . test ( process . platform ) ? 'npm.cmd' : 'npm'
65
+ const npmInit = spawn ( npmCommand , [ 'install' ] , {
66
+ cwd : './' + sandboxDirectory
67
+ } )
68
+
69
+ npmInit . stdout . on ( 'data' , ( data ) => {
70
+ loadingSpinner . stop ( )
71
+ process . stdout . write ( '\rInstalled dependencies!\n' )
72
+ process . stdout . write ( data . toString ( ) )
73
+ } )
74
+
75
+ npmInit . stderr . on ( 'data' , ( data ) => {
76
+ loadingSpinner . stop ( )
77
+ process . stdout . write ( '\rError installing dependencies!\n' )
78
+ process . stdout . write ( data . toString ( ) )
79
+ } )
80
+
81
+ npmInit . on ( 'exit' , ( code ) => {
82
+ console . log ( `React sandbox is set up. Run \`cd ${ sandboxDirectory } && npm start\` to begin. Happy hacking!` )
83
+ } )
45
84
} ) . catch ( ( err ) => {
46
85
console . log ( err )
47
86
} )
0 commit comments