-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.js
129 lines (117 loc) · 3.89 KB
/
utils.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const util = require('util');
const fs = require('fs');
const fetch = require('node-fetch');
const execp = util.promisify(require('child_process').exec);
const exec = async (command, opts) => {
return new Promise(function(resolve, reject) {
const { debug } = opts || {};
execp(command, (error, stdout, stderr) => {
if (debug) console.log(`\nCommand: ${command}\n\t${stdout}\n\t${stderr}`);
if (error) reject(error);
resolve((stdout || stderr).slice(0, -1));
});
});
};
const download = async (url, path) => {
const res = await fetch(url);
const fileStream = fs.createWriteStream(path);
await new Promise((resolve, reject) => {
if (res.status !== 200) return reject(new Error(res.statusText));
res.body.pipe(fileStream);
res.body.on('error', err => {
reject(err);
});
fileStream.on('finish', function() {
resolve();
});
});
};
const getLatestVersion = async () => {
const endpoint = 'https://updater.dvc.org';
const response = await fetch(endpoint, { method: 'GET' });
if (response.ok) {
const { version } = await response.json();
return version;
} else {
const status = `Status: ${response.status} ${response.statusText}`;
const body = `Body:\n${await response.text()}`;
throw new Error(`${status}\n${body}`);
}
};
const prepGitRepo = async () => {
const repo = await exec(`git config --get remote.origin.url`);
const rawToken = await exec(
`git config --get "http.https://github.com/.extraheader"`
);
// format of rawToken "AUTHORIZATION: basic ***"
const [, , token64] = rawToken.split(' ');
// eC1hY2Nlc3MtdG9rZW46Z2hzX ...
const token = Buffer.from(token64, 'base64')
.toString('utf-8')
.split(':')
.pop();
// x-access-token:ghs_***
const newURL = new URL(repo);
newURL.password = token;
newURL.username = 'token';
const finalURL =
newURL.toString() + (newURL.toString().endsWith('.git') ? '' : '.git');
await exec(`git remote set-url origin "${finalURL}"`);
await exec(`git config --unset "http.https://github.com/.extraheader"`);
};
const setupDVC = async opts => {
const { platform } = process;
let { version = 'latest' } = opts;
if (version === 'latest') {
version = await getLatestVersion();
}
if (platform === 'linux') {
let sudo = '';
try {
sudo = await exec('which sudo');
} catch (err) {}
try {
const dvcURL = `https://dvc.org/download/linux-deb/dvc-${version}`;
console.log(`Installing DVC from: ${dvcURL}`);
await download(dvcURL, 'dvc.deb');
} catch (err) {
console.log('DVC Download Failed, trying from GitHub Releases');
const dvcURL = `https://github.com/iterative/dvc/releases/download/${version}/dvc_${version}_amd64.deb`;
console.log(`Installing DVC from: ${dvcURL}`);
await download(dvcURL, 'dvc.deb');
}
console.log(
await exec(
`${sudo} apt update && ${sudo} apt install -y --allow-downgrades git ./dvc.deb && ${sudo} rm -f 'dvc.deb'`
)
);
}
if (platform === 'darwin') {
try {
const dvcURL = `https://dvc.org/download/osx/dvc-${version}`;
console.log(`Installing DVC from: ${dvcURL}`);
await download(dvcURL, 'dvc.pkg');
} catch (err) {
console.log('DVC Download Failed, trying from GitHub Releases');
const dvcURL = `https://github.com/iterative/dvc/releases/download/${version}/dvc-${version}.pkg`;
console.log(`Installing DVC from: ${dvcURL}`);
await download(dvcURL, 'dvc.pkg');
}
console.log(
await exec(`sudo installer -pkg "dvc.pkg" -target / && rm -f "dvc.pkg"`)
);
}
if (platform === 'win32') {
console.log('Installing DVC with pip');
console.log(
await exec(
`pip install --upgrade dvc[all]${
version !== 'latest' ? `==${version}` : ''
}`
)
);
}
};
exports.exec = exec;
exports.setupDVC = setupDVC;
exports.prepGitRepo = prepGitRepo;