1 | 'use strict';
|
2 |
|
3 | let exec = require('child_process').execFile;
|
4 | let fs = require('fs');
|
5 |
|
6 | module.exports = function (context) {
|
7 | let sshGitUrl = app => `git@${context.gitHost}:${app}.git`;
|
8 | let gitUrl = app => `https://${context.httpGitHost}/${app}.git`;
|
9 |
|
10 | function git(args) {
|
11 | return new Promise(function (resolve, reject) {
|
12 | exec('git', args, function (error, stdout, stderr) {
|
13 | process.stderr.write(stderr);
|
14 | if (error) return reject(error);
|
15 | resolve(stdout);
|
16 | });
|
17 | });
|
18 | }
|
19 |
|
20 | function hasGitRemote(remote) {
|
21 | return git(['remote']).then(remotes => remotes.split('\n')).then(remotes => remotes.find(r => r === remote));
|
22 | }
|
23 |
|
24 | function createRemote(remote, url) {
|
25 | return hasGitRemote(remote).then(exists => !exists ? git(['remote', 'add', remote, url]) : null);
|
26 | }
|
27 |
|
28 | function listRemotes() {
|
29 | return git(['remote', '-v']).then(remotes => remotes.trim().split('\n').map(r => r.split(/\s/)));
|
30 | }
|
31 |
|
32 | function inGitRepo() {
|
33 | try {
|
34 | fs.lstatSync('.git');
|
35 | return true;
|
36 | } catch (err) {
|
37 | if (err.code !== 'ENOENT') throw err;
|
38 | }
|
39 | }
|
40 |
|
41 | function rmRemote(remote) {
|
42 | return git(['remote', 'rm', remote]);
|
43 | }
|
44 |
|
45 | return {
|
46 | sshGitUrl,
|
47 | gitUrl,
|
48 | createRemote,
|
49 | listRemotes,
|
50 | rmRemote,
|
51 | inGitRepo
|
52 | };
|
53 | }; |
\ | No newline at end of file |