UNPKG

branch-remover

Version:

A small application for quickly removing unnecessary branches from GitHub.

107 lines 4.28 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const parse_duration_1 = __importDefault(require("parse-duration")); const trim_character_1 = __importDefault(require("trim-character")); const BranchRemover_1 = require("./BranchRemover"); const BranchRemoverOptionsBuilder_1 = require("./BranchRemoverOptionsBuilder"); const CommandLine_1 = require("./CommandLine"); const GitHubProvider_1 = require("./Providers/GitHubProvider"); if (CommandLine_1.params.version) { // TODO: PackageInfo service + Tests const { version } = require('../package.json'); console.log(`v${version}`); process.exit(); } if (CommandLine_1.params.help) { CommandLine_1.helpCommand(); } let options; let provider; // TODO: Service + Tests const normalizeParameterValue = (value) => { return trim_character_1.default(trim_character_1.default(value.trim(), '\'', 'g'), '"', 'g'); }; switch (CommandLine_1.params.provider.toLowerCase()) { case 'github': { if (!CommandLine_1.params['github']) { throw new Error('Expects parameter github.auth or parameters github.owner, github.repo and github.token.'); } let auth = null; if (CommandLine_1.params['github']['auth']) { auth = require(normalizeParameterValue(CommandLine_1.params['github']['auth'])); } else { const requiredParams = [ 'token', 'owner', 'repo', ]; requiredParams.forEach((x) => { if (!CommandLine_1.params['github'][x]) { throw new Error(`"github.${x}" is required. The value must not be empty.`); } }); auth = CommandLine_1.params['github']; } provider = new GitHubProvider_1.GitHubProvider(auth); break; } default: throw Error(`Unknown provider "${CommandLine_1.params.provider}".`); } if (CommandLine_1.params.config) { options = require(CommandLine_1.params.config); } else { const now = new Date(); const builder = new BranchRemoverOptionsBuilder_1.BranchRemoverOptionsBuilder(); if (CommandLine_1.params.quiet) { builder.quiet(); } if (normalizeParameterValue(CommandLine_1.params.merged) !== 'all') { const milliseconds = parse_duration_1.default(normalizeParameterValue(CommandLine_1.params.merged)); builder.merged(new Date(now.getTime() - milliseconds)); } if (CommandLine_1.params.stale) { const milliseconds = parse_duration_1.default(normalizeParameterValue(CommandLine_1.params.stale)); builder.stale(new Date(now.getTime() - milliseconds)); } if (CommandLine_1.params.yes) { builder.yes(); } if (CommandLine_1.params.details) { builder.details(); } if (CommandLine_1.params.before) { builder.beforeRemove(normalizeParameterValue(CommandLine_1.params.before)); } if (CommandLine_1.params.after) { builder.afterRemove(normalizeParameterValue(CommandLine_1.params.after)); } if (CommandLine_1.params.cache) { // TODO: understand at what level it is better to implement the parsing of cache parameters // consider implementing inside BranchRemoverOptionsBuilder or alternative solution const cacheParams = /(?<path>[^\s]*)\s*((timeout=(?<timeout>\d+))|)/g.exec(normalizeParameterValue(CommandLine_1.params.cache)); if (cacheParams.groups['path']) { builder.cachePath(cacheParams.groups['path']); } if (cacheParams.groups['timeout']) { // TODO: Use parse-duration builder.cacheTimeout(parseInt(cacheParams.groups['timeout'], 10)); } } if (CommandLine_1.params.ignore) { builder.ignore(normalizeParameterValue(CommandLine_1.params.ignore)); } if (CommandLine_1.params.debug) { builder.debug(); } options = builder.build(); } const remover = new BranchRemover_1.BranchRemover(provider); remover.execute(options, CommandLine_1.params.test); //# sourceMappingURL=index.js.map