Skip to content

Commit

Permalink
CLI now use the provided --directory & fixed a problem with CLI errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dlmr committed Oct 17, 2016
1 parent 68dc8a2 commit 0b17ceb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 67 deletions.
21 changes: 10 additions & 11 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ const updateNotifier = require('update-notifier');

const packageJSON = require('../package.json');
const fileExists = require('../lib/helpers').fileExists;
const getAbsolutePath = require('../lib/helpers').getAbsolutePath;
const processArguments = require('../lib/cli/processArguments').default;

const currentCli = require.resolve('./cli');
const args = processArguments();
const directory = getAbsolutePath(args.coreOptions.directory || args.coreOptions.d || process.cwd());

const localCli = path.resolve(process.cwd(), 'node_modules/roc/bin/cli');
const currentCli = require.resolve('./cli');
const localCli = path.resolve(directory, 'node_modules/roc/bin/cli');

if (currentCli !== localCli) {
// Are we global?
if (!fileExists(path.resolve(process.cwd(), 'node_modules/.bin/roc'))) {
if (!fileExists(path.resolve(directory, 'node_modules/.bin/roc'))) {
updateNotifier({ pkg: packageJSON }).notify({ defer: false });
require(currentCli); // eslint-disable-line
} else {
// Will try to use the local CLI over the global one
try {
const cli = require.resolve(localCli);
console.log('Found a local version of Roc, will use that over the global one.', '\n');
require(cli); // eslint-disable-line
} catch (err) {
console.log('An error happened when trying to load local CLI, will use the global one.', '\n');
}
const cli = require.resolve(localCli);
console.log('Found a local version of Roc, will use that over the global one.', '\n');
require(cli); // eslint-disable-line
}
} else {
require(currentCli); // eslint-disable-line
Expand Down
35 changes: 35 additions & 0 deletions src/cli/processArguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import minimist from 'minimist';

export default function parseArguments(argv = process.argv) {
/* eslint-disable object-property-newline */
const {
_,
h, help,
V, verbose,
v, version,
c, config,
d, directory,
b, 'better-feedback': betterFeedback,
'--': extraArguments,
...extOptions,
} = minimist(argv.slice(2), { '--': true });

// The first should be our command or commandgroup, if there is one
const [groupOrCommand, ...argsWithoutOptions] = _;

return {
groupOrCommand, // commandgroup or command
coreOptions: { // options managed and parsed by core
h, help,
V, verbose,
v, version,
c, config,
d, directory,
b, betterFeedback,
},
extOptions, // options that will be forwarded to commands from context
argsWithoutOptions, // remaining arguments with no associated options
extraArguments, // arguments after the ended argument list (--)
};
/* eslint-enable object-property-newline */
}
79 changes: 23 additions & 56 deletions src/cli/runCli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isString } from 'lodash';
import minimist from 'minimist';
import isPromise from 'is-promise';

import { appendSettings, getSettings } from '../configuration/manageSettings';
Expand All @@ -23,6 +22,7 @@ import generateCommandsDocumentation from './commands/documentation/generateComm
import getMappings from './commands/getMappings';
import parseArguments from './commands/parseArguments';
import parseOptions from './commands/parseOptions';
import processArguments from './processArguments';

/**
* Invokes the Roc cli.
Expand All @@ -35,7 +35,7 @@ export default function runCli({
argv = process.argv,
invoke = true,
}) {
const input = parseCliInput(minimist(argv.slice(2), { '--': true }));
const input = processArguments(argv);

// If version is selected output that and stop
if (input.coreOptions.version || input.coreOptions.v) {
Expand Down Expand Up @@ -181,68 +181,35 @@ export default function runCli({
cwd: dirPath,
}).catch((error) => {
process.exitCode = error.getCode ? error.getCode() : 1;
log.small.error('A problem happened when running the Roc command');
log.small.error('An error happened when running command');
});
}

const parsedArguments = parseArguments(commandData.commandName, commandData.commands, input.argsWithoutOptions);

// Run the command
const commandResult = command.command({
info,
arguments: parsedArguments,
options: parsedOptions,
extraArguments: input.extraArguments,
// Roc Context
context,
});

if (isPromise(commandResult)) {
return commandResult
.catch((error) => {
log.small.warn('A problem happened when running the Roc command', error);
});
}
try {
const commandResult = command.command({
info,
arguments: parsedArguments,
options: parsedOptions,
extraArguments: input.extraArguments,
// Roc Context
context,
});

return commandResult;
if (isPromise(commandResult)) {
return commandResult
.catch((error) => {
log.small.warn('A problem happened when running command', error);
});
}

return commandResult;
} catch (error) {
log.small.error('An error happened when running command', error);
}
}

return undefined;
}

/**
* Wrap all relevant data from minimist with descriptive names
*/
function parseCliInput(minimistData) {
/* eslint-disable object-property-newline */
const {
_,
h, help,
V, verbose,
v, version,
c, config,
d, directory,
b, 'better-feedback': betterFeedback,
'--': extraArguments,
...extOptions,
} = minimistData;

// The first should be our command or commandgroup, if there is one
const [groupOrCommand, ...argsWithoutOptions] = _;

return {
groupOrCommand, // commandgroup or command
coreOptions: { // options managed and parsed by core
h, help,
V, verbose,
v, version,
c, config,
d, directory,
b, betterFeedback,
},
extOptions, // options that will be forwarded to commands from context
argsWithoutOptions, // remaining arguments with no associated options
extraArguments, // arguments after the ended argument list (--)
};
/* eslint-enable object-property-newline */
}

0 comments on commit 0b17ceb

Please sign in to comment.