-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
76 lines (62 loc) · 1.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const url = require('url');
const path = require('path');
const origin = require('remote-origin-url');
const repo = (options, cb) => {
if (typeof options === 'function') {
cb = options;
options = process.cwd();
}
let promise = repo.promise(options);
if (typeof cb === 'function') {
promise.then(name => cb(null, name)).catch(cb);
return;
}
return promise;
};
repo.promise = (options = {}) => {
if (typeof options === 'string') {
options = { cwd: options };
}
let opts = { cwd: process.cwd(), ...options };
if (!opts.path) {
opts.path = path.resolve(opts.cwd, '.git/config');
}
return new Promise((resolve, reject) => {
origin(opts, (err, giturl) => {
if (err) {
reject(err);
return;
}
if (!giturl) {
reject(new Error('cannot find ".git/config"'));
return;
}
let parsed = url.parse(giturl);
let segments = parsed.pathname.split(path.sep);
let filepath = segments.pop();
resolve(stem(filepath));
});
});
};
repo.sync = (options = {}) => {
if (typeof options === 'string') options = { cwd: options };
let opts = { cwd: process.cwd(), ...options };
if (!opts.path) {
opts.path = path.resolve(opts.cwd, '.git/config');
}
let giturl = origin.sync(opts);
if (!giturl) {
throw new Error('cannot find ".git/config"');
}
let parsed = url.parse(giturl);
let segments = parsed.pathname.split(path.sep);
return stem(segments.pop());
};
function stem(filepath) {
const ext = path.extname(filepath);
return ext === '.git'
? path.basename(filepath, ext)
: path.basename(filepath);
}
module.exports = repo;