-
Notifications
You must be signed in to change notification settings - Fork 15
/
build.js
148 lines (127 loc) · 4.46 KB
/
build.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
const { reporter } = require('@dhis2/cli-helpers-engine')
const fs = require('fs-extra')
const chalk = require('chalk')
const path = require('path')
const i18n = require('../lib/i18n')
const compile = require('../lib/compile')
const makePaths = require('../lib/paths')
const makeShell = require('../lib/shell')
const parseConfig = require('../lib/parseConfig')
const exitOnCatch = require('../lib/exitOnCatch')
const generateManifest = require('../lib/generateManifest')
const bundleApp = require('../lib/bundleApp')
const loadEnvFiles = require('../lib/loadEnvFiles')
const buildModes = ['development', 'production']
const getNodeEnv = () => {
let nodeEnv = process.env['NODE_ENV']
if (nodeEnv) {
nodeEnv = nodeEnv.toLowerCase()
if (buildModes.includes(nodeEnv)) {
return nodeEnv
}
}
return null
}
// This is nasty and frankly wrong (also don't use long unweildy capitalized names in a URL!) but has to match the current DHIS2 app server
// From https://github.com/dhis2/dhis2-core/blob/master/dhis-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/App.java#L360-L371
const getUrlFriendlyName = name =>
name
.trim()
.replace(/[^A-Za-z0-9\s-]/g, '')
.replace(/ /g, '-')
const handler = async ({
cwd,
mode,
dev,
watch,
shell: shellSource,
force,
}) => {
const paths = makePaths(cwd)
mode = mode || (dev && 'development') || getNodeEnv() || 'production'
loadEnvFiles(paths, mode)
reporter.info(`Build mode: ${chalk.bold(mode)}`)
const config = parseConfig(paths)
const shell = makeShell({ config, paths })
process.env.PUBLIC_URL = process.env.PUBLIC_URL || '.'
if (!config.standalone) {
process.env.DHIS2_BASE_URL =
process.env.DHIS2_BASE_URL || config.coreApp ? `..` : `../../..`
}
await fs.remove(paths.buildOutput)
await exitOnCatch(
async () => {
reporter.info('Generating internationalization strings...')
await i18n.extract({ input: paths.src, output: paths.i18nStrings })
await i18n.generate({
input: paths.i18nStrings,
output: paths.i18nLocales,
namespace: config.name || 'default',
})
if (config.type === 'app') {
reporter.info('Bootstrapping local appShell...')
await shell.bootstrap({ shell: shellSource, force })
}
reporter.info(`Building app ${chalk.bold(config.name)}...`)
await compile({
config,
paths,
mode,
watch,
})
reporter.info(chalk.dim(` - Built in mode ${chalk.bold(mode)}`))
if (config.type === 'app') {
reporter.info('Building appShell...')
await shell.build()
reporter.info(chalk.dim(` - Built in mode ${chalk.bold(mode)}`))
}
},
{
name: 'build',
onError: () => reporter.error('Build script failed'),
}
)
if (config.type === 'app') {
if (!fs.pathExistsSync(paths.shellBuildOutput)) {
reporter.error('No build output found')
process.exit(1)
}
await fs.copy(paths.shellBuildOutput, paths.buildAppOutput)
reporter.info('Generating manifest...')
await generateManifest(paths, config, process.env.PUBLIC_URL)
const appBundle = paths.buildAppBundle
.replace(/{{name}}/, config.name)
.replace(/{{version}}/, config.version)
reporter.info(
`Creating app archive at ${chalk.bold(
path.relative(process.cwd(), appBundle)
)}...`
)
await bundleApp(paths.buildAppOutput, appBundle)
reporter.print(chalk.green('\n**** DONE! ****'))
}
}
const command = {
aliases: 'b',
desc: 'Build a production app bundle for use with the DHIS2 app-shell',
builder: {
mode: {
description: 'Specify the target build environment',
aliases: 'm',
choices: buildModes,
defaultDescription: 'production',
},
dev: {
type: 'boolean',
description: 'Build in development mode',
conflicts: 'mode',
},
watch: {
type: 'boolean',
description: 'Watch source files for changes',
default: false,
},
},
handler,
}
module.exports = command