-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·122 lines (97 loc) · 3.15 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
import { dirname } from 'path'
import fs from 'fs'
import { fileURLToPath } from 'url'
import { program } from 'commander'
import chalk from 'chalk'
import inquirer from 'inquirer'
const CURR_DIR = process.cwd()
const templates = `${dirname(fileURLToPath(import.meta.url))}/templates`
function copyTemplate(destination, language, name) {
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination)
}
if (!isDestinationEmpty(destination)) {
console.log(
`⚠️ The destination folder ${chalk.yellow(
destination.split('/').pop()
)} is not empty. Please make sure it is empty before proceeding.`
)
return
}
const templatePath =
language === 'javascript'
? `${templates}/express-js`
: `${templates}/express-ts`
console.log(
`⏳ Copying ${
language === 'javascript'
? chalk.yellow('Javascript')
: chalk.cyan('Typescript')
} template...`
)
fs.cpSync(templatePath, destination, { recursive: true })
const packageJsonPath = `${destination}/package.json`
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
packageJson.name = name
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n')
fs.renameSync(`${destination}/_gitignore`, `${destination}/.gitignore`)
}
async function handleNewAction(projectName, options) {
console.log(chalk.green('🚀 Preparing your express app...'))
let projectPath = `${CURR_DIR}/${projectName}`
if (projectName === '.' || projectName === './') {
projectName = process.cwd().split('/').pop()
projectPath = process.cwd()
}
let { javascript, typescript } = options
if (typescript && javascript) {
console.log(chalk.red('⚠️ Please specify only one language to use!'))
return
}
if (!typescript && !javascript) {
const language = await selectLanguage()
javascript = language === 'Javascript'
typescript = language === 'Typescript'
}
if (javascript) {
copyTemplate(projectPath, 'javascript', projectName)
}
if (typescript) {
copyTemplate(projectPath, 'typescript', projectName)
}
console.log(chalk.green('🎉 Your express app is ready!'))
if (projectPath !== process.cwd()) {
console.log(
`👉 Run ${chalk.cyan(
`cd ${projectName} && npm install`
)} to install dependencies.`
)
}
console.log(`👉 Run ${chalk.cyan(`npm install`)} to install dependencies.`)
}
async function selectLanguage() {
const answers = await inquirer.prompt([
{
type: 'list',
name: 'language',
message: 'Which language do you want to use?',
choices: ['Javascript', 'Typescript'],
},
])
return answers.language
}
const isDestinationEmpty = (destination) => {
const files = fs.readdirSync(destination)
return files.length === 0
}
program
.command('new <project-name>')
.description('Create a new express app')
.option('-ts, --typescript', 'Use typescript')
.option('-js, --javascript', 'Use javascript')
.action(handleNewAction)
program
.command('generate <type> <name>')
.description('Generate a new file' + chalk.red(' (Not implemented yet)'))
program.parse(process.argv)