UNPKG

@salesforce/plugin-release-management

Version:
132 lines 4.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CircleCiEnvvars = void 0; /* * Copyright (c) 2020, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ const fs_1 = require("fs"); const os_1 = require("os"); const command_1 = require("@salesforce/command"); const kit_1 = require("@salesforce/kit"); const core_1 = require("@salesforce/core"); const ts_types_1 = require("@salesforce/ts-types"); const got_1 = require("got"); const chalk_1 = require("chalk"); const URL_BASE = 'https://circleci.com/api/v2/project'; class CircleCiEnvvars extends command_1.SfdxCommand { constructor() { super(...arguments); this.envvarValues = {}; } get headers() { const token = kit_1.env.getString('CIRCLE_CI_TOKEN'); if (!token) { throw new core_1.SfError('The environment variable "CIRCLE_CI_TOKEN" is required.'); } return { 'Circle-Token': token, }; } async resolveEnvvarValues() { for (const envvarName of this.getFlagAsArray('envvar')) { const envvarValue = kit_1.env.getString(envvarName); if (envvarValue) { this.envvarValues[envvarName] = envvarValue; } else if (!(await this.isPipedIn())) { this.envvarValues[envvarName] = await this.ux.prompt(envvarName, { type: 'mask' }); } else { throw new core_1.SfError(`missing envvar value for ${envvarName}`); } } } async resolveSlugs() { let slugs = []; if (await this.isPipedIn()) { const input = await this.readPipedInput(); try { const json = JSON.parse(input); if ((0, ts_types_1.isArray)(json)) { slugs = json; } else if ((0, ts_types_1.isArray)((0, ts_types_1.getArray)(json, 'result', null))) { slugs = (0, ts_types_1.getArray)(json, 'result', []); } } catch (error) { slugs = input.split('\n'); } } slugs = [...slugs, ...this.getFlagAsArray('slug')]; if (!slugs) { throw new core_1.SfError('missing input slugs'); } return slugs.filter((slug) => !!slug); } async getCircleCiEnvvars(slug) { const response = await got_1.default.get(`${URL_BASE}/${slug}/envvar`, { headers: this.headers }); const body = JSON.parse(response.body); return body.items; } // These methods are to support piping. When OCLIF supports piping, this can be removed. async isPipedIn() { return new Promise((resolve) => { (0, fs_1.fstat)(0, (err, stats) => { if (err) resolve(false); else resolve(stats.isFIFO()); }); }); } async readPipedInput() { const isPiped = await this.isPipedIn(); return new Promise((resolve, reject) => { if (!isPiped) reject(); else { const stdin = process.stdin; stdin.setEncoding('utf-8'); let data = ''; stdin.on('data', (chunk) => { data += chunk; }); stdin.on('end', () => { resolve(data); }); } }); } getFlagAsArray(name) { const value = this.flags[name]; if ((0, ts_types_1.isArray)(value)) { return value; } else if (value) { return [value]; } else { return []; } } printStatus(slug, status) { let message = (0, chalk_1.bold)((0, chalk_1.cyan)(slug)); if ((0, ts_types_1.isString)(status)) { message += `: ${(0, chalk_1.red)(status)}`; } else { message += os_1.EOL + status .map((s) => s.success ? ` ${(0, chalk_1.green)('✔')} ${(0, chalk_1.bold)(s.name)}` : ` ${(0, chalk_1.red)('✘')} ${(0, chalk_1.bold)(s.name)}: ${(0, chalk_1.red)(s.message)}`) .join(os_1.EOL); } this.ux.log(message); } } exports.CircleCiEnvvars = CircleCiEnvvars; //# sourceMappingURL=circleCiEnvvars.js.map