Skip to content

Commit 16eca8f

Browse files
author
欧如栋
committed
增加更多日志
1 parent 64ab389 commit 16eca8f

File tree

5 files changed

+594
-16
lines changed

5 files changed

+594
-16
lines changed

.eslintrc.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"commonjs": true,
5+
"es2021": true
6+
},
7+
"extends": "eslint:recommended",
8+
"parserOptions": {
9+
"ecmaVersion": "latest"
10+
},
11+
"rules": {
12+
"no-unused-vars": "warn"
13+
}
14+
}

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ npm install coding-generic -g
99

1010
## 使用
1111

12+
- 推送单个制品
1213
```shell
1314
coding-generic --username=<USERNAME>[:password] --path=<FILE.EXT> --registry=<REGISTRY>
1415
```
16+
- 推送文件夹(仅 1.2.7 及以上版本支持)
17+
```shell
18+
coding-generic -u=coding@coding.com --dir --path=<LOCAL_FOLDER_NAME> --registry=<REGISTRY>
19+
```

bin/index.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ const upload = async (filePath, parts = [], requestUrl) => {
3939
const uploadChunk = async (currentChunk, currentChunkIndex, parts, isRetry) => {
4040
if (parts.some(({ partNumber, size }) => partNumber === currentChunkIndex && size === currentChunk.length)) {
4141
bar.tick();
42+
logger.info(`分片(${currentChunkIndex})已经上传,跳过 (path: ${filePath}) , url: ${requestUrl})`);
4243
return Promise.resolve();
4344
}
4445

4546
try {
47+
logger.info(`开始上传分片(${currentChunkIndex}) (path: ${filePath}) , url: ${requestUrl})`);
4648
await _uploadChunk(requestUrl, {
4749
uploadId,
4850
version,
@@ -55,18 +57,23 @@ const upload = async (filePath, parts = [], requestUrl) => {
5557
},
5658
Authorization
5759
});
60+
logger.info(`分片(${currentChunkIndex})上传完毕 (path: ${filePath}) , url: ${requestUrl})`);
5861
bar.tick();
5962
} catch (error) {
63+
console.error(`分片(${currentChunkIndex})上传失败 (path: ${filePath}) , url: ${requestUrl})`);
64+
logger.error(`分片(${currentChunkIndex})上传失败 (path: ${filePath}) , url: ${requestUrl})`);
6065
logger.error(error.message);
6166
logger.error(error.stack);
6267
if (['ECONNREFUSED', 'ECONNRESET', 'ENOENT', 'EPROTO'].includes(error.code)) {
6368
// 没有重试过就重试一次
6469
if (!isRetry) {
6570
logger.warn('retry')
6671
logger.warn(error.code);
72+
logger.info(`重试分片(${currentChunkIndex})上传 (path: ${filePath}) , url: ${requestUrl})`);
6773
await uploadChunk(currentChunk, currentChunkIndex, parts, true);
6874
} else {
6975
console.log(chalk.red('网络连接异常,请重新执行命令继续上传'));
76+
logger.error(`分片(${currentChunkIndex})上传时网络连接异常 (path: ${filePath}) , url: ${requestUrl})`);
7077
process.exit(1);
7178
}
7279
} else {
@@ -77,18 +84,20 @@ const upload = async (filePath, parts = [], requestUrl) => {
7784
}
7885

7986
console.log(`\n开始上传 (${filePath})\n`);
80-
logger.info(`开始上传 (${filePath})`);
87+
logger.info(`开始上传 (path: ${filePath}) , url: ${requestUrl})`);
8188

8289
try {
8390

84-
const chunkIndexs = new Array(totalChunk).fill("").map((_, index) => index + 1)
91+
const chunkIndexs = new Array(totalChunk).fill("").map((_, index) => index + 1);
92+
93+
logger.info(`分片总数:${totalChunk},分片大小:${chunkSize} (path: ${filePath}) , url: ${requestUrl})`);
8594

8695
await BlueBirdPromise.map(chunkIndexs, (currentChunkIndex) => {
8796
const start = (currentChunkIndex - 1) * chunkSize;
8897
const end = ((start + chunkSize) >= fileSize) ? fileSize : start + chunkSize - 1;
8998
const stream = fs.createReadStream(filePath, { start, end })
9099
let buf = [];
91-
return new Promise((resolve) => {
100+
return new Promise((resolve, reject) => {
92101
stream.on('data', data => {
93102
buf.push(data)
94103
})
@@ -101,6 +110,7 @@ const upload = async (filePath, parts = [], requestUrl) => {
101110
resolve();
102111
})
103112
}).catch(error => {
113+
logger.error(`读取分片 ${currentChunkIndex} 数据失败 (path: ${filePath}) , url: ${requestUrl})`);
104114
throw Error(error)
105115
})
106116
}, { concurrency: argv.concurrency })
@@ -117,7 +127,8 @@ const upload = async (filePath, parts = [], requestUrl) => {
117127

118128

119129
const merge = async () => {
120-
console.log(chalk.cyan('正在合并分片,请稍等...'))
130+
console.log(chalk.cyan('正在合并分片,请稍等...'));
131+
logger.info(`正在合并分片 (path: ${filePath}) , url: ${requestUrl})`);
121132
return await _mergeAllChunks(requestUrl, {
122133
version,
123134
uploadId,
@@ -132,21 +143,24 @@ const upload = async (filePath, parts = [], requestUrl) => {
132143
try {
133144
const res = await withRetry(merge, 3, 500);
134145
if (res.code) {
146+
logger.error(`合并分片失败 (path: ${filePath}) , url: ${requestUrl})`);
135147
throw (res.message);
136148
}
137149
} catch (error) {
138150
logger.error(error.message);
139151
logger.error(error.stack);
140152
console.log(chalk.red((error.response && error.response.data) || error.message));
141-
return;
153+
process.exit(1);
142154
}
143155

144156
console.log(chalk.green(`\n上传完毕 (${filePath})\n`))
145-
logger.info('************************ 上传完毕 ************************')
157+
logger.info(`************************ 上传完毕 (path: ${filePath}) , url: ${requestUrl}) ************************`)
146158
}
147159

148160
const getFileMD5Success = async (filePath, requestUrl) => {
161+
let uploadedParts = []
149162
try {
163+
logger.info(`获取已上传信息 (path: ${filePath} , url: ${requestUrl})`);
150164
const res = await _getExistChunks(requestUrl, {
151165
fileSize,
152166
version,
@@ -158,20 +172,23 @@ const getFileMD5Success = async (filePath, requestUrl) => {
158172
throw (res.message);
159173
}
160174
uploadId = res.data.uploadId;
161-
175+
logger.info(`上传的 UploadId: ${uploadId} (path: ${filePath} , url: ${requestUrl})`);
162176
// 上传过一部分
163177
if (Array.isArray(res.data.parts)) {
164-
await upload(filePath, res.data.parts, requestUrl);
178+
uploadedParts = res.data.parts
165179
} else {
166180
// 未上传过
167-
await upload(filePath, [], requestUrl);
181+
uploadedParts = []
168182
}
169183
} catch (error) {
184+
logger.error(`获取已上传信息错误 (path: ${filePath} , url: ${requestUrl})`);
170185
logger.error(error.message);
171186
logger.error(error.stack);
172187
console.log(chalk.red((error.response && error.response.data) || error.message));
173188
process.exit(1);
174189
}
190+
191+
await upload(filePath, uploadedParts, requestUrl);
175192
}
176193

177194
const getFileMD5 = async (filePath, requestUrl) => {
@@ -186,8 +203,8 @@ const getFileMD5 = async (filePath, requestUrl) => {
186203
logger.info(`开始计算 MD5 (${filePath})`);
187204

188205
const bar = new ProgressBar(':bar [:current/:total] :percent ', { total: totalChunk });
189-
await new Promise(resolve => {
190-
stream = fs.createReadStream(filePath, { highWaterMark: chunkSize });
206+
await new Promise((resolve, reject) => {
207+
const stream = fs.createReadStream(filePath, { highWaterMark: chunkSize });
191208
stream.on('data', chunk => {
192209
bar.tick();
193210
spark.append(chunk)
@@ -203,6 +220,7 @@ const getFileMD5 = async (filePath, requestUrl) => {
203220
resolve();
204221
})
205222
}).catch(error => {
223+
logger.error(`计算 MD5 失败(${filePath})`);
206224
throw Error(error);
207225
})
208226
} catch (error) {
@@ -215,6 +233,7 @@ const getFileMD5 = async (filePath, requestUrl) => {
215233

216234
const uploadFile = async (filePath, size, requestUrl) => {
217235
fileSize = size;
236+
logger.info(`('************************ 开始上传 (${filePath}) ('************************`);
218237
await getFileMD5(filePath, requestUrl);
219238
md5 = '';
220239
uploadId = '';
@@ -245,7 +264,7 @@ const uploadDir = async (dir) => {
245264
logger.error(error.stack);
246265
process.exit(1);
247266
} else {
248-
resolve(files)
267+
return files;
249268
}
250269
}
251270

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coding-generic",
3-
"version": "1.2.7",
3+
"version": "1.2.8",
44
"description": "",
55
"main": "index.js",
66
"bin": {
@@ -26,5 +26,8 @@
2626
"repository": {
2727
"type": "git",
2828
"url": "git@github.com:Coding/coding-generic.git"
29+
},
30+
"devDependencies": {
31+
"eslint": "^8.44.0"
2932
}
3033
}

0 commit comments

Comments
 (0)