All files / src index.js

100% Statements 212/212
100% Branches 76/76
100% Functions 10/10
100% Lines 212/212

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 232 233 234 235 236 237 238 239 240 241 242 243 244 2451x       1x 22x 22x 22x 22x 5x 2x 2x 2x 2x 2x 2x 3x 3x 22x   1x 20x 20x 20x 20x 3x 2x 2x 2x 2x 2x 2x 2x 1x 1x 20x   1x 17x 17x 17x 17x   44x 44x 44x 1x 1x 1x 1x 1x   43x 44x 1x 1x 1x 1x 1x   44x 44x 1x 1x 1x 1x 1x   41x 44x 1x 1x 1x 1x 1x   40x 40x   1x 24x 24x 3x 3x   21x 21x   1x 23x 23x   23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x   1x 17x 17x   1x 8x 8x 8x 4x 4x 3x 3x 3x 3x 3x 3x 1x 1x 8x   1x 7x 7x 7x 7x 7x   1x 20x   20x 20x 20x 20x   20x 20x 20x 20x 20x 9x 9x 8x 8x 8x   8x 8x 9x 9x   9x 9x   9x 2x 2x 2x 2x 2x 2x 2x   7x 7x 7x 7x 9x 3x 9x 4x 4x   9x 3x 3x 3x 3x 9x 1x 1x 1x 20x   20x 20x 20x 20x 20x 9x 9x 8x 8x 8x   8x 8x 9x 9x   9x 9x   9x   9x 4x 4x 4x 4x 4x 2x 2x 2x 4x   8x 8x 9x 9x 9x 9x 9x 5x 5x 5x 9x 1x 1x 1x 20x   20x 20x  
import { writeFileSync, existsSync } from 'node:fs'
import { Command } from 'commander'
import { execa } from 'execa'
 
export async function checkAioCli () {
  console.error('$ aio --version')
  try {
    await execa('aio', ['--version'])
  } catch (err) {
    if (err.code === 'ENOENT') {
      throw new Error(
        'aio CLI is not installed.\n\n' +
        'To install it, run:\n' +
        '  npm install -g @adobe/aio-cli'
      )
    }
    throw err
  }
}
 
export async function checkGhCli () {
  console.error('$ gh --version')
  try {
    await execa('gh', ['--version'])
  } catch (err) {
    if (err.code === 'ENOENT') {
      throw new Error(
        'gh CLI is not installed.\n\n' +
        'To install it, run:\n' +
        '  brew install gh\n\n' +
        'Or visit: https://cli.github.com for other installation options.'
      )
    }
    throw err
  }
}
 
export async function fetchAioConfig () {
  console.error('$ aio config ls --json')
  const { stdout } = await execa('aio', ['config', 'ls', '--json'])
  return JSON.parse(stdout)
}
 
function getServerToServerImsContext (config) {
  const credentials = config?.project?.workspace?.details?.credentials
  if (!Array.isArray(credentials)) {
    throw new Error(
      'Your Adobe Developer App configuration is missing workspace credentials.\n\n' +
      'Run `aio config ls --json` to inspect the active workspace and ensure it includes an OAuth Server-to-Server credential.'
    )
  }
 
  const credential = credentials.find(c => c.integration_type === 'oauth_server_to_server')
  if (!credential) {
    throw new Error(
      'No OAuth Server-to-Server credential was found in the active workspace.\n\n' +
      'Update your Adobe Developer App to include an OAuth Server-to-Server credential, then try again.'
    )
  }
 
  const ctx = credential.name?.toLowerCase()
  if (!ctx) {
    throw new Error(
      'The OAuth Server-to-Server credential in the active workspace is missing a name.\n\n' +
      'Recreate or repair the credential in Adobe Developer Console, then try again.'
    )
  }
 
  const imsCtx = config?.ims?.contexts?.[ctx]
  if (!imsCtx) {
    throw new Error(
      `The IMS context "${ctx}" for the OAuth Server-to-Server credential was not found in your local aio configuration.\n\n` +
      'Run `aio config ls --json` to verify the active configuration, or re-run `aio app use` / `aio console org select` to refresh it.'
    )
  }
 
  return { ctx, imsCtx }
}
 
export function validateConfig (config) {
  const services = config?.project?.workspace?.details?.services
  if (!Array.isArray(services) || !services.some(s => s.code === 'AdobeIOManagementAPISDK')) {
    throw new Error('I/O Management API was not found in your workspace.')
  }
 
  getServerToServerImsContext(config)
}
 
export function buildEnvVars (config, { noSuffix = false } = {}) {
  const { imsCtx } = getServerToServerImsContext(config)
  const suffix = noSuffix ? '' : (config.project.workspace.name === 'Production' ? '_PROD' : '_STAGE')
 
  return {
    [`CLIENTID${suffix}`]: imsCtx.client_id,
    [`CLIENTSECRET${suffix}`]: (Array.isArray(imsCtx.client_secrets) ? imsCtx.client_secrets : JSON.parse(imsCtx.client_secrets))[0],
    [`TECHNICALACCID${suffix}`]: imsCtx.technical_account_id,
    [`TECHNICALACCEMAIL${suffix}`]: imsCtx.technical_account_email,
    [`IMSORGID${suffix}`]: imsCtx.ims_org_id,
    [`SCOPES${suffix}`]: (Array.isArray(imsCtx.scopes) ? imsCtx.scopes : String(imsCtx.scopes).split(',')).join(','),
    [`AIO_RUNTIME_NAMESPACE${suffix}`]: config.runtime.namespace,
    [`AIO_RUNTIME_AUTH${suffix}`]: config.runtime.auth,
    [`AIO_PROJECT_ID${suffix}`]: config.project.id,
    [`AIO_PROJECT_NAME${suffix}`]: config.project.name,
    [`AIO_PROJECT_ORG_ID${suffix}`]: config.project.org.id,
    [`AIO_PROJECT_WORKSPACE_ID${suffix}`]: config.project.workspace.id,
    [`AIO_PROJECT_WORKSPACE_NAME${suffix}`]: config.project.workspace.name,
    [`AIO_PROJECT_WORKSPACE_DETAILS_SERVICES${suffix}`]: JSON.stringify(config.project.workspace.details.services)
  }
}
 
export function formatEnvVars (envVars) {
  return Object.entries(envVars).map(([k, v]) => `${k}=${v}`).join('\n')
}
 
export async function createGhEnvironment (envName) {
  console.error(`$ gh api -X PUT repos/{owner}/{repo}/environments/${envName}`)
  try {
    await execa('gh', ['api', '-X', 'PUT', `repos/{owner}/{repo}/environments/${envName}`])
  } catch (err) {
    if (err.stderr?.includes('404') || err.message?.includes('404')) {
      throw new Error(
        `Failed to create environment '${envName}' (HTTP 404).\n\n` +
        'You may be authenticated as the wrong GitHub user. Try:\n' +
        '  gh auth switch'
      )
    }
    throw err
  }
}
 
export async function uploadSecrets (output, { envName = null } = {}) {
  const args = ['secret', 'set', '-f', '-']
  if (envName) args.push('--env', envName)
  console.error(`$ gh ${args.join(' ')}`)
  await execa('gh', args, { input: output })
}
 
export function createCli () {
  const program = new Command()
 
  program
    .name('ab-app-secrets-uploader')
    .description('CLI tool for uploading App Builder app secrets')
    .version('0.1.0')
 
  program
    .command('create-env <output-file>')
    .description('Fetch secrets from aio config and write to a file with upload instructions')
    .option('--no-suffix', 'omit the _PROD/_STAGE suffix from env var names')
    .action(async (outputFile, options) => {
      try {
        await checkAioCli()
        await checkGhCli()
        const config = await fetchAioConfig()
        validateConfig(config)
 
        const noSuffix = !options.suffix
        const envName = noSuffix
          ? (config.project.workspace.name === 'Production' ? 'production' : 'stage')
          : null
 
        const envVars = buildEnvVars(config, { noSuffix })
        const output = formatEnvVars(envVars)
 
        if (existsSync(outputFile)) {
          const { confirm } = await import('@inquirer/prompts')
          const overwrite = await confirm({
            message: `'${outputFile}' already exists. Overwrite?`,
            default: false
          })
          if (!overwrite) return
        }
 
        writeFileSync(outputFile, output, { mode: 0o600 })
        console.error(`Environment variables written to ${outputFile}`)
        console.error('')
        console.error('To upload these secrets to a GitHub repository, run:')
        if (noSuffix) {
          console.error(`  gh secret set -f ${outputFile} --env ${envName}`)
        } else {
          console.error(`  gh secret set -f ${outputFile}`)
        }
 
        if (noSuffix) {
          console.error('')
          console.error(`To create the '${envName}' GitHub environment, run:`)
          console.error(`  gh api -X PUT repos/{owner}/{repo}/environments/${envName}`)
        }
      } catch (err) {
        console.error(`Error: ${err.message}`)
        process.exit(1)
      }
    })
 
  program
    .command('upload')
    .description('Fetch secrets from aio config and upload directly as GitHub secrets')
    .option('--no-suffix', 'omit the _PROD/_STAGE suffix from env var names')
    .action(async (options) => {
      try {
        await checkAioCli()
        await checkGhCli()
        const config = await fetchAioConfig()
        validateConfig(config)
 
        const noSuffix = !options.suffix
        const envName = noSuffix
          ? (config.project.workspace.name === 'Production' ? 'production' : 'stage')
          : null
 
        const envVars = buildEnvVars(config, { noSuffix })
        const output = formatEnvVars(envVars)
 
        const { confirm } = await import('@inquirer/prompts')
 
        if (noSuffix) {
          const shouldCreate = await confirm({
            message: `Create '${envName}' environment in this GitHub repo?`,
            default: false
          })
          if (shouldCreate) {
            await createGhEnvironment(envName)
            console.error(`Environment '${envName}' created (or already existed).`)
          }
        }
 
        const shouldUpload = await confirm({
          message: noSuffix
            ? `Upload secrets to the '${envName}' environment in this GitHub repo?`
            : 'Upload secrets to this GitHub repo?',
          default: false
        })
        if (shouldUpload) {
          await uploadSecrets(output, { envName })
          console.error('Secrets uploaded successfully.')
        }
      } catch (err) {
        console.error(`Error: ${err.message}`)
        process.exit(1)
      }
    })
 
  return program
}