-
Notifications
You must be signed in to change notification settings - Fork 964
/
dev.js
131 lines (118 loc) · 3.42 KB
/
dev.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
import fs from 'fs'
import path from 'path'
import concurrently from 'concurrently'
import terminalLink from 'terminal-link'
import { getConfig, shutdownPort } from '@redwoodjs/internal'
import { getPaths } from 'src/lib'
import c from 'src/lib/colors'
import { generatePrismaClient } from 'src/lib/generatePrismaClient'
import runPreBuildTasks from 'src/lib/runPreBuildTasks'
export const command = 'dev [side..]'
export const description = 'Start development servers for api, and web'
export const builder = (yargs) => {
yargs
.positional('side', {
choices: ['api', 'web'],
default: ['api', 'web'],
description: 'Which dev server(s) to start',
type: 'array',
})
.positional('forward', {
alias: 'fwd',
description:
'String of one or more Webpack DevServer config options, for example: `--fwd="--port=1234 --open=false"`',
type: 'string',
})
.option('esbuild', {
type: 'boolean',
required: false,
description: 'Use ESBuild for api side [experimental]',
})
.epilogue(
`Also see the ${terminalLink(
'Redwood CLI Reference',
'https://redwoodjs.com/reference/command-line-interface#dev'
)}`
)
}
export const handler = async ({
side = ['api', 'web'],
forward = '',
esbuild = false,
}) => {
// We use BASE_DIR when we need to effectively set the working dir
const BASE_DIR = getPaths().base
// For validation, e.g. dirExists?, we use these
// note: getPaths().web|api.base returns undefined on Windows
const API_DIR_SRC = getPaths().api.src
const WEB_DIR_SRC = getPaths().web.src
// Run tasks like type generate, etc.
runPreBuildTasks()
if (side.includes('api')) {
try {
await generatePrismaClient({
verbose: false,
force: false,
schema: getPaths().api.dbSchema,
})
} catch (e) {
console.error(c.error(e.message))
}
try {
await shutdownPort(getConfig().api.port)
} catch (e) {
console.error(
`Error whilst shutting down "api" port: ${c.error(e.message)}`
)
}
}
if (side.includes('web')) {
try {
await shutdownPort(getConfig().web.port)
} catch (e) {
console.error(
`Error whilst shutting down "web" port: ${c.error(e.message)}`
)
}
}
const jobs = {
api: {
name: 'api',
command: `cd "${path.join(
BASE_DIR,
'api'
)}" && yarn cross-env NODE_ENV=development yarn dev-server`,
prefixColor: 'cyan',
runWhen: () => fs.existsSync(API_DIR_SRC),
},
web: {
name: 'web',
command: `cd "${path.join(
BASE_DIR,
'web'
)}" && yarn webpack serve --config ../node_modules/@redwoodjs/core/config/webpack.development.js ${forward}`,
prefixColor: 'blue',
runWhen: () => fs.existsSync(WEB_DIR_SRC),
},
}
if (esbuild) {
jobs.api.name = 'api esbuild'
jobs.api.command =
'yarn cross-env NODE_ENV=development NODE_OPTIONS=--enable-source-maps yarn rw-api-server-watch'
jobs.web.name = 'web esbuild'
jobs.web.command = 'yarn cross-env ESBUILD=1 && ' + jobs.web.command
}
concurrently(
Object.keys(jobs)
.map((n) => side.includes(n) && jobs[n])
.filter((job) => job && job.runWhen()),
{
prefix: '{name} |',
timestampFormat: 'HH:mm:ss',
}
).catch((e) => {
if (typeof e?.message !== 'undefined') {
console.log(c.error(e.message))
}
})
}