Skip to content

Commit

Permalink
disallow camelised command names - fixes #1530
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian McKenzie committed Nov 1, 2016
1 parent f057b8e commit 8eccdbc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
10 changes: 4 additions & 6 deletions src/cli/commands/_build-sub-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import type {Reporter} from '../../reporters/index.js';
import type Config from '../../config.js';
import type {CLIFunction} from '../../types.js';
import {MessageError} from '../../errors.js';

const camelCase = require('camelcase');
import {camelCase, hyphenate} from '../../util/misc.js';

type SubCommands = {
[commandName: string]: CLIFunction,
Expand All @@ -20,7 +19,7 @@ type Return = {
type Usage = Array<string>;

export default function(rootCommandName: string, subCommands: SubCommands, usage?: Usage = []): Return {
const subCommandNames = Object.keys(subCommands);
const subCommandNames = Object.keys(subCommands).map(hyphenate);

function setFlags(commander: Object) {
commander.usage(`${rootCommandName} [${subCommandNames.join('|')}] [flags]`);
Expand All @@ -32,9 +31,8 @@ export default function(rootCommandName: string, subCommands: SubCommands, usage
flags: Object,
args: Array<string>,
): Promise<void> {
const subName: string = camelCase(args.shift() || '');
const isValidCommand = subName && subCommandNames.indexOf(subName) >= 0;
if (isValidCommand) {
const subName: ?string = camelCase(args.shift() || '');
if (subName && subCommands[subName]) {
const command: CLIFunction = subCommands[subName];
const res = await command(config, reporter, flags, args);
if (res !== false) {
Expand Down
20 changes: 13 additions & 7 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import * as network from '../util/network.js';
import {MessageError} from '../errors.js';
import aliases from './aliases.js';
import Config from '../config.js';
import {hyphenate, camelCase} from '../util/misc.js';

const camelCase = require('camelcase');
const chalk = require('chalk');
const commander = require('commander');
const fs = require('fs');
Expand Down Expand Up @@ -80,11 +80,10 @@ commander.option(
);

// get command name
let commandName: string = args.shift() || '';
let commandName: ?string = args.shift() || '';
let command;

//
const hyphenate = (string) => string.replace(/[A-Z]/g, (match) => ('-' + match.charAt(0).toLowerCase()));
const getDocsLink = (name) => `https://yarnpkg.com/en/docs/cli/${name || ''}`;
const getDocsInfo = (name) => 'Visit ' + chalk.bold(getDocsLink(name)) + ' for documentation about this command.';

Expand Down Expand Up @@ -138,7 +137,12 @@ if (commandName === 'help' && args.length) {

//
invariant(commandName, 'Missing command name');
command = command || commands[camelCase(commandName)];
if (!command) {
const camelised = camelCase(commandName);
if (camelised) {
command = commands[camelised];
}
}

//
if (command && typeof command.setFlags === 'function') {
Expand Down Expand Up @@ -378,9 +382,11 @@ config.init({
onUnexpectedError(err);
}

const actualCommandForHelp = commands[commandName] ? commandName : aliases[commandName];
if (actualCommandForHelp) {
reporter.info(getDocsInfo(actualCommandForHelp));
if (commandName) {
const actualCommandForHelp = commands[commandName] ? commandName : aliases[commandName];
if (command && actualCommandForHelp) {
reporter.info(getDocsInfo(actualCommandForHelp));
}
}

process.exit(1);
Expand Down
16 changes: 16 additions & 0 deletions src/util/misc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* @flow */

const _camelCase = require('camelcase');

export function sortAlpha(a: string, b: string): number {
// sort alphabetically
return a.toLowerCase().localeCompare(b.toLowerCase());
Expand Down Expand Up @@ -38,3 +40,17 @@ export function addSuffix(pattern: string, suffix: string): string {

return pattern;
}

export function hyphenate(str: string): string {
return str.replace(/[A-Z]/g, (match) => {
return '-' + match.charAt(0).toLowerCase();
});
}

export function camelCase(str: string): ?string {
if (/[A-Z]/.test(str)) {
return null;
} else {
return _camelCase(str);
}
}

0 comments on commit 8eccdbc

Please sign in to comment.