-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
73 lines (65 loc) · 1.97 KB
/
install.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
const download = require('download');
const Progress = require('progress');
const fs = require('fs-extra');
const cp = require('child_process');
const path = require('path');
// Global variables.
const RELEASE = '19.6.1';
const URLPREFIX = 'https://github.com/golangf/youtubeuploader/releases/download';
const ARCH = {
arm: 'armv7',
x64: 'amd64'
};
const PLATFORM = {
darwin: 'mac',
win32: 'windows'
};
const FORMAT = '[:bar] :percent :etas';
const OPTIONS = {
complete: '=',
incomplete: ' ',
width: 20,
total: 0
};
// Run a command, return true if success.
function cpRun(txt) {
try { cp.execSync(txt, {stdio: []}); }
catch(e) { return false; }
return true;
};
// Match files matching pattern in path.
function readdirMatch(pth, pat) {
return fs.readdirSync(pth).filter(nam => pat.test(nam));
};
// Get download URL.
function downloadUrl() {
var arch = ARCH[process.arch]||process.arch;
var platform = PLATFORM[process.platform]||'linux';
return `${URLPREFIX}/${RELEASE}/youtubeuploader_${platform}_${arch}.zip`;
};
// Download and extract files (with progress).
function edownload(url, dst, opt) {
var o = (opt || typeof dst==='string'? opt:dst)||{};
var bar = o.progress===undefined? new Progress(FORMAT, OPTIONS):o.progress;
return download(url, dst, opt).on('response', o.onresponse||(res => {
if(bar==null) return;
bar.total = res.headers['content-length'];
res.on('data', dat => bar.tick(dat.length));
}));
};
// Setup "youtubeuploader".
async function setup() {
fs.removeSync('youtubeuploader');
if(cpRun('youtubeuploader --version')) {
console.log('setup: youtubeuploader already exists.');
return fs.removeSync('.');
}
var url = downloadUrl();
console.log(`setup: Downloading ${url}`);
await edownload(url, '.', {extract: true});
var fil = readdirMatch('.', /youtubeuploader.*/)[0];
fs.renameSync(fil, 'youtubeuploader'+path.extname(fil));
cpRun('chmod -f +x youtubeuploader*');
fs.removeSync('node_modules');
};
setup();