Skip to content

Commit a225e82

Browse files
authored
Merge pull request #2 from rpiontik/new-api
New API
2 parents 57e27a3 + d16894f commit a225e82

File tree

7 files changed

+627
-541
lines changed

7 files changed

+627
-541
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ npx archpkg remove dochub-examples
130130
npx archpkg clean
131131
```
132132

133+
# Переменные среды
134+
135+
* **ARCHPKG_CACHE_FOLDER** - путь к директории кэширования. По умолчанию: <ползовательская директория>/.archpkg
136+
* **ARCHPKG_REPO_SERVER** - сервер репозитория. По умолчанию: https://registry.dochub.info/
137+
* **ARCHPKG_DOWNLOAD_CERT** - пусть к ssl-сертификату для скачивания пакетов. По умолчанию не установлено.
133138

134139
# Лицензия
135140

cli/downloader.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const zlib = require('zlib');
2+
const tar = require("tar");
3+
4+
// Реализует функцию загрузки и распаковки пакетов
5+
module.exports = function (context) {
6+
const fs = context.fs;
7+
const log = context.log;
8+
9+
return {
10+
makeDownloadParams(url, method = 'GET') {
11+
if (context.env.downloadCert) {
12+
return {
13+
method,
14+
url,
15+
agentOptions: {
16+
ca: context.env.downloadCert
17+
}
18+
};
19+
} else return url;
20+
},
21+
22+
async downloadAndUnzip(url, tmpFolder) {
23+
return new Promise((success, reject) => {
24+
// Создаем временную папку для скачивания
25+
const tryFolder = `${tmpFolder}__`;
26+
fs.existsSync(tryFolder) && fs.rmSync(tryFolder, { recursive: true });
27+
fs.mkdirSync(tryFolder, { recursive: true });
28+
29+
log.begin(`Downloading ${url}...`);
30+
let totalBytes = 0;
31+
let receivedBytes = 0;
32+
33+
context.request(this.makeDownloadParams(url))
34+
.on('response', (data) => {
35+
totalBytes = parseInt(data.headers['content-length']);
36+
if (data.statusCode === 404)
37+
reject(`Package unavailable on URL ${url}`);
38+
if ((data.statusCode < 200) || data.statusCode > 300)
39+
reject(`Error of downloading package from [${url}]. Response with code ${data.statusCode}.`);
40+
log.progressBegin();
41+
})
42+
.on('data', (chunk) => {
43+
receivedBytes += chunk.length;
44+
log.progress(receivedBytes, totalBytes);
45+
})
46+
.on('end', (chunk) => {
47+
log.progressEnd();
48+
log.end('Done.');
49+
})
50+
.on('error', reject)
51+
//.pipe(unzipper.Extract({ path: tmpFolder }))
52+
.pipe(zlib.createGunzip())
53+
.pipe(tar.x({
54+
strip: 1,
55+
C: tryFolder
56+
}))
57+
.on('error', reject)
58+
.on('close', () => {
59+
fs.renameSync(tryFolder, tmpFolder);
60+
success(tmpFolder);
61+
});
62+
});
63+
}
64+
}
65+
}

cli/log.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
module.exports = function () {
3+
return {
4+
level: 0,
5+
begin(info) {
6+
console.log(this.getMargin(), info);
7+
this.level++;
8+
},
9+
end(info) {
10+
console.info(this.getMargin(), `\x1b[32m${info}\x1b[0m`);
11+
this.level && this.level--;
12+
},
13+
getMargin() {
14+
return "||||||||||||||||||||||||||||||||||||||||||||||||||||||||".slice(0, this.level) + '-';
15+
},
16+
debug(info) {
17+
console.log(this.getMargin(), info);
18+
},
19+
error(info) {
20+
console.error(this.getMargin(), `\x1b[31m${info}\x1b[0m`);
21+
},
22+
success(info) {
23+
console.info(this.getMargin(), `\x1b[32m${info}\x1b[0m`);
24+
},
25+
progressBegin() {
26+
},
27+
progress(value, total) {
28+
if (total) {
29+
const percentage = ((value * 100) / total).toFixed(2);
30+
process.stdout.write(`\r${this.getMargin()} [${percentage}%] ${value} bytes out of ${total} bytes.`);
31+
} else {
32+
process.stdout.write(`\r${this.getMargin()} Recieved ${value} bytes.`);
33+
}
34+
},
35+
progressEnd() {
36+
process.stdout.write('\r\n');
37+
},
38+
};
39+
}

0 commit comments

Comments
 (0)