|
| 1 | +const path = require('path') |
| 2 | +const fs = require('fs') |
| 3 | +const cac = require('cac') |
| 4 | +const spawn = require('cross-spawn') |
| 5 | +const ora = require('ora') |
| 6 | +const matcher = require('multimatch') |
| 7 | +const kleur = require('kleur') |
| 8 | +const logger = require('../utils/logger') |
| 9 | +const parseArgs = require('../utils/parseArgs') |
| 10 | + |
| 11 | +module.exports = class Core { |
| 12 | + constructor() { |
| 13 | + this.cwd = process.cwd() |
| 14 | + this.rawArgs = process.argv |
| 15 | + this.args = parseArgs(this.rawArgs) |
| 16 | + this.isRemotes = this.args.has('r') || this.args.has('remotes') |
| 17 | + |
| 18 | + this.isGitProject() |
| 19 | + |
| 20 | + this.initCli() |
| 21 | + } |
| 22 | + |
| 23 | + initCli() { |
| 24 | + const cli = (this.cli = cac()) |
| 25 | + this.command = cli |
| 26 | + .command('[...tags]') |
| 27 | + .usage('[...tags] [options]') |
| 28 | + .option('-r, --remotes', 'Delete remotes tags') |
| 29 | + .action((tags, options) => { |
| 30 | + if (tags.length === 0) cli.outputHelp() |
| 31 | + |
| 32 | + this.deleteTag(tags, options) |
| 33 | + }) |
| 34 | + |
| 35 | + if (this.isRemotes) { |
| 36 | + cli.option('--scope <scope>', 'Remote tag scope', { |
| 37 | + default: 'origin' |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + cli.version(require('../package.json').version).help() |
| 42 | + |
| 43 | + this.cli.parse(this.rawArgs, { run: false }) |
| 44 | + } |
| 45 | + |
| 46 | + isGitProject() { |
| 47 | + if (!fs.existsSync(path.join(this.cwd, '.git'))) { |
| 48 | + throw new Error( |
| 49 | + logger.error('Current working directory is not a git project!') |
| 50 | + ) |
| 51 | + } |
| 52 | + return true |
| 53 | + } |
| 54 | + |
| 55 | + getTag(options) { |
| 56 | + const { stdout } = spawn.sync( |
| 57 | + 'git', |
| 58 | + this.isRemotes ? ['ls-remote', '--tags'] : ['tag'] |
| 59 | + ) |
| 60 | + |
| 61 | + let tags = [] |
| 62 | + |
| 63 | + tags = stdout |
| 64 | + .toString() |
| 65 | + .trimRight() |
| 66 | + .split('\n') |
| 67 | + |
| 68 | + if (this.isRemotes && options.scope) { |
| 69 | + tags = tags |
| 70 | + .map(tag => tag.match(/refs\/tags\/([\S]*)/)[1]) |
| 71 | + .filter(tag => !tag.includes('^{}')) |
| 72 | + } |
| 73 | + |
| 74 | + return tags |
| 75 | + } |
| 76 | + |
| 77 | + deleteTag(tags, options) { |
| 78 | + const matched = matcher(this.getTag(options), tags) |
| 79 | + |
| 80 | + matched.forEach(tag => { |
| 81 | + const spinner = ora(`Deleting${this.text(tag)}`) |
| 82 | + spinner.start() |
| 83 | + const args = this.isRemotes |
| 84 | + ? ['push', options.scope, `:refs/tags/${tag}`] |
| 85 | + : ['tag', tag, '-d'] |
| 86 | + const ps = spawn.sync('git', args) |
| 87 | + if (ps.status === 0) { |
| 88 | + spinner.succeed(`Deleted${this.text(tag)}`) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + text(tag) { |
| 94 | + return `${this.isRemotes ? ' remotes' : ''} tag ` + kleur.magenta(tag) |
| 95 | + } |
| 96 | + |
| 97 | + run() { |
| 98 | + this.cli.runMatchedCommand() |
| 99 | + } |
| 100 | +} |
0 commit comments