forked from leedium/occ-download-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·119 lines (104 loc) · 3.61 KB
/
index.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
#!/usr/bin/env node
/*
* Copyright (c) 2018 LEEDIUM.
* This file is subject to the terms and conditions
* defined in file 'LICENSE.txt', which is part of this
* source code package.
*/
/**
* @project occ-download-extension
* @file index.js
* @company LEEDIUM
* @createdBy davidlee
* @contact [email protected]
* @dateUpdated 12/12/2018
* @description Downloads an extension to a zip file;
*/
const program = require('commander');
const axios = require('axios');
const fs = require('fs');
const occTokenGenerator = require('./occ-token-generator');
const packageJSON = require('./package');
let counter = 0;
Promise.each = (arr, func) => {
return arr.reduce((a, item) => {
return a.then(() => {
return func(item);
})
}, Promise.resolve())
};
const main = () => {
program
.version(packageJSON.version)
.description(packageJSON.description)
.option(
"-s, --sourceserver <sourceserver> ",
"Occ Admin url for source instance (from)"
)
.option(
"-k, --sourcekey <sourcekey>",
"Occ Admin api key for source instance (from)"
).parse(process.argv);
/**
* Starts the task
* Login and get an access_token, then request the zip and pipe a file
* @returns {Promise<void>}
*/
const start = async () => {
try {
const token = await occTokenGenerator.generateToken(program.sourceserver, program.sourcekey);
const result = await axios(
{
"method": "GET",
"url": `${program.sourceserver}/ccadmin/v1/extensions?fields=items.name,items.zipPath`,
"responseType": "arrayBuffer",
"headers": {
"Authorization": `Bearer ${token}`,
"X-CCAsset-Language": "en"
}
});
const {items} = result.data;
Promise.each(result.data.items, (item) =>
new Promise(async (resolve, reject) => {
try {
const {data} = await axios(
{
"method": "GET",
"url": `${program.sourceserver}/file/${encodeURIComponent(item.zipPath)}`,
"responseType": "arraybuffer",
"headers": {
"Authorization": `Bearer ${token}`,
"X-CCAsset-Language": "en"
}
});
const fileStream = fs.createWriteStream(`${item.name.replace(/\//g, '%2F')}.zip`);
fileStream.on('finish', () => {
console.log(`${item.name} download complete. - ${++counter}/${items.length}`);
resolve();
});
fileStream.write(data, 'binary');
fileStream.end();
} catch (err) {
reject();
}
})
)
} catch (err) {
console.log(err.message);
}
};
try {
if (typeof program.sourceserver === 'undefined') {
throw new Error('missing sourceserver -s');
}
else if (typeof program.sourcekey === 'undefined') {
throw new Error('missing sourcekey : -k');
}
else {
start();
}
} catch (err) {
console.log(err.message);
}
};
return main();