UNPKG

changelog-guru

Version:
43 lines (42 loc) 1.71 kB
import findupSync from 'findup-sync'; import fs from 'fs'; import path from 'path'; import TaskTree from 'tasktree-cli'; import { getUserAgent } from 'universal-user-agent'; export var Branch; (function (Branch) { Branch["Main"] = "main"; Branch["Dev"] = "dev"; })(Branch || (Branch = {})); export default class GitProvider { constructor({ type, url, branch }) { this.package = 'package.json'; this.version = process.env.npm_package_version; const task = TaskTree.add('Initializing git provider'); const pathname = new URL(url).pathname.split('/'); const filePath = findupSync('.git/HEAD', { cwd: process.cwd() }); this.type = type; this.repository = path.basename(pathname.pop(), '.git'); this.owner = pathname.pop(); this.userAgent = `changelog-guru/${this.version} ${getUserAgent()}`; this.branch = { main: branch ?? 'master', dev: '' }; if (filePath && fs.existsSync(filePath)) { const buffer = fs.readFileSync(filePath); const match = /ref: refs\/heads\/([^\n]+)/.exec(buffer.toString()); if (match && match[1]) { [, this.branch.dev] = match; } else { task.warn('{bold .git/HEAD} - ref(s) SHA not found'); } } else { task.warn('{bold .git/HEAD} - does not exist'); } task.log(`Provider: {bold ${this.type}}`); task.log(`Repository: {bold ${this.repository}}`); task.log(`Branches: from {bold ${this.branch.dev}} to {bold ${this.branch.main}}`); task.log(`Owner: {bold ${this.owner}}`); task.complete('Git provider:'); } }