UNPKG

@contract-case/cli

Version:

Command-line interface for ContractCase, allowing interactions with the Contract / Pact Broker

77 lines 3.69 kB
import { program } from 'commander'; import { handleError, downloadContracts, canDeploy, } from '../connectors/index.js'; import { versionString } from '../entities/versionString.js'; process.env['CASE_CONNECTOR_CLIENT'] = 'contract-case-cli'; const mapConfig = (options) => { // This function is full of type assertions because the case core does the actual validation. // TODO: Replace the boundary config type with one that accepts unknowns and // undefined so this abomination can go away if (options != null && typeof options === 'object') { return { ...('contractDir' in options && typeof options.contractDir === 'string' ? { contractDir: options.contractDir } : {}), ...('logLevel' in options && typeof options.logLevel === 'string' ? { logLevel: options.logLevel } : {}), ...('brokerCiAccessToken' in options ? { brokerCiAccessToken: options.brokerCiAccessToken } : {}), ...('brokerBaseUrl' in options ? { brokerCiAccessToken: options.brokerBaseUrl } : {}), ...('brokerPassword' in options || 'brokerUsername' in options ? { brokerBasicAuth: { ...{ username: 'brokerUsername' in options ? options.brokerUsername : undefined, password: 'brokerPassword' in options ? options.brokerPassword : undefined, }, }, } : {}), }; } return {}; }; program .name('ContractCase') .description('CLI to interact with contract files and contract brokers') .version(`${versionString}`); const commonOptions = (command) => command .option('-l, --log-level <level>', 'log level') .option('--broker-ci-access-token', 'broker CI token (recommended over username + password)') .option('--broker-base-url <baseurl>', 'broker base URL') .option('--broker-username <username>', 'broker basic auth username. Do not supply this if you are also using a broker CI token. Must be supplied with --broker-password') .option('--broker-password <password>', 'broker basic auth password. Do not supply this if you are also using a broker CI token. Must be supplied with --broker-username'); commonOptions(program .command('download-contracts') .description('download contracts from a broker') .argument('<provider-name>', 'Name of the provider to download contracts for') .option('--contract-dir <dir>', 'directory to download contracts to')).action((providerName, options) => Promise.resolve() .then(() => downloadContracts(providerName, mapConfig(options))) .then(() => { process.exit(0); }, (e) => { handleError(e); process.exit(1); })); commonOptions(program .command('can-deploy') .description('Ask the broker if it is safe to deploy') .argument('<service-name>', 'Name of the service to check') .requiredOption('-e, --environment <environment>', 'environment to check against') .option('--override-version <version>', 'override the version detection and instead query this specific version')).action((providerName, options) => Promise.resolve() .then(() => canDeploy(providerName, options.environment, mapConfig(options))) .then(() => { process.exit(0); }, (e) => { handleError(e); process.exit(1); })); program.parse(); //# sourceMappingURL=cli.js.map