Skip to content

Commit c0eeb2a

Browse files
committed
fix(pack): 修复获取压缩文件 size 不准确
1 parent 719f3e0 commit c0eeb2a

File tree

6 files changed

+33
-9
lines changed

6 files changed

+33
-9
lines changed

lib/commands/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ exports.run = function (options) {
106106

107107
var contentLength = res._contentLength || '';
108108
if (contentLength) {
109-
contentLength = contentLength > 1024 ? (contentLength / 1024).toFixed(2) + 'kB ' : contentLength + 'bytes ';
109+
contentLength = contentLength > 1024 ? (contentLength / 1024).toFixed(2) + 'KB ' : contentLength + 'Bytes ';
110110
}
111111

112112
format = format.replace(/%date/g, '\x1b[90m' + '[' + moment().format(dateFormat) + ']' + '\x1b[0m');

lib/models/Project.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,14 @@ var Project = function () {
417417

418418
var assetsInfo = self.config._config.assetsInfo || statsInfo.assets;
419419
assetsInfo.map(function (asset) {
420-
var size = asset.size > 1024 ? (asset.size / 1024).toFixed(2) + ' kB' : asset.size + ' bytes';
420+
421+
var fileSize = UtilFs.getFileSize(path.resolve(cwd, asset.name));
422+
if (!fileSize) {
423+
fileSize = asset.size > 1024 ? (asset.size / 1024).toFixed(2) + ' KB' : asset.size + ' Bytes';
424+
}
425+
421426
if (!/\.cache$/.test(asset.name)) {
422-
log('- '.gray + asset.name + ' - ' + size);
427+
log('- '.gray + asset.name + ' - ' + fileSize);
423428
}
424429
});
425430

lib/utils/fs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,10 @@ exports.deleteFolderRecursive = function (filePath, remainRootDir) {
130130
fs.rmdirSync(filePath);
131131
}
132132
}
133+
};
134+
135+
exports.getFileSize = function (filename) {
136+
var stats = fs.statSync(filename);
137+
138+
return stats['size'] > 1024 ? (stats['size'] / 1024).toFixed(2) + ' KB' : stats['size'] + ' Bytes';
133139
};

src/commands/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ exports.run = (options) => {
103103
let contentLength = res._contentLength || '';
104104
if (contentLength) {
105105
contentLength = contentLength > 1024
106-
? (contentLength / 1024).toFixed(2) + 'kB '
107-
: contentLength + 'bytes ';
106+
? (contentLength / 1024).toFixed(2) + 'KB '
107+
: contentLength + 'Bytes ';
108108
}
109109

110110
format = format.replace(/%date/g, '\x1b[90m' + '[' + (moment().format(dateFormat)) + ']' + '\x1b[0m');

src/models/Project.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,16 @@ class Project {
392392

393393
const assetsInfo = self.config._config.assetsInfo || statsInfo.assets;
394394
assetsInfo.map((asset) => {
395-
const size = asset.size > 1024
396-
? (asset.size / 1024).toFixed(2) + ' kB'
397-
: asset.size + ' bytes';
395+
396+
let fileSize = UtilFs.getFileSize(path.resolve(cwd, asset.name));
397+
if(!fileSize) {
398+
fileSize = asset.size > 1024
399+
? (asset.size / 1024).toFixed(2) + ' KB'
400+
: asset.size + ' Bytes';
401+
}
402+
398403
if (!/\.cache$/.test(asset.name)) {
399-
log('- '.gray + asset.name + ' - ' + size);
404+
log('- '.gray + asset.name + ' - ' + fileSize);
400405
}
401406
});
402407

src/utils/fs.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,11 @@ exports.deleteFolderRecursive = function(filePath, remainRootDir) {
108108
}
109109
}
110110
};
111+
112+
exports.getFileSize = function(filename) {
113+
const stats = fs.statSync(filename);
114+
115+
return stats['size'] > 1024
116+
? (stats['size'] / 1024).toFixed(2) + ' KB'
117+
: stats['size'] + ' Bytes';
118+
};

0 commit comments

Comments
 (0)