Skip to content

Commit

Permalink
moved flaghelper to util
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaymathur committed Jan 30, 2018
1 parent dcee875 commit 0846073
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BoltError } from './utils/errors';
import cleanStack from 'clean-stack';
import * as commands from './commands';
import * as options from './utils/options';
import * as flagHelpers from './functions/flagHelper';
import * as flagHelpers from './utils/flagHelper';

const commandMap = {
ADD: { add: true },
Expand Down Expand Up @@ -101,7 +101,6 @@ const commandMap = {
};

function runCommandFromCli(args: options.Args, flags: options.Flags, pureArgs) {
console.log(flags);
let flagsArgs = flagHelpers.identifyFlags(pureArgs);
let { additionalArgs, updatedFlags } = flagHelpers.extractPossibleArgs(flags);

Expand Down
25 changes: 12 additions & 13 deletions src/functions/flagHelper.js → src/utils/flagHelper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// @flow
import * as options from './options';

const IS_BOOLEAN_FLAGS = {
const BOOLEAN_FLAGS = {
'--dev': true,
'--peerdev': true
'--peer': true
};

function isFlagWithValue(flag) {
Expand All @@ -14,28 +15,26 @@ function isFlag(maybeFlag) {
}

export function identifyFlags(args: Array<string> = []) {
let flags = [];
let flagsWithArgs = [];
args.forEach((arg, index) => {
if (isFlag(arg)) {
if (IS_BOOLEAN_FLAGS[arg]) {
flags.push(arg);
if (BOOLEAN_FLAGS[arg]) {
flagsWithArgs.push(arg);
} else {
if (isFlagWithValue(arg)) {
flags.push(arg);
} else {
flags.push(arg, args[++index]);
}
index < args.length - 1
? flagsWithArgs.push(`${arg}=${arg[++index]}`)
: flagsWithArgs.push(args);
}
}
});
return flags;
return flagsWithArgs;
}

export function extractPossibleArgs(flags = {}) {
export function extractPossibleArgs(flags: options.Flags = {}) {
let additionalArgs = [];
let updatedFlags = Object.assign({}, flags);
Object.keys(updatedFlags).forEach(flag => {
if (IS_BOOLEAN_FLAGS['--' + flag]) {
if (BOOLEAN_FLAGS['--' + flag]) {
additionalArgs.push(updatedFlags[flag]);
updatedFlags[flag] = true;
}
Expand Down

0 comments on commit 0846073

Please sign in to comment.