-
Notifications
You must be signed in to change notification settings - Fork 0
/
githubLinkConverterDownloadwer.js
71 lines (63 loc) · 2.48 KB
/
githubLinkConverterDownloadwer.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
const { exec } = require('child_process');
const path = require('path');
function githubLinkConverter(link) {
const releaseTagPattern = /https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/releases\/tag\/([\d\.]+)\/([\w\-\.]+)$/;
const blobPattern = /https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/blob\/([\w\/\-\.]+)$/;
const releaseDownloadPattern = /https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/releases\/download\/([\d\.]+)\/([\w\-\.]+)$/;
const rawPattern = /https:\/\/raw\.githubusercontent\.com\/([\w-]+)\/([\w-]+)\/([\w\/\-\.]+)$/;
if (releaseTagPattern.test(link)) {
return link.replace(releaseTagPattern, 'https://github.com/$1/$2/releases/download/$3/$4');
}
if (blobPattern.test(link)) {
return link.replace(blobPattern, 'https://raw.githubusercontent.com/$1/$2/$3');
}
if (releaseDownloadPattern.test(link)) {
return link.replace(releaseDownloadPattern, 'https://github.com/$1/$2/releases/tag/$3/$4');
}
if (rawPattern.test(link)) {
return link.replace(rawPattern, 'https://github.com/$1/$2/blob/$3');
}
return 'Invalid Input.';
}
function downloadFile(link, method) {
const transformedLink = githubLinkConverter(link);
if (method === 'wget') {
exec(`wget -O ${path.basename(transformedLink)} ${transformedLink}`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`Downloaded with wget: ${stdout}`);
});
} else if (method === 'clink') {
exec(`clink -o ${path.basename(transformedLink)} ${transformedLink}`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`Downloaded with clink: ${stdout}`);
});
}
}
const args = process.argv.slice(2);
if (args.length === 0) {
console.log("Usage: node githubLinkConverter.js <link1> <link2> ... [-w|--wget | -c|--clink]");
process.exit(1);
}
let method = null;
if (args.includes('-w') || args.includes('--wget')) {
method = 'wget';
} else if (args.includes('-c') || args.includes('--clink')) {
method = 'clink';
}
const links = args.filter(arg => !arg.startsWith('-'));
if (method) {
links.forEach((link) => {
downloadFile(link, method);
});
} else {
links.forEach((link) => {
const transformedLink = githubLinkConverter(link);
console.log(`${transformedLink}\n`);
});
}