Skip to content

Commit

Permalink
feat(command-listener): add autocomplete on tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mollweide committed May 4, 2018
1 parent 0eb9969 commit 717a405
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/commandListener/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
'use strict';
const path = require('path');
const keypress = require('keypress');
const chalk = require('chalk');
const { getUiState } = require('../store');
const getLernaPackages = require('../getLernaPackages');
const { getProgram } = require('../commander');
const getPackage = require('../getPackage');
const { COMMANDS } = require('../executeCmd');

keypress(process.stdin);

Expand Down Expand Up @@ -71,6 +77,39 @@ const onReturn = onCommandEntered => {
getUiState().onChange('');
};

const onTab = () => {
const availablePackages = Object.values(COMMANDS);

getLernaPackages(packagePath => {
// eslint-disable-next-line
const packageData = getPackage(path.join(packagePath, 'package.json'));
if (packageData && packageData.scripts && packageData.scripts[getProgram().script]) {
availablePackages.push(path.basename(packagePath));
}
});

const filteredPackages = availablePackages.filter(
availablePackage => new RegExp(`^${buffer}`).exec(availablePackage) !== null
);

if (filteredPackages.length === 1) {
buffer = filteredPackages[0];
getUiState().onChange(filteredPackages[0]);
return;
}

if (filteredPackages.length > 1) {
const originalBuffer = buffer;
buffer = `commands: ${chalk.blue(filteredPackages.join(', '))}`;
getUiState().onChange(`commands: ${chalk.blue(filteredPackages.join(', '))}`);

setTimeout(() => {
buffer = originalBuffer;
getUiState().onChange(originalBuffer);
}, 1500);
}
};

const onRecieveLetter = letter => {
if (!letter) {
return;
Expand All @@ -84,6 +123,7 @@ const keyNameMap = {
up: onUp,
down: onDown,
return: onReturn,
tab: onTab,
};

/**
Expand Down
107 changes: 107 additions & 0 deletions src/commandListener/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@
/* eslint global-require: 0*/
const commandListener = require('./index');
const { getUiState } = require('../store');
const getLernaPackages = require('../getLernaPackages');
const { getProgram } = require('../commander');
const getPackage = require('../getPackage');

jest.mock('../store');
jest.mock('../getLernaPackages');
jest.mock('../commander');
jest.mock('../getPackage');

getLernaPackages.mockImplementation(onMatch => {
onMatch('path/a-package');
onMatch('path/b-package');
onMatch('path/s-package');
});
getProgram.mockImplementation(() => ({
script: 'start',
}));

const _process = {
env: global.process.env,
Expand Down Expand Up @@ -47,6 +62,14 @@ const addBackspace = () => [
},
];

const addTab = () => [
'',
{
ctrl: false,
name: 'tab',
},
];

const addUp = () => [
'',
{
Expand All @@ -63,6 +86,7 @@ const addDown = () => [
describe('commandListener', () => {
beforeEach(() => {
getUiState.mockClear();
getPackage.mockClear();
});
it('write result in buffer', done => {
getUiState.mockImplementation(() => ({
Expand Down Expand Up @@ -226,4 +250,87 @@ describe('commandListener', () => {
});
expect(commandListener(() => {})).toBe(undefined);
});
it('press tab - with a', done => {
let counter = 0;
getPackage.mockImplementation(() => ({
scripts: {
start: 'something',
},
}));
getUiState.mockImplementation(() => ({
onChange(value) {
counter += 1;
if (counter === 3) {
expect(value).toBe('a-package');
}
},
}));
global.process = Object.assign(_process, {
stdin: Object.assign(_process.stdin, {
on: (value, cb) => {
cb(...addReturn());
cb(...addString('a'));
cb(...addTab());
done();
},
}),
});
expect(commandListener(() => {})).toBe(undefined);
});
it('press tab - invalid package data', done => {
let counter = 0;
getPackage.mockImplementation(() => ({
scripts: {},
}));
getUiState.mockImplementation(() => ({
onChange(value) {
counter += 1;
if (counter === 3) {
expect(value).toBe('a');
}
},
}));
global.process = Object.assign(_process, {
stdin: Object.assign(_process.stdin, {
on: (value, cb) => {
cb(...addReturn());
cb(...addString('a'));
cb(...addTab());
done();
},
}),
});
expect(commandListener(() => {})).toBe(undefined);
});
it('press tab - multiple commands available', done => {
let counter = 0;
getPackage.mockImplementation(() => ({
scripts: {
start: 'something',
},
}));
getUiState.mockImplementation(() => ({
onChange(value) {
counter += 1;
if (counter === 3) {
expect(value.search('start, stop, s-package') >= 0).toBe(true);
}
// reset after 1500 ms
if (counter === 4) {
expect(value).toBe('s');
done();
}
},
}));
global.process = Object.assign(_process, {
stdin: Object.assign(_process.stdin, {
on: (value, cb) => {
cb(...addReturn());
cb(...addString('s'));
cb(...addTab());
},
}),
});
expect(commandListener(() => {})).toBe(undefined);
});
});
1 change: 1 addition & 0 deletions src/executeCmd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ function executeCmd(cmd) {
}

module.exports = executeCmd;
module.exports.COMMANDS = COMMANDS;

0 comments on commit 717a405

Please sign in to comment.