@heroku-cli/command
Version:
base class for Heroku CLI commands
62 lines (61 loc) • 1.68 kB
JavaScript
import { Errors } from '@oclif/core';
import childProcess from 'node:child_process';
import { vars } from './vars.js';
export class Git {
get remotes() {
return this.exec('remote -v')
.split('\n')
.filter(l => l.endsWith('(fetch)'))
.map(l => {
const [name, url] = l.split('\t');
return { name, url: url.split(' ')[0] };
});
}
exec(cmd) {
try {
return childProcess.execSync(`git ${cmd}`, {
encoding: 'utf8',
stdio: [null, 'pipe', null],
});
}
catch (error) {
if (error.code === 'ENOENT') {
throw new Errors.CLIError('Git must be installed to use the Heroku CLI. See instructions here: http://git-scm.com');
}
throw error;
}
}
}
export function configRemote() {
const git = new Git();
try {
return git.exec('config heroku.remote').trim();
}
catch { }
}
export function getGitRemotes(onlyRemote) {
const git = new Git();
const appRemotes = [];
let remotes;
try {
remotes = git.remotes;
}
catch {
return [];
}
for (const remote of remotes) {
if (onlyRemote && remote.name !== onlyRemote)
continue;
for (const prefix of vars.gitPrefixes) {
const suffix = '.git';
const match = remote.url.match(`${prefix}(.*)${suffix}`);
if (!match)
continue;
appRemotes.push({
app: match[1],
remote: remote.name,
});
}
}
return appRemotes;
}