Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cli command size #215

Merged
merged 3 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env node

import { join, dirname } from 'path';
import { promises } from 'fs';
import { promises, statSync, lstatSync } from 'fs';
const { copyFile, mkdir } = promises;
const rimraf = require('rimraf');
import { nodeFileTrace } from './node-file-trace';


async function cli(
action = process.argv[2],
files = process.argv.slice(3),
Expand All @@ -15,7 +16,7 @@ async function cli(
const opts = {
ts: true,
mixedModules: true,
log: true
log: action !== 'size'
};

const { fileList, esmFileList, warnings } = await nodeFileTrace(files, opts);
Expand All @@ -39,8 +40,31 @@ async function cli(
await mkdir(dir, { recursive: true });
await copyFile(src, dest);
}
} else if (action === 'size') {
const isSymbolicLink = (m: number) => (m & 61440) === 40960;
let bytes = 0;
for (const f of allFiles) {
const lstat = lstatSync(f);
if (isSymbolicLink(lstat.mode)) {
bytes += lstat.size;
} else {
const stat = statSync(f)
bytes += stat.size;
}
}
stdout.push(`${bytes} bytes total`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be preferable to make this KB by default, or choose between bytes or not. Here's an example function - https://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable-string/14919494#14919494.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could potentially leverage pretty-bytes here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started going down that road but I wasn't sure if we wanted to use base 2 (1024) or base 10 (1000) to mean 1KB (I'll need to verify how the API does it) so I left that for a follow up PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps at least use numeric separators with a standard format like 123,456,789B?

} else {
stdout.push('△ nft - provide an action such as `nft build` or `nft print`.');
stdout.push(`△ nft ${require('../package.json').version}`);
stdout.push('');
stdout.push('Usage:');
stdout.push('');
stdout.push(` $ nft [command] <file>`);
stdout.push('');
stdout.push('Commands:');
stdout.push('');
stdout.push(' build trace and copy to the dist directory');
stdout.push(' print trace and print to stdout');
stdout.push(' size trace and print size in bytes');
}
return stdout.join('\n');
}
Expand Down
20 changes: 18 additions & 2 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ it('should correctly build dist from cli', async () => {
expect(found).toBe(true);
});

it('should correctly show size from cli', async () => {
const { stderr, stdout } = await exec(`node ../out/cli.js size ${inputjs}`, { cwd: __dirname });
if (stderr) {
throw new Error(stderr);
}
expect(stdout).toMatch('bytes total');
});

it('should correctly print help when unknown action is used', async () => {
const { stderr, stdout } = await exec(`node ../out/cli.js unknown ${inputjs}`, { cwd: __dirname });
if (stderr) {
throw new Error(stderr);
}
expect(stdout).toMatch('provide an action');
expect(stdout).toMatch('$ nft [command] <file>');
});

it('[codecov] should correctly print trace from required cli', async () => {
Expand All @@ -57,10 +65,18 @@ it('[codecov] should correctly build dist from required cli', async () => {
expect(found).toBe(true);
});

it('[codecov] should correctly show size in bytes from required cli', async () => {
// This test is only here to satisfy code coverage
const cli = require('../out/cli.js')
const files = [join(__dirname, inputjs)];
const stdout = await cli('size', files);
expect(stdout).toMatch('bytes total');
});

it('[codecov] should correctly print help when unknown action is used', async () => {
// This test is only here to satisfy code coverage
const cli = require('../out/cli.js')
const files = [join(__dirname, inputjs)];
const stdout = await cli('unknown', files);
expect(stdout).toMatch('provide an action');
expect(stdout).toMatch('$ nft [command] <file>');
});