1 | const sg = require('simple-git')()
|
2 | const which = require('which')
|
3 | const log = console.log
|
4 |
|
5 | let git = {
|
6 | $scl: 'git',
|
7 | $lcl: /notpossiblyfind/,
|
8 |
|
9 | info(ad, done) {
|
10 | which('git', err => {
|
11 | if (err) {
|
12 | if (ad.source === 'git')
|
13 | log(
|
14 | 'In order to install from arbitrary git, wowa requires git to be installed and that it can be called using the command git.'
|
15 | )
|
16 | return done()
|
17 | }
|
18 | sg.listRemote([ad.uri], (err, data) => {
|
19 | if (err) return done()
|
20 |
|
21 | let d = { 'refs/tags/': 1, 'refs/heads/': 1 }
|
22 | let info = {
|
23 | name: ad.uri,
|
24 | page: ad.uri,
|
25 | version: []
|
26 | }
|
27 |
|
28 | for (k in d)
|
29 | data.split('\n').forEach(line => {
|
30 |
|
31 | if (line.match(/{}$/)) return
|
32 | if (line.match(k))
|
33 | info.version.unshift({
|
34 | name: line.slice(line.search(k) + k.length),
|
35 | hash: line.split('\t')[0]
|
36 | })
|
37 | })
|
38 |
|
39 |
|
40 | done(info)
|
41 | })
|
42 | })
|
43 | },
|
44 |
|
45 | clone(uri, ref, to, hook) {
|
46 | sg.outputHandler((cmd, o1, o2) => {
|
47 | let unit = { KiB: 1024, MiB: 1 << 20, GiB: 1 << 30 }
|
48 | o2.on('data', line => {
|
49 | line
|
50 | .toString()
|
51 | .split('\r')
|
52 | .forEach(l => {
|
53 | if (!l.match(/Receiving objects/)) return
|
54 | let tr = /, [0-9\.]+ [KMG]iB \|/.exec(l)
|
55 | let evt = {
|
56 | percent:
|
57 | /\([0-9]+\
|
58 | /\/[0-9]+\)/.exec(l)[0].slice(1, -1),
|
59 | transferred: !tr
|
60 | ? 0
|
61 | : tr[0].slice(2, -6) * unit[tr[0].slice(-5, -2)]
|
62 | }
|
63 | hook(evt)
|
64 |
|
65 |
|
66 | })
|
67 | })
|
68 | })
|
69 | .silent(true)
|
70 | .clone(
|
71 | uri,
|
72 | to,
|
73 | ['-b', ref, '--single-branch', '--depth', 1, '--progress', '--verbose'],
|
74 | (err, data) => {
|
75 | if (err) return hook(err.toString())
|
76 | else hook('done')
|
77 | }
|
78 | )
|
79 | }
|
80 | }
|
81 |
|
82 | module.exports = git
|