Skip to content

Commit d18082d

Browse files
committed
fix: optimize npm deps
1 parent e99c35d commit d18082d

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@
7272
"dependencies": {
7373
"@tencent-sdk/capi": "^0.2.17",
7474
"@ygkit/request": "^0.0.6",
75-
"klaw-sync": "^6.0.0",
7675
"moment": "^2.25.3",
77-
"tencent-cloud-sdk": "^1.0.2"
76+
"tencent-cloud-sdk": "^1.0.3"
7877
}
7978
}

src/modules/cos/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const { cos } = require('tencent-cloud-sdk');
22
const util = require('util');
33
const path = require('path');
44
const fs = require('fs');
5-
const klawSync = require('klaw-sync');
65
const exec = util.promisify(require('child_process').exec);
6+
const { traverseDirSync } = require('../../utils');
77
const { TypeError } = require('../../utils/error');
88

99
class Cos {
@@ -412,7 +412,7 @@ class Cos {
412412

413413
const items = await new Promise((resolve, reject) => {
414414
try {
415-
resolve(klawSync(inputs.dir));
415+
resolve(traverseDirSync(inputs.dir));
416416
} catch (error) {
417417
reject(error);
418418
}

src/utils/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
14
/**
25
* is array
36
* @param obj object
@@ -120,6 +123,36 @@ function strip(num, precision = 12) {
120123
return +parseFloat(num.toPrecision(precision));
121124
}
122125

126+
function traverseDirSync(dir, opts, ls) {
127+
if (!ls) {
128+
ls = [];
129+
dir = path.resolve(dir);
130+
opts = opts || {};
131+
if (opts.depthLimit > -1) {
132+
opts.rootDepth = dir.split(path.sep).length + 1;
133+
}
134+
}
135+
const paths = fs.readdirSync(dir).map((p) => dir + path.sep + p);
136+
for (let i = 0; i < paths.length; i++) {
137+
const pi = paths[i];
138+
const st = fs.statSync(pi);
139+
const item = { path: pi, stats: st };
140+
const isUnderDepthLimit =
141+
!opts.rootDepth || pi.split(path.sep).length - opts.rootDepth < opts.depthLimit;
142+
const filterResult = opts.filter ? opts.filter(item) : true;
143+
const isDir = st.isDirectory();
144+
const shouldAdd = filterResult && (isDir ? !opts.nodir : !opts.nofile);
145+
const shouldTraverse = isDir && isUnderDepthLimit && (opts.traverseAll || filterResult);
146+
if (shouldAdd) {
147+
ls.push(item);
148+
}
149+
if (shouldTraverse) {
150+
ls = traverseDirSync(pi, opts, ls);
151+
}
152+
}
153+
return ls;
154+
}
155+
123156
module.exports = {
124157
isArray,
125158
isObject,
@@ -128,4 +161,5 @@ module.exports = {
128161
uniqueArray,
129162
camelCaseProperty,
130163
strip,
164+
traverseDirSync,
131165
};

0 commit comments

Comments
 (0)