@@ -19,73 +19,74 @@ const { getCliEnv } = require('@adobe/aio-lib-env')
1919const RuntimeBaseCommand = require ( './RuntimeBaseCommand' )
2020
2121class 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
9192DeployServiceCommand . flags = {
0 commit comments