UNPKG

@ajhenry/stack

Version:

A CLI to bootstrap dev environments lightning fast ⚡

140 lines (139 loc) 5.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const shelljs_1 = tslib_1.__importDefault(require("shelljs")); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const command_exists_1 = tslib_1.__importDefault(require("command-exists")); const logger_1 = tslib_1.__importDefault(require("../logger")); const constants_1 = require("../constants"); class Runner { constructor(stack, path, options) { var _a; this.stack = stack; this.workingDir = path; this.options = options !== null && options !== void 0 ? options : {}; logger_1.default.setSettings({ minLevel: (_a = this.options.logLevel) !== null && _a !== void 0 ? _a : "info", }); } run() { return tslib_1.__awaiter(this, void 0, void 0, function* () { logger_1.default.debug("Runner:run"); yield this.checkRequires(); yield this.createWorkingDir(); try { yield this.install(); yield this.postinstall(); if (this.options.start) { yield this.start(); } } catch (e) { this.cleanUp(); throw e; } }); } install() { return tslib_1.__awaiter(this, void 0, void 0, function* () { logger_1.default.silly("Parser:install"); const steps = this.convertCommands(this.stack.install); Promise.all(steps.map(this.executeStep.bind(this))); }); } start() { return tslib_1.__awaiter(this, void 0, void 0, function* () { logger_1.default.silly("Parser:start"); const steps = this.convertCommands(this.stack.start); if (!steps) return; Promise.all(steps.map(this.executeStep.bind(this))); }); } postinstall() { return tslib_1.__awaiter(this, void 0, void 0, function* () { logger_1.default.silly("Parser:postinstall"); const steps = this.convertCommands(this.stack.postinstall); if (!steps) return; Promise.all(steps.map(this.executeStep.bind(this))); }); } convertCommands(steps) { logger_1.default.debug("Parser:convertCommands"); function convert(step) { if (typeof step === "string") { return { cmd: step, }; } if (step.hasOwnProperty("message")) { return { cmd: `echo ${step.message}`, }; } return step; } if (!steps) return steps; return steps.map(convert); } checkRequires() { return tslib_1.__awaiter(this, void 0, void 0, function* () { logger_1.default.debug("checkRequires"); for (const tool of this.stack.requires) { logger_1.default.debug(`Checking if ${tool} is installed`); if (yield command_exists_1.default(tool)) { logger_1.default.debug(`Success: ${tool} exists`); } else { logger_1.default.error(`${tool} not installed, cannot continue`); throw new Error(`Please install ${tool} to continue`); } } }); } createWorkingDir() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const pathExists = yield fs_extra_1.default.pathExists(this.workingDir); if (pathExists) { if (!this.options.overwrite) { logger_1.default.error(`Specified path already exists`); throw new Error("Specified path already exists"); } else { logger_1.default.debug("Deleting working directory since --overwrite is enabled"); shelljs_1.default.rm("-rf", this.workingDir); } } const status = shelljs_1.default.mkdir("-p", this.workingDir); if (status.code !== 0) { logger_1.default.error("Failed to create specified path"); throw new Error("Failed to create path"); } }); } executeStep(step) { return tslib_1.__awaiter(this, void 0, void 0, function* () { console.log(this.workingDir); const cdStatus = shelljs_1.default.cd(this.workingDir); if (cdStatus.code !== 0) { throw new Error(`Failed to cd into ${this.workingDir}`); } const status = shelljs_1.default.exec(step.cmd); if (status.code !== 0) { logger_1.default.error(`Failed to execute step`); logger_1.default.error(step.cmd); logger_1.default.error(`Failed with code ${status.code}`); throw new Error(`Failed to execute command`); } }); } cleanUp() { logger_1.default.silly("Runner:cleanUp"); shelljs_1.default.cd(constants_1.CWD); shelljs_1.default.rm("-rf", this.workingDir); logger_1.default.info("Cleaned up working directory"); } } exports.default = Runner;