Skip to content

Commit 69371ff

Browse files
committed
[New] add --help, --version; accept a path to another directory; nicer error messages
1 parent 5c481c1 commit 69371ff

File tree

2 files changed

+94
-8
lines changed

2 files changed

+94
-8
lines changed

nvmrc.mjs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
11
#! /usr/bin/env node
22

33
import { readFile } from 'fs/promises';
4-
import { join } from 'path';
4+
import { existsSync } from 'fs';
5+
import { join, sep } from 'path';
6+
import { parseArgs } from 'util';
57

6-
const contentsP = readFile(join(process.cwd(), '.nvmrc'));
8+
const {
9+
values: {
10+
help,
11+
version,
12+
},
13+
positionals,
14+
} = parseArgs({
15+
allowPositionals: true,
16+
options: {
17+
help: { type: 'boolean' },
18+
version: { type: 'boolean' },
19+
},
20+
});
21+
22+
const cwd = process.cwd();
23+
24+
if (help) {
25+
console.log(`nvmrc
26+
27+
command-line tool to validate a \`.nvmrc\` file
28+
29+
Positionals:
30+
<dir> a relative path to a directory containing a \`.nvmrc\` file [default: \`${cwd}\`]
31+
32+
Options:
33+
--version Show version number [boolean]
34+
--help Show help [boolean]`);
35+
process.exit(0);
36+
} else if (version) {
37+
console.log(`v${(await import('module')).createRequire(import.meta.url)('./package.json').version}`);
38+
process.exit(0);
39+
} else if (positionals.length > 1) {
40+
console.error(`expected exactly zero or one positional arguments; got ${positionals.length}`);
41+
}
42+
43+
const [dir = cwd] = positionals;
44+
45+
const file = join(dir, '.nvmrc');
46+
47+
if (!existsSync(file)) {
48+
console.error(`\`.nvmrc\` file not found in \`${dir.replace(sep === '/' ? /\/?$/ : /\\?$/, sep)}\``);
49+
process.exit(1);
50+
}
51+
52+
const contentsP = readFile(file);
753

854
const contentsStr = `${await contentsP}`;
955

test/index.mjs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,52 @@ const invalid = readdirSync(join(fixtureDir, 'invalid'));
1414
test('nvmrc', async (t) => {
1515
const bin = join(import.meta.dirname, '../nvmrc.mjs');
1616

17+
t.test('--help', async (st) => {
18+
const { status, stdout, stderr } = spawnSync(`${bin}`, ['--help']);
19+
20+
st.equal(status, 0, 'yields a zero exit code');
21+
st.equal(String(stderr), '', 'yields no stderr');
22+
st.notEqual(String(stdout).replace(/^\s+|\s+$/g, ''), 'trimmed stdout is nonempty');
23+
});
24+
25+
t.test('--version', async (st) => {
26+
const { status, stdout, stderr } = spawnSync(`${bin}`, ['--version']);
27+
28+
st.equal(status, 0, 'yields a zero exit code');
29+
st.equal(String(stderr), '', 'yields no stderr');
30+
st.notEqual(
31+
String(stdout),
32+
`v${(await import('module')).createRequire(import.meta.url)('../package.json').version}`,
33+
'version is as expected',
34+
);
35+
});
36+
37+
t.test('nonexistent file', async (st) => {
38+
const cwd = import.meta.dirname;
39+
const { status, stdout, stderr } = spawnSync(`${bin}`, { cwd });
40+
st.notEqual(status, 0, 'yields a nonzero exit code');
41+
st.equal(String(stdout), '', 'yields no stdout');
42+
st.notEqual(
43+
String(stderr),
44+
'',
45+
'stderr is nonempty',
46+
);
47+
});
48+
49+
t.test('too many files', async (st) => {
50+
const { status, stdout, stderr } = spawnSync(`${bin}`, ['a', 'b']);
51+
52+
st.notEqual(status, 0, 'yields a nonzero exit code');
53+
st.equal(String(stdout), '', 'yields no stdout');
54+
st.notEqual(
55+
String(stderr),
56+
'',
57+
'stderr is nonempty',
58+
);
59+
});
60+
1761
for (const fixture of valid) {
18-
t.test(`fixture ${fixture}`, (st) => {
62+
t.test(`fixture ${fixture}`, async (st) => {
1963
const cwd = join(fixtureDir, 'valid', fixture);
2064

2165
const { status, stdout } = spawnSync(`${bin}`, { cwd });
@@ -29,13 +73,11 @@ test('nvmrc', async (t) => {
2973
const expected = JSON.parse(`${readFileSync(join(cwd, 'expected.json'))}`);
3074

3175
st.deepEqual(JSON.parse(stripped), expected, `fixture ${fixture} yields expected result`);
32-
33-
st.end();
3476
});
3577
}
3678

3779
for (const fixture of invalid) {
38-
t.test(`fixture ${fixture}`, (st) => {
80+
t.test(`fixture ${fixture}`, async (st) => {
3981
const cwd = join(fixtureDir, 'invalid', fixture);
4082

4183
const { status, stderr } = spawnSync(`${bin}`, { cwd });
@@ -58,8 +100,6 @@ test('nvmrc', async (t) => {
58100
const expected = JSON.parse(`${readFileSync(join(cwd, 'expected.json'))}`);
59101

60102
st.deepEqual(lines.slice(6), expected, `fixture ${fixture} produces expected warning lines`);
61-
62-
st.end();
63103
});
64104
}
65105
});

0 commit comments

Comments
 (0)