-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (79 loc) · 2.36 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/bin/env node
import path from 'path'
import chalk from 'chalk'
import inquirer from 'inquirer'
import { createSpinner } from 'nanospinner'
import { exec } from 'child_process'
import fs from 'fs'
import util from 'util'
const myExec = util.promisify(exec)
const projectNameRegex = new RegExp('\w|\d_|-')
const repository = 'https://github.com/mateusabelli/create-webreact-app.git'
let projectName = 'my-webreact-app'
let projectPath = ''
function handleFailure(message) {
const spinner = createSpinner(message)
spinner.error()
return process.exit(1)
}
async function getProjectName() {
const answers = await inquirer.prompt({
name: 'project_name',
type: 'input',
message: 'What is your project named?',
default() {
return projectName
}
})
projectName = answers.project_name
if (projectNameRegex.test(projectName)) {
projectPath = path.resolve(projectName)
} else {
handleFailure('Invalid project name or path.')
}
}
await getProjectName()
async function getProjectTemplate() {
const answers = await inquirer.prompt({
name: 'project_template',
type: 'list',
message: `Would you like to use ${chalk.blue('Typescript')} with this project?`,
choices: [
'Yes',
'No'
]
})
return handleAnswer(answers.project_template)
}
await getProjectTemplate()
async function handleAnswer(isTypescript) {
const bootstrapMsg = `
You're good to go, now run:
cd ${projectName}
npm install
npm run dev
`
if (isTypescript === 'Yes') {
const spinner = createSpinner(`Creating project at ${projectPath}`).start()
await myExec(`git clone --branch react-ts ${repository} ${projectName}`, async (error) => {
if (error) {
handleFailure(`error: ${error.message}`)
}
await fs.promises.rm(`${projectPath}/.git`, { recursive: true, force: true })
spinner.success()
console.log(bootstrapMsg)
return process.exit(0)
})
} else {
const spinner = createSpinner(`Creating project at ${projectPath}`).start()
await myExec(`git clone --branch react ${repository} ${projectName}`, async (error) => {
if (error) {
handleFailure(`error: ${error.message}`)
}
await fs.promises.rm(`${projectPath}/.git`, { recursive: true, force: true })
spinner.success()
console.log(bootstrapMsg)
return process.exit(0)
})
}
}