Skip to content

Commit b48ef27

Browse files
committed
fix issues with env undefined, cloned options so a function doesn't mutate its inputs
1 parent aba23bc commit b48ef27

File tree

3 files changed

+41
-42
lines changed

3 files changed

+41
-42
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@
7070
},
7171
"repository": "adobe/aio-cli-plugin-runtime",
7272
"scripts": {
73-
"eslint-fix": "eslint src test e2e --fix",
74-
"posttest": "eslint src test e2e",
73+
"lint-fix": "eslint src test e2e --fix",
74+
"lint": "eslint src test e2e",
75+
"posttest": "npm run lint",
7576
"test": "npm run unit-tests",
7677
"unit-tests": "jest --ci",
7778
"prepack": "oclif manifest && oclif readme --no-aliases",

src/DeployServiceCommand.js

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,73 +19,74 @@ const { getCliEnv } = require('@adobe/aio-lib-env')
1919
const RuntimeBaseCommand = require('./RuntimeBaseCommand')
2020

2121
class DeployServiceCommand extends RuntimeBaseCommand {
22-
2322
/**
2423
* Retrieves an access token for Adobe I/O CLI authentication.
2524
* This function handles both CLI and custom contexts, setting up the appropriate
2625
* authentication context and retrieving the corresponding access token.
27-
*
26+
*
2827
* @async
2928
* @function getAccessToken
30-
* @param {Object} [options] - Options for token retrieval
31-
* @param {boolean} [options.useCachedToken=false] - Whether to use a cached token instead of requesting a new one
29+
* @param {object} [options] - Options for token retrieval
30+
* @param {string} [options.env] - The environment to use (e.g. 'prod', 'stage')
31+
* @param {boolean} [options.useCachedToken] - Whether to use a cached token instead of requesting a new one
3232
* @returns {Promise<{accessToken: string|null, env: string}>} An object containing:
3333
* - accessToken: The retrieved access token for authentication, or null if token retrieval failed
34-
* - env: The current CLI environment (e.g. 'prod', 'stage')
34+
* - env: The current CLI environment
3535
* @throws {Error} If token retrieval fails or context setup fails
3636
*/
37-
async getAccessToken({ useCachedToken = false } = {}) {
38-
const env = getCliEnv()
39-
let contextName = CLI // default
40-
const currentContext = await context.getCurrent() // potential override
37+
async getAccessToken ({ env = getCliEnv(), useCachedToken = false } = {}) {
38+
let contextName = CLI // default
39+
const currentContext = await context.getCurrent() // potential override
40+
41+
if (currentContext !== CLI) {
42+
contextName = currentContext
43+
} else {
44+
await context.setCli({ 'cli.bare-output': true }, false) // set this globally
45+
}
4146

42-
if (currentContext !== CLI) {
43-
contextName = currentContext
44-
} else {
45-
await context.setCli({ 'cli.bare-output': true }, false) // set this globally
46-
}
47+
let accessToken = null
48+
if (useCachedToken) {
49+
const contextConfig = await context.get(contextName)
50+
accessToken = contextConfig?.access_token?.token
51+
} else {
52+
accessToken = await getToken(contextName)
53+
}
4754

48-
let accessToken = null
49-
if (useCachedToken) {
50-
const contextConfig = await context.get(contextName)
51-
accessToken = contextConfig?.access_token?.token
52-
} else {
53-
accessToken = await getToken(contextName)
55+
return { accessToken, env }
5456
}
5557

56-
return { accessToken, env }
57-
}
58-
59-
6058
getAuthHandler () {
59+
const env = getCliEnv()
6160
return {
6261
getAuthHeader: async () => {
6362
this.debug(`Retrieving CLI Token using env=${env}`)
64-
const { accessToken } = await this.getAccessToken()
63+
const { accessToken } = await this.getAccessToken({ env })
6564

6665
return `Bearer ${accessToken}`
6766
}
6867
}
6968
}
7069

71-
async setRuntimeApiHostAndAuthHandler(options) {
72-
if (!options.useRuntimeAuth) {
70+
async setRuntimeApiHostAndAuthHandler (options) {
71+
let _options = structuredClone(options)
72+
if (!_options?.useRuntimeAuth) {
7373
const endpoint = process.env.AIO_DEPLOY_SERVICE_URL ?? PropertyDefault.DEPLOYSERVICEURL
74-
options.apihost = `${endpoint}/runtime`
75-
options.auth_handler = this.getAuthHandler()
74+
_options = _options ?? {}
75+
_options.apihost = `${endpoint}/runtime`
76+
_options.auth_handler = this.getAuthHandler()
7677
}
7778

78-
return options
79+
return _options
7980
}
8081

8182
async wsk (options) {
82-
if (!options) {
83-
options = await super.getOptions()
84-
options =await this.setRuntimeApiHostAndAuthHandler(options)
83+
let _options = structuredClone(options)
84+
if (!_options) {
85+
_options = await super.getOptions()
86+
_options = await this.setRuntimeApiHostAndAuthHandler(_options)
8587
}
86-
return runtimeLib.init(options)
88+
return runtimeLib.init(_options)
8789
}
88-
8990
}
9091

9192
DeployServiceCommand.flags = {

src/RuntimeBaseCommand.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ const debug = createDebug('aio-cli-plugin-runtime')
1818
const http = require('http')
1919
const runtimeLib = require('@adobe/aio-lib-runtime')
2020
const config = require('@adobe/aio-lib-core-config')
21-
const { getToken, context } = require('@adobe/aio-lib-ims')
22-
const { getCliEnv } = require('@adobe/aio-lib-env')
23-
const { CLI } = require('@adobe/aio-lib-ims/src/context')
2421

2522
/**
26-
* @typedef {Object} WskCreateOptions
23+
* @typedef {object} WskCreateOptions
2724
* @property {boolean} [useRuntimeAuth=false] - Whether to use Runtime authentication
28-
* @property {Object} [wskClientOptions] - The options to pass to the wsk client. If not provided, will be generated from getOptions()
25+
* @property {object} [wskClientOptions] - The options to pass to the wsk client. If not provided, will be generated from getOptions()
2926
*/
3027

3128
class RuntimeBaseCommand extends Command {

0 commit comments

Comments
 (0)