This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
executable file
·173 lines (153 loc) · 4.56 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env node
const chalk = require('chalk')
const { Command } = require('commander')
const path = require('path')
const prompts = require('prompts')
const checkForUpdate = require('update-check')
const { createApp } = require('./create-app')
const { validateNpmName } = require('./helpers/validate-pkg')
const packageJson = require('./package.json')
const { shouldUseYarn } = require('./helpers/should-use-yarn')
const { log, error } = require('./helpers/logger')
let projectPath
const program = new Command()
.storeOptionsAsProperties() // TODO: Remove this and use Commander 7.0 opts() https://github.com/tj/commander.js/pull/1409
.name(packageJson.name)
.version(packageJson.version)
.arguments('[project-directory]')
.usage(`${chalk.yellow('[project-directory]')} ${chalk.yellow('[options]')}`)
.action(name => {
projectPath = typeof(name) === 'string' ? name.trim() : undefined
})
.option('--use-npm')
.option('--no-git', 'skip git creation and commits')
.option(
'--with-ssr',
'the generated project will have React server-side rendering'
)
.option(
'-e, --example <example-path>',
'an example to bootstrap the app with'
)
.allowUnknownOption()
.parse(process.argv)
async function run() {
const questions = []
if (!projectPath) {
questions.push({
type: 'text',
name: 'path',
message: 'What is your project named?',
initial: 'my-app',
validate: name => {
const validation = validateNpmName(path.basename(path.resolve(name)))
if (validation.valid) {
return true
}
return `Invalid project name: ${validation.problems[0]}`
}
})
}
if (!program.withSsr) {
questions.push({
type: 'confirm',
name: 'isStatic',
message:
'Is this app a statically-generated site? (For server-side-rendering choose No)',
initial: true
})
}
const defaultGitUrl = 'git+ssh://[email protected]/example/example.git'
if (program.git) {
questions.push({
type: 'text',
name: 'gitUrl',
message: 'Please provide the full URL used to clone the git repo:',
initial: defaultGitUrl
})
}
const res = await prompts(questions)
if (typeof res.path === 'string') {
projectPath = res.path.trim()
}
if (!projectPath) {
log()
log('Please specify the project directory:')
log(` ${chalk.cyan(program.name())} ${chalk.green('<project-directory>')}`)
log()
log('For example:')
log(` ${chalk.cyan(program.name())} ${chalk.green('my-next-app')}`)
log()
log(`Run ${chalk.cyan(`${program.name()} --help`)} to see all options.`)
process.exit(1)
}
const resolvedProjectPath = path.resolve(projectPath)
const projectName = path.basename(resolvedProjectPath)
const { valid, problems } = validateNpmName(projectName)
if (!valid) {
error(
`Could not create a project called ${chalk.red(
`"${projectName}"`
)} because of npm naming restrictions:`
)
problems.forEach(p => error(` ${chalk.red.bold('*')} ${p}`))
process.exit(1)
}
await createApp({
appPath: resolvedProjectPath,
useNpm: !!program.useNpm,
noGit: !program.git,
isStatic: program.withSsr ? !program.withSsr : res.isStatic,
gitRemote: program.git ? res.gitUrl : defaultGitUrl,
example:
typeof program.example === 'string' && program.example.trim()
? program.example.trim()
: undefined
})
}
const update = checkForUpdate(packageJson).catch(() => null)
async function notifyUpdate() {
try {
const res = await update
if (res && res.latest) {
const isYarn = shouldUseYarn()
log()
log(
chalk.yellow.bold(
'A new version of `create-amclin-nextjs-app` is available!'
)
)
log(`
You can update by running:
${chalk.cyan(
isYarn
? 'yarn global add create-amclin-nextjs-app'
: 'npm i -g create-amclin-nextjs-app'
)}
`)
log()
log(
`But an even better option is to globally uninstall and in the future run:`
)
log(chalk.cyan()` npx create-amclin-nextjs-app`)
log(`That way you'll always have the latest.`)
}
} catch (e) {
// ignore error
}
}
run()
.then(notifyUpdate)
.catch(async reason => {
log()
log('Aborting installation.')
if (reason.command) {
log(` ${chalk.cyan(reason.command)} has failed.`)
} else {
log(chalk.red('Unexpected error. Please report it as a bug:'))
log(reason)
}
log()
await notifyUpdate()
process.exit(1)
})