Skip to content

Commit

Permalink
feat: add cli command size (#215)
Browse files Browse the repository at this point in the history
Instead of `nft build index.js && du -sh dist`, this introduces a new command: `nft size index.js`.

I also updated the CLI output to be a little friendlier:

```
△ nft 0.12.2

Usage:

  $ nft [command] <file>

Commands:

  build    trace and copy to the dist directory
  print    trace and print to stdout
   size    trace and print size in bytes
```
  • Loading branch information
styfle authored Jun 1, 2021
1 parent 2b97d7d commit 05995af
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
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`)
} 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>');
});

0 comments on commit 05995af

Please sign in to comment.