Skip to content

Commit e8f6084

Browse files
committed
feat: add console output
1 parent 9b91cb5 commit e8f6084

File tree

4 files changed

+94
-15
lines changed

4 files changed

+94
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

bin/index.js

+49-10
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
const fs = require('fs').promises;
66
const path = require('node:path')
77
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+
911
const fsOptions = { encoding: 'utf-8' }
12+
const spinnerOptions = { clearChar: true, hideCursor: true }
13+
const SPINNER_SPEED = 400
1014

1115
if (process.argv.length < 3) {
1216
console.error('Please speicify the project directory')
@@ -20,28 +24,63 @@ fs.mkdir(sandboxDirectory).then(() => Promise.all([
2024
// create the sandbox structure
2125
fs.mkdir(path.join(sandboxDirectory, 'src')),
2226
fs.mkdir(path.join(sandboxDirectory, 'public'))
23-
])).then(() => Promise.all([
27+
])).then(() => {
2428
// 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) => {
3139
// create the files in the sandbox
3240
const [babelrcText, indexHtmlText, indexJsxText, webpackConfigText, packageJsonText] = fileTexts
3341
const packageOptions = JSON.parse(packageJsonText)
3442
packageOptions.name = sandboxDirectory
3543

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+
3649
return Promise.all([
3750
fs.writeFile(path.join(sandboxDirectory, './.babelrc'), babelrcText),
3851
fs.writeFile(path.join(sandboxDirectory, './public/index.html'), indexHtmlText),
3952
fs.writeFile(path.join(sandboxDirectory, './src/index.jsx'), indexJsxText),
4053
fs.writeFile(path.join(sandboxDirectory, './webpack.config.js'), webpackConfigText),
4154
fs.writeFile(path.join(sandboxDirectory, './package.json'), JSON.stringify(packageOptions, null, 4))
4255
])
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 = /^win/.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+
})
4584
}).catch((err) => {
4685
console.log(err)
4786
})

package-lock.json

+33-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
{
22
"name": "create-react-sandbox",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Create a lightweight React sandbox on your local system that runs like Create React App but without all the dependencies.",
55
"author": "Bruce Wu <[email protected]>",
66
"license": "ISC",
77
"bin": {
88
"create-react-sandbox": "bin/index.js"
99
},
10-
"keywords": ["react", "create-react-app", "sandbox", "boilerplate"],
11-
"homepage": "https://github.com/bruce-x-wu/create-react-sandbox"
10+
"keywords": [
11+
"react",
12+
"create-react-app",
13+
"sandbox",
14+
"boilerplate"
15+
],
16+
"homepage": "https://github.com/bruce-x-wu/create-react-sandbox",
17+
"dependencies": {
18+
"loading-spinner": "^1.2.1"
19+
}
1220
}

0 commit comments

Comments
 (0)