UNPKG

@adinsure-ops/ops-cli

Version:

Operations CLI for working with AdInsure

97 lines (96 loc) 4.66 kB
import { __awaiter } from "tslib"; import { Command } from '@oclif/core'; import axios from 'axios'; import chalk from 'chalk'; import fs from 'fs-extra'; import path from 'path'; import SecurityContext from './security/context.js'; /* eslint-disable-next-line @typescript-eslint/no-unused-vars */ export default class CommandBase extends Command { constructor(argv, config) { super(argv, config); this.security = new SecurityContext(this.config.configDir); } getGitRoot(startDir) { return __awaiter(this, void 0, void 0, function* () { let currentPath = path.resolve(startDir !== null && startDir !== void 0 ? startDir : process.cwd()); /* eslint-disable-next-line no-constant-condition */ while (true) { // We've reached the root and didn't find .git if (currentPath === path.parse(currentPath).root) { throw new Error('Not a git repository (or any of the parent directories)'); } if (yield fs.pathExists(path.resolve(currentPath, '.git'))) { // toLowerCase because of different behaviour when executing tests with yarn or with Test Explorer in VS Code return currentPath.toLowerCase(); } currentPath = path.dirname(currentPath); } }); } getVersion() { return __awaiter(this, arguments, void 0, function* (versionFile = 'VERSION', ...filesToCheck) { filesToCheck.unshift(versionFile); const gitRoot = yield this.getGitRoot(); for (const file of filesToCheck) { const versionPath = path.resolve(gitRoot, file); if (yield fs.pathExists(versionPath)) { return fs.readFileSync(versionPath, 'utf8').trim(); } } return this.error(`${filesToCheck} file was not found`, { exit: 2 }); }); } /** * Resolve path to Changelog folder from current execution directory * @returns {string} the resolved path to changelogs */ getChangelogsRoot() { return __awaiter(this, void 0, void 0, function* () { const currentPath = path.resolve(process.cwd()); if (!(yield fs.pathExists(currentPath))) { this.log(`Directory '${currentPath}' doesn't exist ... nothing to do here.`); this.exit(0); } return path.resolve(currentPath, 'changelogs', 'unreleased'); }); } /** * Get all files inside directory and normalize it against {baseRelativePath}. * @param {string} dir The name or path of the current directory * @param {string} baseRelativePath The relatice path format from the `dir` * @param {RegExp} filter Regular expressions filter for the file path * @returns {string[]} An array of files inside the directory with the specified filter */ getFilesRecursive(dir, baseRelativePath, filter) { return __awaiter(this, void 0, void 0, function* () { if (!(yield fs.pathExists(dir))) { this.warn(`Directory '${dir}' doesn't exist ... nothing to do here.`); return []; } let relativePath = path.relative(baseRelativePath, dir); relativePath = relativePath === '' ? '.' : relativePath; const entries = yield fs.readdir(dir); // Get files within the current directory and add a relative path to the file objects const files = entries .filter(file => !fs.statSync(path.resolve(dir, file)).isDirectory()) .filter(file => filter.test(path.resolve(dir, file))) .map(file => `${relativePath}/${file}`); // Get folders within the current directory const folders = entries.filter(filetype => fs.statSync(path.resolve(dir, filetype)).isDirectory()); for (const folder of folders) // Recursivly call this function and get relative paths of files inside directory files.push(...yield this.getFilesRecursive(path.join(dir, folder), baseRelativePath, filter)); return files; }); } // eslint-disable-next-line @typescript-eslint/no-explicit-any run() { return __awaiter(this, void 0, void 0, function* () { axios.interceptors.request.use(req => { this.debug(`${chalk.bgCyan('DEBUG: ')} Request that was sent with your command:\n${JSON.stringify(req, null, 2)}`); return req; }); }); } }