UNPKG

@ajhenry/stack

Version:

A CLI to bootstrap dev environments lightning fast ⚡

130 lines (129 loc) 5.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const find_up_1 = tslib_1.__importDefault(require("find-up")); const js_yaml_1 = tslib_1.__importDefault(require("js-yaml")); const url_join_1 = tslib_1.__importDefault(require("url-join")); const json5_1 = tslib_1.__importDefault(require("json5")); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const rest_1 = require("@octokit/rest"); const logger_1 = tslib_1.__importDefault(require("../logger")); const axios_1 = tslib_1.__importDefault(require("axios")); const stacks_1 = require("../stacks"); class Parser { constructor() { this.stackFiles = [".stack", ".stack.yaml", ".stack.yml", ".stack.json"]; } find(path) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (path) { const contents = yield this.read(path); this.stack = this.parse(contents); return; } const dir = yield find_up_1.default(this.stackFiles, { cwd: process.cwd() }); logger_1.default.debug(`Found a file at ${dir}`); if (!dir) { throw new Error("can't find stack file"); } const contents = yield this.read(dir); this.stack = this.parse(contents); }); } useCommonStack(project, commonType) { return tslib_1.__awaiter(this, void 0, void 0, function* () { logger_1.default.debug(`Using common stack: ${commonType}`); if (commonType === "npm-start") { const stackFile = stacks_1.npmStart(project, false); return this.parse(stackFile); } if (commonType === "yarn-start") { const stackFile = stacks_1.npmStart(project, true); return this.parse(stackFile); } throw new Error(`Not a supported common stack: ${commonType}`); }); } read(file) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const contents = yield fs_extra_1.default.readFile(file); return contents.toString(); }); } parse(contents) { let data = undefined; logger_1.default.debug(contents); try { data = js_yaml_1.default.load(contents); } catch (e) { logger_1.default.debug("failed to parse yaml"); } try { data = json5_1.default.parse(contents); } catch (e) { logger_1.default.debug("failed to parse json"); } if (!data) { throw new Error("Failed to parse stack file"); } return data; } readFile(path) { return tslib_1.__awaiter(this, void 0, void 0, function* () { yield this.find(path); return this.stack; }); } getDefaultBranch(project) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const [owner, repo] = project.split("/"); const gh = new rest_1.Octokit(); const data = yield gh.repos.get({ owner, repo, }); return data.data.default_branch; }); } generateGitHubLink(project, branch, path) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const links = project.split("/"); const githubUrlBase = `https://raw.githubusercontent.com/${links[0]}/${links[1]}/`; const githubUrlBranch = url_join_1.default(githubUrlBase, branch !== null && branch !== void 0 ? branch : (yield this.getDefaultBranch(project))); for (const file of this.stackFiles) { const githubUrlPath = url_join_1.default(githubUrlBranch, path !== null && path !== void 0 ? path : file); logger_1.default.debug(`Checking ${githubUrlPath}`); const data = yield this.readGitHubFile(githubUrlPath); logger_1.default.debug(`data: ${data}`); if (data) return data; } throw new Error("Unable to find a stack file within that project"); }); } readGitHubFile(url) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const res = yield axios_1.default .get(url) .then((data) => { return data.data; }) .catch((err) => { logger_1.default.debug("Threw an error in readGitHubFile"); logger_1.default.debug(`Error code: ${err.response.status}`); return undefined; }); return res; }); } readGitHub(project, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const data = yield this.generateGitHubLink(project, options === null || options === void 0 ? void 0 : options.branch, options === null || options === void 0 ? void 0 : options.path); const stackFile = this.parse(data); return stackFile; }); } } exports.default = Parser;