Skip to content

Commit a9e67d8

Browse files
author
Nathan Mahdavi
committed
add cli test and small refactor
1 parent 925e801 commit a9e67d8

File tree

6 files changed

+73
-29
lines changed

6 files changed

+73
-29
lines changed

__tests__/cli.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const program = require('commander');
2+
3+
const cli = require('../cli');
4+
5+
describe('entry', () => {
6+
7+
beforeEach(() => {
8+
jest.spyOn(program, 'option');
9+
jest.spyOn(program, 'parse').mockReturnValue();
10+
});
11+
12+
afterEach(() => {
13+
jest.restoreAllMocks();
14+
});
15+
16+
test('all of the correct options are defined', () => {
17+
const commands = [
18+
['a, add <alias>', 'Add user'],
19+
['r, remove <alias>', 'Remove user'],
20+
['u, use <alias>', 'Use user'],
21+
['l, list', 'List users'],
22+
['w, whoami', 'Current logged in user']
23+
];
24+
25+
cli();
26+
27+
commands.forEach((command) => {
28+
expect(program.option).toHaveBeenCalledWith(...command);
29+
});
30+
});
31+
32+
});

cli.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const program = require('commander');
2+
const fs = require('fs');
3+
4+
function getMappedCommands() {
5+
return fs.readdirSync('./commands').reduce((commands, command) => {
6+
const commandWithoutExtension = command.replace('.js', '');
7+
8+
return Object.assign(commands, {
9+
[commandWithoutExtension]: require(`./commands/${command}`)
10+
});
11+
}, {});
12+
}
13+
14+
function findAndExecuteOption(commands) {
15+
Object.keys(commands).forEach((command) => {
16+
const commandCallback = commands[command];
17+
const commandValue = program[command];
18+
const commandWasCalled = program.hasOwnProperty(command);
19+
20+
if (commandWasCalled) {
21+
commandCallback(commandValue);
22+
}
23+
});
24+
}
25+
26+
module.exports = (args = process.argv) => {
27+
program
28+
.option('a, add <alias>', 'Add user')
29+
.option('r, remove <alias>', 'Remove user')
30+
.option('u, use <alias>', 'Use user')
31+
.option('l, list', 'List users')
32+
.option('w, whoami', 'Current logged in user')
33+
.parse(args);
34+
35+
findAndExecuteOption(getMappedCommands());
36+
};

commands/add.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module.exports = (alias) => {
99
throw new Error('This user already exists. Use `npm-users list` and `npm-users remove <alias>` to resolve.');
1010
}
1111

12-
const loginProcess = child_process.spawnSync('npm', ['login'], { stdio: 'inherit' });
13-
if (loginProcess.status !== 0) {
12+
const { status: loginStatusCode } = child_process.spawnSync('npm', ['login'], { stdio: 'inherit' });
13+
if (loginStatusCode !== 0) {
1414
throw new Error('We were unable to add this login.');
1515
}
1616

commands/remove.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const fs = require('fs');
33

44
module.exports = (alias) => {
55
const targetUserFile = directories.getNpmUsersHomeDirectory() + alias;
6-
const userNotFound = !fs.existsSync(targetUserFile);
6+
const userNotFound = fs.existsSync(targetUserFile) === false;
77
if (userNotFound) {
88
throw new Error('The specified user could not be found.');
99
}

commands/use.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const directories = require('../directories');
33

44
module.exports = (alias) => {
55
const targetUserFile = directories.getNpmUsersHomeDirectory() + alias;
6-
const userNotFound = !fs.existsSync(targetUserFile);
6+
const userNotFound = fs.existsSync(targetUserFile) === false;
77
if (userNotFound) {
88
throw new Error('The specified user could not be found.');
99
}

index.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,4 @@
11
#!/usr/bin/env node
22

3-
const program = require('commander');
4-
const fs = require('fs');
5-
63
require('./setup')();
7-
8-
program
9-
.option('a, add <alias>', 'Add user')
10-
.option('r, remove <alias>', 'Remove user')
11-
.option('u, use <alias>', 'Use user')
12-
.option('l, list', 'List users')
13-
.option('w, whoami', 'Current logged in user')
14-
.parse(process.argv);
15-
16-
17-
const commands = fs.readdirSync('./commands').reduce((commands, command) => {
18-
const commandWithoutExtension = command.replace('.js', '');
19-
return Object.assign(commands, {
20-
[commandWithoutExtension]: require(`./commands/${command}`)
21-
});
22-
}, {});
23-
24-
Object.keys(commands).forEach((command) => {
25-
if (program[command]) {
26-
commands[command](program[command]);
27-
}
28-
});
4+
require('./cli.js')();

0 commit comments

Comments
 (0)