-
Notifications
You must be signed in to change notification settings - Fork 10
/
dev.js
executable file
·231 lines (202 loc) · 9.12 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
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
#!/usr/bin/env node
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
/* eslint-disable no-template-curly-in-string */
const BaseScript = require('../lib/abstract-script')
const path = require('path')
const fs = require('fs-extra')
const BuildActions = require('./build.actions')
const DeployActions = require('./deploy.actions')
const utils = require('../lib/utils')
// TODO: this jar should become part of the distro, OR it should be pulled from bintray or similar.
const OW_JAR_URL = 'https://github.com/chetanmeh/incubator-openwhisk/releases/download/v0.10/openwhisk-standalone.jar'
// This path will be relative to this module, and not the cwd, so multiple projects can use it.
const OW_JAR_FILE = path.resolve(__dirname, '../bin/openwhisk-standalone.jar')
// const OW_LOG_FILE = '.openwhisk-standalone.log'
const DOTENV_SAVE = '.env.app.save'
const WSK_DEBUG_PROPS = '.wskdebug.props.tmp'
const CODE_DEBUG_SAVE = '.vscode/launch.json.save'
const CODE_DEBUG = '.vscode/launch.json'
const OW_LOCAL_APIHOST = 'http://localhost:3233'
const OW_LOCAL_NAMESPACE = 'guest'
const OW_LOCAL_AUTH = '23bc46b1-71f6-4ed5-8c54-816aa4f8c502:123zO3xZCLrMN6v2BKK1dXYFpXlPkccOFqm12CdAsMgRU4VrNZ9lyGVCGuMDGIwP'
class ActionServer extends BaseScript {
async run (args = []) {
const taskName = 'Local Dev Server'
this.emit('start', taskName)
// control variables
const isLocal = !this.config.actions.devRemote
const hasFrontend = this.config.app.hasFrontend
// port for UI
const port = args[0] || process.env.PORT || 9080
// state
const resources = {}
let devConfig // config will be different if local or remote
// bind cleanup function
process.on('SIGINT', () => cleanup(null, resources))
try {
if (isLocal) {
this.emit('progress', 'checking if docker is running...')
if (!utils.hasDockerCLI()) throw new Error('could not find docker CLI, please make sure docker is installed')
if (!utils.isDockerRunning()) throw new Error('docker is not running, please make sure to start docker')
this.emit('progres', `downloading OpenWhisk standalone jar from ${OW_JAR_URL} to ${OW_JAR_FILE}, this might take a while... (to be done only once!)`)
if (!fs.existsSync(OW_JAR_FILE)) await utils.downloadOWJar(OW_JAR_URL, OW_JAR_FILE)
this.emit('progress', 'starting local OpenWhisk stack..')
const res = await utils.runOpenWhiskJar(OW_JAR_FILE, OW_LOCAL_APIHOST, 20000, { stdio: 'inherit' })
resources.owProc = res.proc
this.emit('progress', `saving .env to ${DOTENV_SAVE} and writing new .env with local OpenWhisk guest credentials..`)
utils.saveAndReplaceDotEnvCredentials(DOTENV_SAVE, OW_LOCAL_APIHOST, OW_LOCAL_NAMESPACE, OW_LOCAL_AUTH)
resources.dotenv = '.env'
resources.dotenvSave = DOTENV_SAVE
devConfig = require('../lib/config-loader')() // reload config
} else {
// check credentials
utils.checkOpenWhiskCredentials(this.config)
this.emit('progress', 'using remote actions')
devConfig = this.config
}
// build and deploy actions // todo support live reloading ?
this.emit('progress', 'redeploying actions..')
await (new BuildActions(devConfig)).run()
await (new DeployActions(devConfig)).run()
this.emit('progress', `writing credentials to tmp wskdebug config '${WSK_DEBUG_PROPS}'..`)
// prepare wskprops for wskdebug
fs.writeFileSync(WSK_DEBUG_PROPS, `NAMESPACE=${devConfig.ow.namespace}\nAUTH=${devConfig.ow.auth}\nAPIHOST=${devConfig.ow.apihost}`)
resources.wskdebugProps = WSK_DEBUG_PROPS
this.emit('progress', 'setting up vscode debug configuration files..')
// todo refactor the whole .vscode/launch.json piece into utils
// todo 2 don't enforce vscode config to non vscode dev
fs.ensureDirSync(path.dirname(CODE_DEBUG))
if (fs.existsSync(CODE_DEBUG)) {
if (!fs.existsSync(CODE_DEBUG_SAVE)) {
fs.moveSync(CODE_DEBUG, CODE_DEBUG_SAVE)
}
}
fs.writeFileSync(CODE_DEBUG, JSON.stringify(await this.generateVSCodeDebugConfig(devConfig, hasFrontend, port), null, 2))
resources.vscodeDebugConfig = CODE_DEBUG
resources.vscodeDebugConfigSave = CODE_DEBUG_SAVE
if (hasFrontend) {
// inject backend urls into ui
this.emit('progress', 'injecting backend urls into frontend config')
await utils.writeConfig(devConfig.web.injectedConfig, devConfig.actions.urls)
this.emit('progress', 'starting local frontend server..')
// todo: does it have to be index.html?
const entryFile = path.join(devConfig.web.src, 'index.html')
const app = utils.getUIDevExpressApp(entryFile, devConfig.web.distDev)
resources.uiServer = app.listen(port)
this.emit('progress', `local frontend server running at http://localhost:${port}`)
}
this.emit('progress', 'press CTRL+C to terminate dev environment')
if (!resources.owProc && !resources.uiServer) {
// not local + ow is not running => need to explicitely wait for CTRL+C
// trick to avoid termination
process.stdin.resume()
}
} catch (e) {
cleanup(e)
}
}
// todo make util not instance function
async generateVSCodeDebugConfig (devConfig, hasFrontend, uiPort) {
const packageName = devConfig.ow.package
const manifestActions = devConfig.manifest.package.actions // yaml.safeLoad(await fs.readFile(devConfig.manifest.dist, 'utf8')).packages[packageName].actions //
const actionConfigNames = []
const actionConfigs = Object.keys(manifestActions).map(an => {
const name = `Action:${packageName}/${an}`
actionConfigNames.push(name)
const action = manifestActions[an]
const actionPath = this._absApp(action.function)
const config = {
type: 'node',
request: 'launch',
name: name,
// todo allow for global install aswell
runtimeExecutable: this._absApp('./node_modules/.bin/wskdebug'),
env: { WSK_CONFIG_FILE: this._absApp(WSK_DEBUG_PROPS) },
timeout: 30000,
// replaces remoteRoot with localRoot to get src files
localRoot: this._absApp('.'),
remoteRoot: '/code',
outputCapture: 'std'
}
const actionFileStats = fs.lstatSync(actionPath)
if (actionFileStats.isFile()) {
// set wskdebug arg w/ path to src file
config.runtimeArgs = [
`${packageName}/${an}`,
actionPath,
'-v'
]
} else if (actionFileStats.isDirectory()) {
// set wskdebug arg w/ path to src file in function dir
const zipMain = (fs.existsSync(path.join(actionPath, 'package.json')) && fs.readJsonSync(path.join(actionPath, 'package.json')).main) || 'index.js'
config.runtimeArgs = [
`${packageName}/${an}`,
path.join(actionPath, zipMain),
'-v'
]
} else {
throw new Error(`${actionPath} is not a valid file or directory`)
}
return config
})
const debugConfig = {
configurations: actionConfigs,
compounds: [{
name: 'Actions',
configurations: actionConfigNames
}]
}
if (hasFrontend) {
debugConfig.configurations.push({
type: 'chrome',
request: 'launch',
name: 'Web',
url: `http://localhost:${uiPort}`,
webRoot: devConfig.web.src,
sourceMapPathOverrides: {
'webpack:///src/*': '${webRoot}/*'
}
})
debugConfig.compounds.push({
name: 'WebAndActions',
configurations: ['Web'].concat(actionConfigNames)
})
}
return debugConfig
}
}
function cleanup (err, resources) {
console.error() // return to new line
if (err) console.error(err)
if (resources.dotenv && resources.dotenvSave && fs.existsSync(resources.dotenvSave)) {
console.error('resetting .env file...')
fs.moveSync(resources.dotenvSave, resources.dotenv, { overwrite: true })
}
if (resources.owProc) {
console.error('killing local OpenWhisk process...')
resources.owProc.kill()
}
if (resources.wskdebugProps) {
console.error('removing wskdebug tmp credentials file...')
fs.removeSync(resources.wskdebugProps)
}
if (resources.vscodeDebugConfig && resources.vscodeDebugConfigSave && fs.existsSync(resources.vscodeDebugConfigSave)) {
console.error('resetting .vscode/launch.json...')
fs.moveSync(resources.vscodeDebugConfigSave, resources.vscodeDebugConfig, { overwrite: true })
}
if (resources.uiServer) {
console.error('killing ui dev server...')
resources.uiServer.close()
}
err ? process.exit(1) : process.exit(0)
}
module.exports = ActionServer