-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·101 lines (90 loc) · 3.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /usr/local/bin/node
const fs = require('fs')
const shell = require('shelljs')
const chalk = require('chalk')
const yargs = require('yargs')
const Ora = require('ora')
const spinner = new Ora({ color: 'yellow' })
// Get all the templates
// function to create a packageJSON with the projectName
const templates = require('./templates')
// get all the array with dependencies
const { dependencies, devDependencies } = require('./dependencies')
const depCommand = {
'devDependencies': '-D',
'dependencies': '-S'
}
// config to the CLI when they write help
const argv = yargs
.usage(`Usage:\n $0 ${chalk.green('[project-name]')}`)
.demand(1, 'Expecting the project name')
.example('$0 shareableComponent')
.epilog(`Made with ❤️ by @LoconLuis | Usage above, more information check the repo: ${chalk.blue('https://github.com/loconluis/create-react-shared-component')}`)
.help('h')
.alias('h', 'help')
.argv
// Get the name of the project by argument on the console
const projectName = argv._[0] // Project index within the _ array of Yargs.
// create the new directory with the project-name
const createDirectory = () => {
if (shell.exec(`mkdir ${projectName}`).code !== 0) {
throw new Error('File with the same name exist.')
}
console.log(chalk.green(`\n Creating a directory for > ${projectName}...\n`))
}
// return the directory to make moves here
const cdIntoDirectory = () => shell.cd(projectName)
// write all the files
const writeFiles = (files) => {
shell.exec('mkdir src')
console.log('\n Writing some files 📝 \n')
files.forEach(file => fs.writeFileSync(file.name, file.content))
}
// Install dependencies, and devDependencies
function installDeps (type, list) {
return new Promise((resolve, reject) => {
spinner.start(`Time for ${type} 🙏🏼 `)
shell.exec(`npm i ${depCommand[type]} ${list.join(' ')}`, (code, stdout, stderr) => {
spinner.stop()
if (code !== 0) {
reject(new Error(`Something went wrong installing deps: ${stderr}`))
}
spinner.succeed(`Nice! ${type} are installed! 🤓`)
resolve()
})
})
}
// write the last logs for the app UI
const lastLogs = () => {
console.log(chalk.yellow(`\n Yes, the project is ready.`))
console.log(chalk.yellow(`\n Time to use:\n`))
console.log('>>> ', chalk.bgCyan(chalk.black(`cd ${projectName}`)))
console.log(chalk.yellow(`\nTo start run:\n`))
console.log('>>> ', chalk.bgCyan(chalk.black('npm start')))
console.log(chalk.yellow(`\nGo to:\n`))
console.log('>>> ', chalk.bgCyan(chalk.black('src/index.js')))
console.log(chalk.yellow(`\nAnd start coding 👨🏻💻. \n`))
}
// main function to exec all
(async function main () {
console.time('create-react-shared-component')
try {
// mkdir and cd $_
createDirectory()
cdIntoDirectory()
// write templates to folder
writeFiles(templates(projectName))
// waiting to install packages
console.log(chalk.green('\nHere we go..... ⚡️ \n'))
await installDeps('dependencies', dependencies)
console.log(chalk.green('\nJust a few more seconds ✌🏼 \n'))
await installDeps('devDependencies', devDependencies)
// finish
lastLogs()
} catch (e) {
spinner.stop()
console.error(chalk.red(e))
} finally {
console.timeEnd('create-react-shared-component')
}
})()