This repository was 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
/
Copy pathcreate-app.js
208 lines (182 loc) · 5.38 KB
/
create-app.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const chalk = require('chalk')
const fs = require('fs')
const makeDir = require('make-dir')
const os = require('os')
const path = require('path')
const merge = require('deepmerge')
const userName = require('git-user-name')
const { downloadAndExtractExample } = require('./helpers/examples')
const { copyTemplateFiles } = require('./helpers/copy-template-files')
const { hasExample } = require('./helpers/examples')
const { install } = require('./helpers/install')
const { isFolderEmpty } = require('./helpers/is-folder-empty')
const { getOnline } = require('./helpers/is-online')
const { shouldUseYarn } = require('./helpers/should-use-yarn')
const { initGit, commitFirst } = require('./helpers/init-git')
const { populateProject } = require('./helpers/populate-project')
const { log, error } = require('./helpers/logger')
const templateSettings = require('./templates/default.json')
const ssrTemplateSettings = require('./templates/default-ssr.json')
const staticTemplateSettings = require('./templates/default-static.json')
const createApp = async ({
appPath,
example,
gitRemote,
isStatic,
noGit = false,
useNpm,
}) => {
const root = path.resolve(appPath)
const appName = path.basename(root)
if (example) {
const found = await hasExample(example)
if (!found) {
error(
`Could not locate an example named ${chalk.red(
`"${example}"`
)}. Please check your spelling and try again.`
)
process.exit(1)
}
}
const version = '0.1.0'
await makeDir(root)
if (!isFolderEmpty(root, appName)) {
process.exit(1)
}
const useYarn = useNpm ? false : shouldUseYarn()
const isOnline = !useYarn || (await getOnline())
const originalDirectory = process.cwd()
const displayedCommand = useYarn ? 'yarn' : 'npm'
log(`Creating a new Next.js app in ${chalk.green(root)}.`)
log()
await makeDir(root)
process.chdir(root)
const homepage = `https://github.com/amclin/${appName}`
const author = userName()
const year = new Date().getFullYear()
if (noGit) {
log(`Skipping creation of git repository.`)
log()
} else {
log(`Initializing git repository.`)
log()
await initGit(root, { gitRemote })
}
if (example) {
log(
`Downloading files for example ${chalk.cyan(
example
)}. This might take a moment.`
)
log()
await downloadAndExtractExample(root, example)
log('Installing packages. This might take a couple of minutes.')
log()
await install({
root,
useYarn,
isOnline,
})
log()
} else {
const packageJson = merge(
{
...templateSettings.package,
name: `${appName}`,
version,
author,
private: true,
repository: {
type: 'git',
url: gitRemote,
},
homepage,
bugs: {
url: `${homepage}/issues`,
},
},
isStatic ? staticTemplateSettings.package : ssrTemplateSettings.package
)
fs.writeFileSync(
path.join(root, 'package.json'),
JSON.stringify(packageJson, null, 2) + os.EOL
)
log(`Installing runtime dependencies using ${displayedCommand}:`)
templateSettings.dependencies.forEach((dep) => {
log(` * ${chalk.cyan(dep)}`)
})
log()
await install({
root,
dependencies: templateSettings.dependencies,
useYarn,
isOnline,
})
log()
log(`Installing dev dependencies using ${displayedCommand}:`)
templateSettings.devDependencies.forEach((dep) => {
log(` * ${chalk.cyan(dep)}`)
})
log()
await install({
root,
dependencies: templateSettings.devDependencies,
useYarn,
isOnline,
devDependencies: true,
})
log()
await copyTemplateFiles(root, 'default')
// For sites with server-side React (not staticly generated)
// We need a different docker file and different build
// instructions
await copyTemplateFiles(root, isStatic ? 'default-static' : 'default-ssr')
await populateProject({ root, appName, homepage, author, year })
}
if (noGit) {
log(`Skipping initial commit to git repository.`)
log()
} else {
log(`Committing to the git repository.`)
log()
await commitFirst({ version })
}
let cdpath = ''
if (path.join(originalDirectory, appName) === appPath) {
cdpath = appName
} else {
cdpath = appPath
}
log(`${chalk.green('Success!')} Created ${appName} at ${appPath}`)
log('Inside that directory, you can run several commands:')
log()
log(chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}dev`))
log(' Starts the development server.')
log()
log(chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}build`))
log(' Builds the app for production.')
log()
log(chalk.cyan(` ${displayedCommand} start`))
log(' Runs the built app in production mode.')
log()
log('We suggest that you begin by typing:')
log()
log(chalk.cyan(` cd ${cdpath}`))
log(` ${chalk.cyan(`${displayedCommand} ${useYarn ? '' : 'run '}dev`)}`)
log()
log()
if (!noGit) {
log(`-GitHub----------------------------------------
A git repo is created, but changes have not been
pushed to the remote git server. Make sure an
empy repo exists at:
${chalk.cyan(gitRemote)}
and then run the onetime command:
${chalk.cyan('git push --follow-tags push')}`)
log(`-----------------------------------------------`)
}
}
module.exports = {
createApp,
}