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

help command #9

Merged
merged 5 commits into from
Jul 9, 2024
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
33 changes: 33 additions & 0 deletions src/cli/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const help_text = `

pkg-fence

Reading the NPM lockfile from stdin and filtering pkg name by rules.
Exit code: 0 for empty results, 1 for anything matched.



Usage:

cat package-lock.json | pkg-fence <cmd>

npm shrinkwrap && cat npm-shrinkwrap.json | pkg-fence <cmd>



Builtin Presets:

--lodash

--nolyfill



Options:

--extra foo --extra bar --extra=abc,def

--ignore foo --ignore bar --ignore=abc,def

`;

20 changes: 18 additions & 2 deletions src/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { argv, stdin, exit } from 'node:process';

import type { Fn } from '../common.ts';
import { collect, type Flags } from '../collect.ts';
import { help_text } from './help.ts';



Expand Down Expand Up @@ -52,20 +53,35 @@ export function parse (args: Iterable<string>): Flags {
export async function main ({

args = argv.slice(2),
lines = createInterface(stdin),
input = stdin,
lines: optional_lines,
print = console.log,
quit = exit,

}: {

args?: Iterable<string>,
input?: NodeJS.ReadableStream,
lines?: AsyncIterable<string>,
print?: Fn<unknown, void>,
// deno-lint-ignore no-explicit-any
print?: Fn<any, void>,
quit?: Fn<number, void>,

} = {}): Promise<void> {

{ // -h, --help

const [ cmd ] = Array.from(args);

if (cmd == null || cmd === '-h' || cmd === '--help') {
print(help_text);
return quit(0);
}

}

const flags = parse(args);
const lines = optional_lines ?? createInterface({ input });

let code = 0;

Expand Down
52 changes: 52 additions & 0 deletions tests/cli/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { describe, it } from '@std/testing/bdd';
import * as asserts from '@std/assert';
import * as mock from '@std/testing/mock';

import { Readable } from 'node:stream';

import { make_lines } from '../utils.ts';

import {
Expand All @@ -15,6 +18,55 @@ import {

describe('main', function () {

for (const cmd of [ null, '-h', '--help' ]) {

it(`print help message on ${ cmd }`, async function () {

const args = cmd == null ? [] : [ cmd ];

const print = mock.spy(function (help: string) {
asserts.assertMatch(help, /Reading the NPM lockfile/);
});

const quit = mock.spy(() => {});

await main({ args, print, quit });

mock.assertSpyCalls(print, 1);

mock.assertSpyCallArg(quit, 0, 0, 0);
mock.assertSpyCalls(quit, 1);

});

}

it('ok in optional lines with input of ReadableStream', async function () {

const args = [ '--extra', 'acorn-jsx' ];

const input = Readable.from(`
"version": "1.2.3",
"lockfileVersion": 2,
"node_modules/lodash.memoize": {
"node_modules/acorn-jsx": {
"node_modules/side-channel": {
`);

const print = mock.spy(() => {});

const quit = mock.spy(() => {});

await main({ args, input, print, quit });

mock.assertSpyCallArg(print, 0, 0, 'acorn-jsx');
mock.assertSpyCalls(print, 1);

mock.assertSpyCallArg(quit, 0, 0, 1);
mock.assertSpyCalls(quit, 1);

});

it('exit by 0 with no matches', async function () {

const args = [ '--extra', 'wat' ];
Expand Down