-
Notifications
You must be signed in to change notification settings - Fork 411
/
startAction.js
278 lines (230 loc) · 7.79 KB
/
startAction.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import path from 'path'
import chalk from 'chalk'
import fse from 'fs-extra'
import {isPlainObject} from 'lodash'
import {promisify} from 'es6-promisify'
import {getDevServer} from '@sanity/server'
import getConfig from '@sanity/util/lib/getConfig'
import chooseDatasetPrompt from '../dataset/chooseDatasetPrompt'
import {tryInitializePluginConfigs} from '../../actions/config/reinitializePluginConfigs'
import checkStudioDependencyVersions from '../../util/checkStudioDependencyVersions'
import debug from '../../debug'
import {formatMessage, isLikelyASyntaxError} from './formatMessage'
export default async (args, context) => {
const flags = args.extOptions
const {output, workDir} = context
await ensureProjectConfig(context)
const sanityConfig = getConfig(workDir)
const config = sanityConfig.get('server')
const {port, hostname} = config
const httpHost = flags.host === 'all' ? '0.0.0.0' : flags.host || hostname
const httpPort = flags.port || port
const serverOptions = {
staticPath: resolveStaticPath(workDir, config),
basePath: workDir,
httpHost,
httpPort,
context,
project: sanityConfig.get('project')
}
checkStudioDependencyVersions(workDir)
let compileSpinner
const configSpinner = output.spinner('Checking configuration files...')
await tryInitializePluginConfigs({workDir, output, env: 'development'})
configSpinner.succeed()
const server = getDevServer(serverOptions)
const compiler = server.locals.compiler
// "invalid" doesn't mean the bundle is invalid, but that it is *invalidated*,
// in other words, it's recompiling
compiler.plugin('invalid', () => {
output.clear()
resetSpinner()
})
// Start the server and try to create more user-friendly errors if we encounter issues
try {
await promisify(server.listen.bind(server))(httpPort, httpHost)
} catch (err) {
gracefulDeath(httpHost, config, err)
}
// Hold off on showing the spinner until compilation has started
compiler.plugin('compile', () => resetSpinner())
// "done" event fires when Webpack has finished recompiling the bundle.
// Whether or not you have warnings or errors, you will get this event.
compiler.plugin('done', stats => {
if (compileSpinner) {
compileSpinner.succeed()
}
const hasErrors = stats.hasErrors()
const hasWarnings = stats.hasWarnings()
if (!hasErrors && !hasWarnings) {
output.print(
chalk.green(`Content Studio successfully compiled! Go to http://${httpHost}:${httpPort}`) // eslint-disable-line max-len
)
return
}
const {errors, warnings} = stats.toJson({}, true)
if (hasErrors) {
printErrors(output, errors)
return // If errors exist, ignore warnings.
}
if (hasWarnings) {
printWarnings(output, warnings)
}
output.print(chalk.green(`Content Studio listening on http://${httpHost}:${httpPort}`))
})
function resetSpinner() {
if (compileSpinner) {
compileSpinner.stop()
}
compileSpinner = output.spinner('Compiling...').start()
}
}
async function ensureProjectConfig(context) {
const {workDir, output} = context
const manifestPath = path.join(workDir, 'sanity.json')
const projectManifest = await fse.readJson(manifestPath)
const apiConfig = projectManifest.api
if (typeof apiConfig !== 'undefined' && !isPlainObject(apiConfig)) {
throw new Error('Invalid `api` property in `sanity.json` - should be an object')
}
let displayName = projectManifest.project && projectManifest.project.displayName
let {projectId, dataset} = apiConfig || {}
const configMissing = !projectId || !dataset
if (!configMissing) {
return
}
output.print('Project configuration required before starting studio')
output.print('')
if (!projectId) {
const selected = await getOrCreateProject(context)
projectId = selected.projectId
displayName = selected.displayName
}
if (!dataset) {
const client = context
.apiClient({requireUser: true, requireProject: false})
.config({projectId, useProjectHostname: true})
const apiClient = () => client
const projectContext = {...context, apiClient}
dataset = await chooseDatasetPrompt(projectContext, {allowCreation: true})
}
// Rewrite project manifest (sanity.json)
const projectInfo = projectManifest.project || {}
const newProps = {
root: true,
api: {
...(projectManifest.api || {}),
projectId,
dataset
},
project: {
...projectInfo,
// Keep original name if present
name: projectInfo.name || displayName
}
}
// Ensure root, api and project keys are at top to follow sanity.json key order convention
await fse.outputJSON(
manifestPath,
{
...newProps,
...projectManifest,
...newProps
},
{spaces: 2}
)
output.print('Project ID / dataset configured')
}
function resolveStaticPath(rootDir, config) {
const {staticPath} = config
return path.isAbsolute(staticPath) ? staticPath : path.resolve(path.join(rootDir, staticPath))
}
function gracefulDeath(httpHost, config, err) {
if (err.code === 'EADDRINUSE') {
throw new Error('Port number is already in use, configure `server.port` in `sanity.json`')
}
if (err.code === 'EACCES') {
const help =
config.port < 1024
? 'port numbers below 1024 requires root privileges'
: `do you have access to listen to the given host (${httpHost})?`
throw new Error(
`The Content Studio server does not have access to listen to given port - ${help}`
) // eslint-disable-line max-len
}
throw err
}
function printErrors(output, errors) {
output.print(chalk.red('Failed to compile.'))
output.print('')
const formattedErrors = (errors.some(isLikelyASyntaxError)
? errors.filter(isLikelyASyntaxError)
: errors
).map(message => `Error in ${formatMessage(message)}`)
formattedErrors.forEach(message => {
output.print(message)
output.print('')
})
}
function printWarnings(output, warnings) {
output.print(chalk.yellow('Compiled with warnings.'))
output.print()
warnings
.map(message => `Warning in ${formatMessage(message)}`)
.forEach(message => {
output.print(message)
output.print()
})
}
async function getOrCreateProject(context) {
const {prompt, apiClient} = context
let projects
try {
projects = await apiClient({requireProject: false}).projects.list()
} catch (err) {
throw new Error(`Failed to communicate with the Sanity API:\n${err.message}`)
}
if (projects.length === 0) {
debug('No projects found for user, prompting for name')
const projectName = await prompt.single({message: 'Project name'})
return createProject(apiClient, {displayName: projectName})
}
debug(`User has ${projects.length} project(s) already, showing list of choices`)
const projectChoices = projects.map(project => ({
value: project.id,
name: `${project.displayName} [${project.id}]`
}))
const selected = await prompt.single({
message: 'Select project to use',
type: 'list',
choices: [{value: 'new', name: 'Create new project'}, new prompt.Separator(), ...projectChoices]
})
if (selected === 'new') {
debug('User wants to create a new project, prompting for name')
return createProject(apiClient, {
displayName: await prompt.single({
message: 'Informal name for your project'
})
})
}
debug(`Returning selected project (${selected})`)
return {
projectId: selected,
displayName: projects.find(proj => proj.id === selected).displayName
}
}
function createProject(apiClient, options) {
return apiClient({
requireUser: true,
requireProject: false
})
.request({
method: 'POST',
uri: '/projects',
body: options
})
.then(response => ({
projectId: response.projectId || response.id,
displayName: options.displayName || ''
}))
}