Skip to content

Commit

Permalink
fix: use of generator-aio-app/add-vscode-config generator (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
shazron authored Jan 26, 2021
1 parent fad0a6d commit 7eacde2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 328 deletions.
2 changes: 1 addition & 1 deletion src/lib/run-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async function runDev (args = [], config, options = {}, log = () => {}) {

log('setting up vscode debug configuration files...')
const vscodeConfig = vscode(devConfig)
await vscodeConfig.update({ hasFrontend, withBackend, frontEndUrl })
await vscodeConfig.update({ frontEndUrl })
cleanup.add(() => vscodeConfig.cleanup(), 'cleaning up vscode debug configuration files...')

// automatically fetch logs if there are actions
Expand Down
41 changes: 31 additions & 10 deletions src/lib/run-local-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ const OW_TIMEOUT = 60000
* @property {Function} cleanup callback function to cleanup available resources
*/

/**
* @typedef {object} RuntimeCredentials
* @property {string} namespace the runtime namespace
* @property {string} auth the runtime auth key
* @property {string} apihost the runtime apihost
*/

/**
* Checks the system for pre-requisites to run local Openwhisk, then runs it.
*
Expand Down Expand Up @@ -92,16 +99,7 @@ async function runDevLocal (config, log = () => undefined, verbose = false) {
devConfig.ow = { ...devConfig.ow, ...runtime }

log(`writing credentials to tmp wskdebug config '${devConfig.envFile}'`)
// prepare wskprops for wskdebug
fs.ensureDirSync(config.app.dist)
const envFile = rtLibUtils._absApp(devConfig.root, devConfig.envFile)
await fs.outputFile(envFile, dedent(`
# This file is auto-generated, do not edit.
# The items below are temporary credentials for local debugging
OW_NAMESPACE=${devConfig.ow.namespace}
OW_AUTH=${devConfig.ow.auth}
OW_APIHOST=${devConfig.ow.apihost}
`))
await writeLocalEnvFile(devConfig, runtime)

const cleanup = () => {
aioLogger.debug('stopping local OpenWhisk stack...')
Expand All @@ -119,4 +117,27 @@ async function runDevLocal (config, log = () => undefined, verbose = false) {
}
}

/**
* Writes the local debugging .env file
*
* @param {object} appConfig the app config
* @param {RuntimeCredentials} runtimeCredentials the runtime credentials
*/
async function writeLocalEnvFile (appConfig, runtimeCredentials) {
const { root, envFile } = appConfig
const { dist } = appConfig.app
const { namespace, auth, apihost } = runtimeCredentials

fs.ensureDirSync(dist)
const envFilePath = rtLibUtils._absApp(root, envFile)

await fs.outputFile(envFilePath, dedent(`
# This file is auto-generated, do not edit.
# The items below are temporary credentials for local debugging
OW_NAMESPACE=${namespace}
OW_AUTH=${auth}
OW_APIHOST=${apihost}
`))
}

module.exports = runDevLocal
126 changes: 12 additions & 114 deletions src/lib/vscode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-
const rtLibUtils = require('@adobe/aio-lib-runtime').utils
const fs = require('fs-extra')
const path = require('path')
const cloneDeep = require('lodash.clonedeep')
const yeoman = require('yeoman-environment')

const LAUNCH_JSON_FILE = '.vscode/launch.json'
const LAUNCH_JSON_FILE_BACKUP = '.vscode/launch.json.save'
Expand All @@ -38,7 +38,17 @@ function update (config) {
fs.moveSync(mainFile, backupFile)
}
}
fs.writeJSONSync(mainFile, await generateConfig(config, props), { spaces: 2 })

const generator = '@adobe/generator-aio-app/generators/add-vscode-config'
const env = yeoman.createEnv()
env.register(require.resolve(generator), 'gen')

await env.run('gen', {
'app-config': config,
'env-file': config.envFile,
'frontend-url': props.frontEndUrl,
'skip-prompt': true
})
}
}

Expand All @@ -63,118 +73,6 @@ function cleanup (config) {
}
}

/** @private */
function processPackageActionConfigs (appConfig, packageName, pkg) {
const { ow, root, envFile } = appConfig

const actionConfigNames = []
const actionConfigs = Object.keys(pkg.actions).map(an => {
const name = `Action:${packageName}/${an}`
actionConfigNames.push(name)
const action = pkg.actions[an]
const actionPath = rtLibUtils._absApp(root, action.function)

const config = {
type: 'pwa-node',
request: 'launch',
name: name,
runtimeExecutable: rtLibUtils._absApp(root, './node_modules/.bin/wskdebug'),
envFile: path.join('${workspaceFolder}', envFile), // eslint-disable-line no-template-curly-in-string
timeout: 30000,
// replaces remoteRoot with localRoot to get src files
localRoot: rtLibUtils._absApp(root, '.'),
remoteRoot: '/code',
outputCapture: 'std',
attachSimplePort: 0
}

const actionFileStats = fs.lstatSync(actionPath)
if (actionFileStats.isFile()) {
// why is this condition here?
}
config.runtimeArgs = [
`${packageName}/${an}`,
actionPath,
'-v'
]
if (actionFileStats.isDirectory()) {
// take package.json.main or 'index.js'
const zipMain = rtLibUtils.getActionEntryFile(path.join(actionPath, 'package.json'))
config.runtimeArgs[1] = path.join(actionPath, zipMain)
}
if (action.annotations && action.annotations['require-adobe-auth'] && ow.apihost === 'https://adobeioruntime.net') {
// NOTE: The require-adobe-auth annotation is a feature implemented in the
// runtime plugin. The current implementation replaces the action by a sequence
// and renames the action to __secured_<action>. The annotation will soon be
// natively supported in Adobe I/O Runtime, at which point this condition won't
// be needed anymore.
/* instanbul ignore next */
config.runtimeArgs[0] = `${packageName}/__secured_${an}`
}
if (action.runtime) {
config.runtimeArgs.push('--kind')
config.runtimeArgs.push(action.runtime)
}
return config
})

return [actionConfigNames, actionConfigs]
}

/** @private */
async function generateConfig (appConfig, props) {
const { web } = appConfig
const { hasFrontend, withBackend, frontEndUrl } = props

let actionConfigNames = []
let actionConfigs = []

if (withBackend) {
const modifiedConfig = cloneDeep(appConfig)
const packages = modifiedConfig.manifest.full.packages
const packagePlaceholder = modifiedConfig.manifest.packagePlaceholder
if (packages[packagePlaceholder]) {
packages[modifiedConfig.ow.package] = packages[packagePlaceholder]
delete packages[packagePlaceholder]
}

Object.keys(packages).forEach(pkg => {
const packageConfigs = processPackageActionConfigs(modifiedConfig, pkg, packages[pkg])
// merge the arrays
actionConfigNames = [...actionConfigNames, ...packageConfigs[0]]
actionConfigs = [...actionConfigs, ...packageConfigs[1]]
})
}

const debugConfig = {
configurations: actionConfigs,
compounds: [{
name: 'Actions',
configurations: actionConfigNames
}]
}

if (hasFrontend) {
debugConfig.configurations.push({
type: 'chrome',
request: 'launch',
name: 'Web',
url: frontEndUrl,
webRoot: web.src,
breakOnLoad: true,
sourceMapPathOverrides: {
'*': path.join(web.distDev, '*')
}
})
debugConfig.compounds.push({
name: 'WebAndActions',
configurations: ['Web'].concat(actionConfigNames)
})
}

return debugConfig
}

module.exports = (config) => ({
update: update(config),
cleanup: cleanup(config)
Expand Down
Loading

0 comments on commit 7eacde2

Please sign in to comment.