Skip to content

Commit

Permalink
refactor(CLI): Improve file size output in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Oct 27, 2021
1 parent 26846d5 commit 4448490
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/aws/deployFunction.js
Expand Up @@ -6,7 +6,7 @@ const path = require('path');
const fs = require('fs');
const wait = require('timers-ext/promise/sleep');
const validate = require('./lib/validate');
const filesize = require('filesize');
const filesize = require('../../utils/filesize');
const ServerlessError = require('../../serverless-error');
const { log, style, legacy, progress } = require('@serverless/utils/log');

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/aws/info/display.js
@@ -1,7 +1,7 @@
'use strict';

const chalk = require('chalk');
const filesize = require('filesize');
const filesize = require('../../../utils/filesize');
const { legacy, isVerboseMode, style } = require('@serverless/utils/log');

module.exports = {
Expand Down
13 changes: 13 additions & 0 deletions lib/utils/filesize.js
@@ -0,0 +1,13 @@
'use strict';

const ensureNaturalNumber = require('type/natural-number/ensure');
const filesize = require('filesize');

const resolveSignificant = (size) => {
return size >= 1000 ? resolveSignificant(Math.floor(size / 1000)) : size;
};

module.exports = (size) =>
filesize(size, {
round: resolveSignificant(ensureNaturalNumber(size, { name: 'size' })) >= 9 ? 0 : 1,
});
42 changes: 42 additions & 0 deletions test/unit/lib/utils/filesize.test.js
@@ -0,0 +1,42 @@
'use strict';

const { expect } = require('chai');
const filesize = require('../../../../lib/utils/filesize');

describe('test/unit/lib/utils/filesize.test.js', () => {
it('should display sizes below 1kb literally', () => {
expect(filesize(1)).to.equal('1 B');
expect(filesize(10)).to.equal('10 B');
expect(filesize(12)).to.equal('12 B');
expect(filesize(100)).to.equal('100 B');
expect(filesize(123)).to.equal('123 B');
expect(filesize(987)).to.equal('987 B');
});

it('expect to display round values without decimals', () => {
expect(filesize(1000)).to.equal('1 kB');
expect(filesize(3000)).to.equal('3 kB');
expect(filesize(1000 * 1000)).to.equal('1 MB');
expect(filesize(4000 * 1000)).to.equal('4 MB');
expect(filesize(1000 * 1000 * 1000)).to.equal('1 GB');
expect(filesize(4000 * 1000 * 1000)).to.equal('4 GB');
});

it('expect to display not round values below 9 with decimals', () => {
expect(filesize(1123)).to.equal('1.1 kB');
expect(filesize(8123)).to.equal('8.1 kB');
expect(filesize(1234848)).to.equal('1.2 MB');
expect(filesize(8123494)).to.equal('8.1 MB');
expect(filesize(1123484848)).to.equal('1.1 GB');
expect(filesize(8123494934)).to.equal('8.1 GB');
});

it('expect to display not round values above 9 without decimals', () => {
expect(filesize(12123)).to.equal('12 kB');
expect(filesize(9123)).to.equal('9 kB');
expect(filesize(12234848)).to.equal('12 MB');
expect(filesize(9234949)).to.equal('9 MB');
expect(filesize(12348484848)).to.equal('12 GB');
expect(filesize(9349493432)).to.equal('9 GB');
});
});

0 comments on commit 4448490

Please sign in to comment.