Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7673b7c

Browse files
author
欧如栋
committed
支持下载文件夹
1 parent 2e7ba93 commit 7673b7c

File tree

7 files changed

+109
-7
lines changed

7 files changed

+109
-7
lines changed

‎README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ coding-generic -u=<USERNAME>[:password] --path=<LOCAL_FILE_NAME> --registry=<REG
1616
- 推送文件夹(仅 1.2.7 及以上版本支持)
1717
```shell
1818
coding-generic -u=<USERNAME>[:password] --dir --path=<LOCAL_FOLDER_NAME> --registry=<REGISTRY>
19+
```
20+
21+
- 下载文件夹(仅 1.2.13 及以上版本支持)
22+
```shell
23+
coding-generic --pull -u=<USERNAME>[:password] --registry=<REGISTRY>/list/<DIR>?version=<VERSION>
1924
```

‎bin/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const { getExistChunks: _getExistChunks, uploadChunk: _uploadChunk, mergeAllChun
1818

1919
const { withRetry } = require('../lib/withRetry');
2020
const argv = require('../lib/argv');
21+
const { onDownload } = require('../lib/download');
2122

2223
const { requestUrl, version } = getRegistryInfo(argv.registry);
2324

@@ -339,7 +340,11 @@ const onUpload = async (_username, _password) => {
339340
const [username, password] = argv.username.split(':');
340341

341342
if (username && password) {
342-
onUpload(username, password);
343+
if (argv.pull) {
344+
onDownload()
345+
} else {
346+
onUpload(username, password);
347+
}
343348
} else {
344349
prompts([
345350
{
@@ -353,7 +358,10 @@ if (username && password) {
353358
).then(async (answers) => {
354359
if (!answers.password) {
355360
return;
361+
} if (argv.pull) {
362+
onDownload()
363+
} else {
364+
onUpload(argv.username, answers.password);
356365
}
357-
onUpload(argv.username, answers.password);
358366
})
359367
}

‎lib/argv.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const argv = require('yargs')
22
.usage('上传文件: coding-generic --username=<USERNAME>[:PASSWORD] --path=<FILE.EXT> --registry=<REGISTRY>')
33
.usage('上传文件夹: coding-generic --username=<USERNAME>[:PASSWORD] --dir --path=<FOLDER> --registry=<REGISTRY>')
4+
.usage('下载文件夹: coding-generic --pull --username=<USERNAME>[:PASSWORD] --registry=<REGISTRY>/list/<DIR>?version=<VERSION>')
45
.options({
56
username: {
67
alias: 'u',
@@ -10,7 +11,7 @@ const argv = require('yargs')
1011
path: {
1112
alias: 'p',
1213
describe: '需要上传的文件路径',
13-
demandOption: true
14+
// demandOption: true
1415
},
1516
registry: {
1617
alias: 'r',
@@ -27,6 +28,10 @@ const argv = require('yargs')
2728
alias: 'd',
2829
describe: '上传文件夹',
2930
boolean: true,
31+
},
32+
pull: {
33+
describe: '下载',
34+
boolean: true,
3035
}
3136
})
3237
.alias('version', 'v')

‎lib/download.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const logger = require('./log');
4+
const { generateAuthorization, getRegistryInfo } = require('./utils');
5+
const { fetchDownloadList, downloadFile } = require('../lib/request');
6+
const argv = require('./argv');
7+
8+
const { version, host, protocol, pathname } = getRegistryInfo(argv.registry);
9+
10+
let Authorization = '';
11+
12+
const onDownload = async () => {
13+
console.log('************************ 准备下载 ************************');
14+
logger.info('************************ 准备下载 ************************');
15+
Authorization = generateAuthorization(argv.username, argv.password);
16+
const res = await fetchDownloadList(argv.registry, Authorization)
17+
const { status, fileInfos = [] } = res.data
18+
if (status === 200) {
19+
await downloadFiles(fileInfos)
20+
console.log('************************ 下载完毕 ************************');
21+
logger.info('************************ 下载完毕 ************************');
22+
}
23+
}
24+
25+
const downloadFiles = async (fileInfos = []) => {
26+
try {
27+
return await Promise.all(fileInfos.map(async info => {
28+
console.log(`正在下载 ${info.fileName} ...`);
29+
logger.info(`正在下载 ${info.fileName} ...`);
30+
const p = path.join(process.cwd(), info.fileName);
31+
const dir = p.split('/').slice(0, -1).join('/');
32+
if (dir && !fs.existsSync(dir)) {
33+
fs.mkdirSync(dir);
34+
}
35+
const writer = fs.createWriteStream(p);
36+
const url = `${protocol}//${path.join(host, path.join(pathname.split('/').slice(0, -2).join('/'), info.fileName))}`
37+
const res = await downloadFile(url, { version }, Authorization);
38+
await res.data.pipe(writer)
39+
await writer.end();
40+
await writer.close();
41+
console.log(`下载 ${info.fileName} 完成`);
42+
logger.info(`下载 ${info.fileName} 完成`);
43+
}));
44+
} catch (error) {
45+
console.log(error);
46+
logger.error(error);
47+
throw error;
48+
}
49+
50+
}
51+
52+
module.exports = {
53+
onDownload
54+
}

‎lib/request.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const responseSuccess = response => {
1414
}
1515

1616
const responseFailed = error => {
17+
console.log('eeee=>', error)
1718
const url = error && error.config && error.config.url
1819
console.error('网络请求错误', `(${url})`);
1920
logger.error(`网络请求错误 (${url})`);
@@ -100,8 +101,36 @@ const mergeAllChunks = (requestUrl, {
100101
})
101102
}
102103

104+
105+
const fetchDownloadList = async (registry, Authorization) => {
106+
return http.post(registry, {
107+
}, {
108+
headers: { Authorization }
109+
})
110+
111+
}
112+
113+
//http:/codingcorp-generic.pkg.coding-artifacts.test-codingcorp.woa.com/coding-xxx-567023e/generic-public/test/coding-coding
114+
//http://codingcorp-generic.pkg.coding-artifacts.test-codingcorp.woa.com/coding-xxx-567023e/generic-public/test/coding-coding
115+
116+
const downloadFile = async (url, params, Authorization) => {
117+
return axios.get(url, {
118+
params,
119+
headers: {
120+
Authorization
121+
},
122+
responseType: 'stream'
123+
});
124+
125+
}
126+
127+
103128
module.exports = {
104129
getExistChunks,
105130
uploadChunk,
106-
mergeAllChunks
107-
}
131+
mergeAllChunks,
132+
fetchDownloadList,
133+
downloadFile
134+
}
135+
136+

‎lib/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const getRegistryInfo = (registry) => {
2323
const { version } = querystring.parse(query)
2424
return {
2525
requestUrl: `${protocol}//${path.join(host, pathname)}`,
26-
version: !version || version === '<VERSION>' ? 'latest' : version
26+
version: !version || version === '<VERSION>' ? 'latest' : version,
27+
host, protocol, pathname
2728
}
2829
}
2930

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coding-generic",
3-
"version": "1.2.12",
3+
"version": "1.2.13",
44
"description": "",
55
"main": "index.js",
66
"bin": {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /