Skip to content

Commit

Permalink
feat(run-npm-scripts): allow multiple scriptNames
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mollweide committed Aug 8, 2018
1 parent 448f351 commit abbf9dc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# lerna-terminal [![build status](https://img.shields.io/travis/smollweide/lerna-terminal/master.svg)](https://travis-ci.org/smollweide/lerna-terminal)

> Powerful cli ui for lerna - replace `lerna run <scriptName>` with `lerna-terminal <scriptName>`.
> Powerful cli ui for lerna - replace `lerna run <scriptName>` with `lerna-terminal <scriptNames>`.
![kapture 2017-11-02 at 17 25 41](https://user-images.githubusercontent.com/2912007/32337702-08be4f34-bff3-11e7-808b-54c9bd09a0c9.gif)

Expand All @@ -20,7 +20,7 @@ $ npm install lerna-terminal
$ lerna-terminal --help
Usage
$ lerna-terminal <scriptName> [options]
$ lerna-terminal <scriptNames> [options]
Options
-V, --version Output the version number
Expand Down
8 changes: 4 additions & 4 deletions src/commander/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const packageData = require('../../package.json');
function runCommander() {
program
.version(packageData.version)
.arguments('<scriptName>')
.action(scriptName => {
program.script = scriptName;
.arguments('<scriptNames>')
.action(scriptNames => {
program.scripts = scriptNames.split(',');
})
.option('-i, --ignoredPackages [string]', 'Add packages which should be ignored')
.option('-f, --focus [string]', 'Focus one subterminal initially')
Expand All @@ -19,7 +19,7 @@ function runCommander() {
.parse(process.argv);

/* istanbul ignore next */
if (!program.script) {
if (!program.scripts || !Array.isArray(program.scripts) || program.scripts.length < 1) {
throw new Error('--script is required');
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
const { runCommander } = require('./commander');
const { resizeListener } = require('./getDimensions');
const { provideStore } = require('./store');
const { getProgram } = require('./commander');
const commandListener = require('./commandListener');
const runNpmScripts = require('./runNpmScripts');
const render = require('./render');
const executeCmd = require('./executeCmd');
const handleKillProcess = require('./handleKillProcess');
const program = getProgram();

runCommander();
provideStore();
runNpmScripts();
program.scripts.forEach(scriptName => {
runNpmScripts(scriptName);
});
resizeListener(render);
commandListener(executeCmd);
handleKillProcess();
11 changes: 6 additions & 5 deletions src/runNpmScripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ const parseText = text => {
};

/**
* @param {string} scriptName - npm script name
* @returns {Object} state
**/
function runNpmScripts() {
function runNpmScripts(scriptName) {
const commands = getScriptCommands();
const program = getProgram();
const state = getState();

if (!commands[program.script]) {
if (!commands[scriptName]) {
throw new Error("the given script wasn't found!");
}

Object.keys(commands[program.script]).forEach(index => {
const packagePath = commands[program.script][index];
Object.keys(commands[scriptName]).forEach(index => {
const packagePath = commands[scriptName][index];
const packageName = path.basename(packagePath);
if (isIgnoredPackage(packagePath, program.ignoredPackages)) {
return;
Expand All @@ -38,7 +39,7 @@ function runNpmScripts() {
log: [],
};
state[packageName].terminal = runNpmScript({
scriptName: program.script,
scriptName,
packagePath,
onRecieve(text) {
if (text.search('\x1Bc') >= 0) {
Expand Down
16 changes: 8 additions & 8 deletions src/runNpmScripts/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ describe('runNpmScripts', () => {
getScriptCommands.mockImplementation(() => ({}));
getProgram.mockImplementation(() => ({ script: 'start' }));
expect(() => {
runNpmScripts();
runNpmScripts('start');
}).toThrow();
});
it('packages ignored', () => {
basename.mockImplementation(_path => _path.split('/')[4]);
getScriptCommands.mockImplementation(() => ({ start: ['/path/to/package/utils', '/path/to/package/ui'] }));
getProgram.mockImplementation(() => ({ script: 'start' }));
isIgnoredPackage.mockImplementation(() => true);
runNpmScripts();
runNpmScripts('start');
});
it('packages in state', () => {
getState.mockImplementation(() => ({}));
getScriptCommands.mockImplementation(() => ({ start: ['/path/to/package/utils', '/path/to/package/ui'] }));
isIgnoredPackage.mockImplementation(() => false);
basename.mockImplementation(_path => _path.split('/')[4]);
runNpmScript.mockImplementation(() => 'terminal');
expect(runNpmScripts()).toEqual({
expect(runNpmScripts('start')).toEqual({
ui: { log: [], terminal: 'terminal' },
utils: { log: [], terminal: 'terminal' },
});
Expand All @@ -67,7 +67,7 @@ describe('runNpmScripts', () => {
onRecieve('test\n');
onRecieve('test2');
});
expect(runNpmScripts().utils.log).toEqual(['test', '', 'test2']);
expect(runNpmScripts('start').utils.log).toEqual(['test', '', 'test2']);
});
it('package.terminal.onError -> update log', () => {
getState.mockImplementation(() => ({}));
Expand All @@ -81,7 +81,7 @@ describe('runNpmScripts', () => {
onError('test\n');
onError('test2');
});
expect(runNpmScripts().utils.log).toEqual(['test', '', 'test2']);
expect(runNpmScripts('start').utils.log).toEqual(['test', '', 'test2']);
});
it('package.terminal.onExit -> update log', () => {
getState.mockImplementation(() => ({}));
Expand All @@ -94,7 +94,7 @@ describe('runNpmScripts', () => {
runNpmScript.mockImplementation(({ onExit }) => {
onExit();
});
expect(runNpmScripts().utils.log).toEqual(['stop: utils']);
expect(runNpmScripts('start').utils.log).toEqual(['stop: utils']);
});
it('package.terminal.onRecieve \x1Bc -> clear log', () => {
getState.mockImplementation(() => ({}));
Expand All @@ -107,7 +107,7 @@ describe('runNpmScripts', () => {
runNpmScript.mockImplementation(({ onRecieve }) => {
onRecieve('\x1Bc');
});
expect(runNpmScripts().utils.log).toEqual(['']);
expect(runNpmScripts('start').utils.log).toEqual(['']);
});
it('package.terminal.onError \x1Bc -> clear log', () => {
getState.mockImplementation(() => ({}));
Expand All @@ -120,6 +120,6 @@ describe('runNpmScripts', () => {
runNpmScript.mockImplementation(({ onError }) => {
onError('\x1Bc');
});
expect(runNpmScripts().utils.log).toEqual(['']);
expect(runNpmScripts('start').utils.log).toEqual(['']);
});
});

0 comments on commit abbf9dc

Please sign in to comment.