pivotal-flow
Version:
🔀 A command-line tool that helps you manage & automate your workflow to use with PivotalTracker.
126 lines (124 loc) • 5.19 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const os_1 = require("os");
const path_1 = require("path");
const cosmiconfig_1 = require("cosmiconfig");
// other utils
const inquirer_1 = __importDefault(require("../../utils/inquirer"));
const console_1 = require("../../utils/console");
const client_1 = __importDefault(require("../../utils/pivotal/client"));
const fs_1 = require("../../utils/fs");
// constants & types
const constants_1 = require("./constants");
const questions_1 = require("./questions");
/**
* Searches for pivotal flow config and returns a config if found or returns undefined
*/
exports.getPivotalFlowConfig = () => __awaiter(void 0, void 0, void 0, function* () {
const explorer = cosmiconfig_1.cosmiconfig(constants_1.COSMICONFIG_MODULE_NAME, {
searchPlaces: constants_1.COSMICONFIG_SEARCH_PLACES,
});
try {
const result = (yield explorer.search());
if (result && !result.isEmpty) {
return result.config;
}
return null;
}
catch (e) {
console_1.error(`Some error occurred while creating the config file, Please try again!. ${e}`);
return null;
}
});
/**
* Checks for config object and required configs are exits or not
*/
exports.isSetupComplete = () => __awaiter(void 0, void 0, void 0, function* () {
const config = yield exports.getPivotalFlowConfig();
if (config) {
const { pivotalApiToken, projects } = config;
return Boolean(pivotalApiToken && projects && projects.length >= 1);
}
return false;
});
/**
* create and returns a config object for a given projectDetails
* @param {PivotalProjectResponse} projectDetails
* @param apiToken
*/
exports.getBasicConfiguration = (projectDetails, apiToken) => {
const { name, id } = projectDetails;
return { projects: [{ name, id }], pivotalApiToken: apiToken };
};
/**
* creates a config file at user's home directory to store pivotal projects and API token
* @param projectId
* @param apiToken
*/
exports.createPivotalFlowConfigFile = (projectId, apiToken) => __awaiter(void 0, void 0, void 0, function* () {
const client = new client_1.default({ projectId, apiToken });
const projectDetails = yield client.getProject();
const config = exports.getBasicConfiguration(projectDetails, apiToken);
const filePath = path_1.join(os_1.homedir(), constants_1.DEFAULT_CONFIG_FILE_NAME);
yield fs_1.writeFile(filePath, JSON.stringify(config, null, 2), { encoding: 'utf8' });
console_1.log(chalk_1.default `
A basic configuration file has been created for you in your home directory:-
{bold ${filePath}}
Find more details about addding/changing the configuration options here:
{underline https://github.com/ClearTax/pivotal-flow#configuration}
`);
process.exit(0);
});
/**
* Collects user input & displays setup instructions accordingly & creates a config file with given projectId.
*/
exports.performSetup = () => __awaiter(void 0, void 0, void 0, function* () {
if (!(yield exports.isSetupComplete())) {
const answers = yield inquirer_1.default.prompt(questions_1.SetupQuestions);
const { pivotalProjectId, pivotalToken } = answers;
try {
yield exports.createPivotalFlowConfigFile(pivotalProjectId, pivotalToken);
}
catch (e) {
console_1.error(`Failed to set-up pivotal-flow. Please try again. ${e}`);
}
}
else {
console_1.log(chalk_1.default `
{bold Looks like your setup is ready!}
{bold You can start creating stories by running: 'pivotal-flow start'}
`);
}
});
/**
* Aborts if pivotal-flow has not been set-up. Displays setup instructions before aborting.
*/
exports.abortIfNotSetup = () => __awaiter(void 0, void 0, void 0, function* () {
if (!(yield exports.isSetupComplete())) {
const { wantSetup } = yield inquirer_1.default.prompt(questions_1.PromptToSetup);
if (wantSetup) {
yield exports.performSetup();
}
else {
console_1.error(`Setup for pivotal-flow incomplete. Please try again after running 'pivotal-flow init'.`);
}
// return non-zero error code when set-up is not completed.
process.exit(wantSetup ? 0 : 1);
}
else {
return;
}
});