UNPKG

rdme

Version:

ReadMe's official CLI and GitHub Action.

55 lines (54 loc) 2.69 kB
import { Flags } from '@oclif/core'; import chalk from 'chalk'; import configstore from '../configstore.js'; import { keyFlag } from '../flags.js'; import getCurrentConfig from '../getCurrentConfig.js'; import isCI, { isTest } from '../isCI.js'; import { info } from '../logger.js'; import loginFlow from '../loginFlow.js'; const hook = async function run(options) { this.debug('configstore location:', configstore.path); if (options.Command?.flags?.key) { this.debug('current command has --key flag'); if (isTest()) { options.Command.flags.key = keyFlag; } else { options.Command.flags.key = Flags.string({ // `parse` is run if the user passes in a `--key` flag parse: async (input) => { this.debug('--key flag detected in parse function'); const { email, project } = getCurrentConfig.call(this); // We only want to log this if the API key is stored in the configstore, **not** in an env var. if (input && configstore.get('apiKey') === input) { info(`🔑 ${chalk.green(email)} is currently logged in, using the stored API key for this project: ${chalk.blue(project)}`, { includeEmojiPrefix: false }); } return input; }, // `default` is run if no `--key` flag is passed default: async () => { this.debug('no --key flag detected, running default function'); const { apiKey } = getCurrentConfig.call(this); // if the user is passing an API key via env var or configstore, use that if (apiKey) { this.debug('api key found in config, returning'); return apiKey; } if (isCI()) { throw new Error('No project API key provided. Please use `--key`.'); } // if in non-CI and the user hasn't passed in a key, we prompt them to log in info("Looks like you're missing a ReadMe API key, let's fix that! 🦉", { includeEmojiPrefix: false }); const result = await loginFlow.call(this); info(result, { includeEmojiPrefix: false }); // loginFlow sets the configstore value, so let's use that return configstore.get('apiKey'); }, }); } } else { this.debug('current command does not have --key flag'); } }; export default hook;