-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathgit.js
50 lines (41 loc) · 1.1 KB
/
git.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
import crypto from 'crypto';
import os from 'os';
import path from 'path';
import * as sander from '@marionebl/sander';
import execa from 'execa';
import pkgDir from 'pkg-dir';
export {bootstrap, clone, init};
async function bootstrap(fixture) {
const cwd = path.join(os.tmpdir(), rand());
if (typeof fixture !== 'undefined') {
await sander.copydir(await pkgDir(), fixture).to(cwd);
}
await init(cwd);
return cwd;
}
async function clone(source, ...args) {
const cwd = path.join(os.tmpdir(), rand());
await execa('git', ['clone', ...args, source, cwd]);
await setup(cwd);
return cwd;
}
async function init(cwd) {
await execa('git', ['init', cwd]);
await setup(cwd);
return cwd;
}
async function setup(cwd) {
try {
await execa('git', ['config', 'user.name', 'ava'], {cwd});
await execa('git', ['config', 'user.email', '[email protected]'], {cwd});
await execa('git', ['config', 'commit.gpgsign', 'false'], {cwd});
} catch (err) {
console.warn(`git config in ${cwd} failed`, err.message);
}
}
function rand() {
return crypto
.randomBytes(Math.ceil(6))
.toString('hex')
.slice(0, 12);
}