Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolving cli flags to commands #104

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ 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 './utils/flagHelper';

const commandMap = {
ADD: { add: true },
AUTOCLEAN: { autoclean: true },
BIN: { bin: true },
BUILD: { build: true },
CACHE: { cache: true },
CACHE_CLEAN: { clean: true },
CACHE_DIR: { dir: true },
Expand Down Expand Up @@ -100,6 +102,15 @@ const commandMap = {
};

function runCommandFromCli(args: options.Args, flags: options.Flags) {
const {
additionalArgs,
scriptFlags,
updatedFlags
} = flagHelpers.getArgsBooleanFlagsScriptFlags(flags);

args = args.concat(additionalArgs);
flags = updatedFlags;

let [command, ...commandArgs] = args;
let [subCommand, ...subCommandArgs] = commandArgs;

Expand All @@ -109,6 +120,10 @@ function runCommandFromCli(args: options.Args, flags: options.Flags) {
return commands.autoclean(commands.toAutocleanOptions(commandArgs, flags));
} else if (commandMap.BIN[command]) {
return commands.bin(commands.toBinOptions(commandArgs, flags));
} else if (commandMap.BUILD[command]) {
return commands.build(
commands.toBuildOptions(commandArgs, flags, scriptFlags)
);
} else if (commandMap.CACHE[command]) {
if (commandMap.CACHE_CLEAN[subCommand]) {
return commands.cacheClean(
Expand All @@ -127,7 +142,9 @@ function runCommandFromCli(args: options.Args, flags: options.Flags) {
);
}
} else if (commandMap.CHECK[command]) {
return commands.check(commands.toCheckOptions(commandArgs, flags));
return commands.check(
commands.toCheckOptions(commandArgs, flags, scriptFlags)
);
} else if (commandMap.CONFIG[command]) {
if (commandMap.CONFIG_DELETE[subCommand]) {
return commands.configDelete(
Expand Down Expand Up @@ -156,11 +173,13 @@ function runCommandFromCli(args: options.Args, flags: options.Flags) {
} else if (commandMap.CREATE[command]) {
return commands.create(commands.toCreateOptions(commandArgs, flags));
} else if (commandMap.DOC[command]) {
return commands.doc(commands.toDocOptions(commandArgs, flags));
return commands.doc(commands.toDocOptions(commandArgs, flags, scriptFlags));
} else if (commandMap.EXEC[command]) {
return commands.exec(commands.toExecOptions(commandArgs, flags));
} else if (commandMap.FORMAT[command]) {
return commands.format(commands.toFormatOptions(commandArgs, flags));
return commands.format(
commands.toFormatOptions(commandArgs, flags, scriptFlags)
);
} else if (commandMap.GENERATE[command]) {
return commands.generate(commands.toGenerateOptions(commandArgs, flags));
} else if (commandMap.GLOBAL[command]) {
Expand Down Expand Up @@ -197,7 +216,9 @@ function runCommandFromCli(args: options.Args, flags: options.Flags) {
} else if (commandMap.INIT[command]) {
return commands.init(commands.toInitOptions(commandArgs, flags));
} else if (commandMap.INSTALL[command] || typeof command === 'undefined') {
return commands.install(commands.toInstallOptions(commandArgs, flags));
return commands.install(
commands.toInstallOptions(commandArgs, flags, scriptFlags)
);
} else if (commandMap.LICENSES[command]) {
if (commandMap.LICENSES_GENERATE_DISCLAIMER[subCommand]) {
return commands.licensesGenerateDisclaimer(
Expand All @@ -214,7 +235,9 @@ function runCommandFromCli(args: options.Args, flags: options.Flags) {
} else if (commandMap.LINK[command]) {
return commands.link(commands.toLinkOptions(commandArgs, flags));
} else if (commandMap.LINT[command]) {
return commands.lint(commands.toLintOptions(commandArgs, flags));
return commands.lint(
commands.toLintOptions(commandArgs, flags, scriptFlags)
);
} else if (commandMap.LIST[command]) {
return commands.list(commands.toListOptions(commandArgs, flags));
} else if (commandMap.LOGIN[command]) {
Expand Down Expand Up @@ -283,7 +306,7 @@ function runCommandFromCli(args: options.Args, flags: options.Flags) {
} else if (commandMap.REMOVE[command]) {
return commands.remove(commands.toRemoveOptions(commandArgs, flags));
} else if (commandMap.RUN[command]) {
return commands.run(commands.toRunOptions(commandArgs, flags));
return commands.run(commands.toRunOptions(commandArgs, flags, scriptFlags));
} else if (commandMap.TAG[command]) {
if (commandMap.TAG_ADD[subCommand]) {
return commands.tagAdd(commands.toTagAddOptions(subCommandArgs, flags));
Expand Down Expand Up @@ -321,7 +344,9 @@ function runCommandFromCli(args: options.Args, flags: options.Flags) {
);
}
} else if (commandMap.TEST[command]) {
return commands.test(commands.toTestOptions(commandArgs, flags));
return commands.test(
commands.toTestOptions(commandArgs, flags, scriptFlags)
);
} else if (commandMap.UNLINK[command]) {
return commands.unlink(commands.toUnlinkOptions(commandArgs, flags));
} else if (commandMap.UPGRADE[command]) {
Expand Down Expand Up @@ -399,7 +424,7 @@ function runCommandFromCli(args: options.Args, flags: options.Flags) {
);
}
} else {
return commands.run(commands.toRunOptions(args, flags));
return commands.run(commands.toRunOptions(args, flags, scriptFlags));
}

throw new BoltError(`You must specify a valid command`);
Expand Down
22 changes: 11 additions & 11 deletions src/commands/__tests__/install.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('install', () => {

test('simple-package, should run yarn install at the root', async () => {
let cwd = f.find('simple-package');
await install(toInstallOptions([], { cwd }));
await install(toInstallOptions([], { cwd }, []));
expect(unsafeProcesses.spawn).toHaveBeenCalledTimes(1);
expect(unsafeProcesses.spawn).toHaveBeenCalledWith('yarn', ['install'], {
cwd,
Expand All @@ -61,7 +61,7 @@ describe('install', () => {
test('should still run yarn install at the root when called from ws', async () => {
let rootDir = f.find('simple-project');
let cwd = path.join(path.join(rootDir, 'packages', 'foo'));
await install(toInstallOptions([], { cwd }));
await install(toInstallOptions([], { cwd }, []));
expect(unsafeProcesses.spawn).toHaveBeenCalledTimes(1);
expect(unsafeProcesses.spawn).toHaveBeenCalledWith('yarn', ['install'], {
cwd: rootDir,
Expand All @@ -73,7 +73,7 @@ describe('install', () => {
test('should pass the --pure-lockfile flag correctly', async () => {
let rootDir = f.find('simple-project');
let cwd = path.join(path.join(rootDir, 'packages', 'foo'));
await install(toInstallOptions([], { cwd, pureLockfile: true }));
await install(toInstallOptions([], { cwd }, ['--pure-lockfile']));
expect(unsafeProcesses.spawn).toHaveBeenCalledTimes(1);
expect(unsafeProcesses.spawn).toHaveBeenCalledWith(
'yarn',
Expand All @@ -91,7 +91,7 @@ describe('install', () => {
let project = await Project.init(cwd);
let workspaces = await project.getWorkspaces();

await install(toInstallOptions([], { cwd }));
await install(toInstallOptions([], { cwd }, []));

for (let workspace of workspaces) {
assertNodeModulesExists(workspace);
Expand All @@ -103,7 +103,7 @@ describe('install', () => {
const spawnSpy = jest.spyOn(processes, 'spawn');
let cwd = f.find('nested-workspaces-with-scoped-package-names');

await install(toInstallOptions([], { cwd }));
await install(toInstallOptions([], { cwd }, []));

expect(spawnSpy.mock.calls[0][2].env.npm_config_user_agent).toEqual(
'bolt/9.9.9 yarn/7.7.7 npm/? node/v8.9.4 darwin x64'
Expand All @@ -114,7 +114,7 @@ describe('install', () => {
const spawnSpy = jest.spyOn(processes, 'spawn');
let cwd = f.find('nested-workspaces-with-scoped-package-names');

await install(toInstallOptions([], { cwd }));
await install(toInstallOptions([], { cwd }, []));

expect(spawnSpy.mock.calls[0][2].env.parent_env).toEqual(1);
});
Expand All @@ -124,7 +124,7 @@ describe('install', () => {
let project = await Project.init(cwd);
let workspaces = await project.getWorkspaces();

await install(toInstallOptions([], { cwd }));
await install(toInstallOptions([], { cwd }, []));

for (let workspace of workspaces) {
let runFn = unsafeYarn.runIfExists;
Expand All @@ -140,7 +140,7 @@ describe('install', () => {
const project = await Project.init(cwd);
const workspaces = await project.getWorkspaces();

await install(toInstallOptions([], { cwd }));
await install(toInstallOptions([], { cwd }, []));

for (let workspace of workspaces) {
assertNodeModulesExists(workspace);
Expand All @@ -153,8 +153,8 @@ describe('install', () => {
let cwd = f.copy('invalid-project-root-dependency-on-ws');
let project = await Project.init(cwd);

await expect(install(toInstallOptions([], { cwd }))).rejects.toBeInstanceOf(
Error
);
await expect(
install(toInstallOptions([], { cwd }, []))
).rejects.toBeInstanceOf(Error);
});
});
12 changes: 8 additions & 4 deletions src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ import { run } from './run';

export type BuildOptions = {|
cwd?: string,
args: options.Args
args: options.Args,
scriptFlags: Array<string>
|};

export function toBuildOptions(
args: options.Args,
flags: options.Flags
flags: options.Flags,
scriptFlags: Array<string>
): BuildOptions {
return {
cwd: options.string(flags.cwd, 'cwd'),
args: args
args: args,
scriptFlags
};
}

export async function build(opts: BuildOptions) {
await run({
cwd: opts.cwd,
script: 'build',
scriptArgs: opts.args
scriptArgs: opts.args,
scriptFlags: opts.scriptFlags
});
}
12 changes: 8 additions & 4 deletions src/commands/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ import { run } from './run';

export type CheckOptions = {|
cwd?: string,
args: options.Args
args: options.Args,
scriptFlags: Array<string>
|};

export function toCheckOptions(
args: options.Args,
flags: options.Flags
flags: options.Flags,
scriptFlags: Array<string>
): CheckOptions {
return {
cwd: options.string(flags.cwd, 'cwd'),
args: args
args: args,
scriptFlags
};
}

export async function check(opts: CheckOptions) {
await run({
cwd: opts.cwd,
script: 'check',
scriptArgs: opts.args
scriptArgs: opts.args,
scriptFlags: opts.scriptFlags
});
}
12 changes: 8 additions & 4 deletions src/commands/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ import { run } from './run';

export type DocOptions = {|
cwd?: string,
args: options.Args
args: options.Args,
scriptFlags: Array<string>
|};

export function toDocOptions(
args: options.Args,
flags: options.Flags
flags: options.Flags,
scriptFlags: Array<string>
): DocOptions {
return {
cwd: options.string(flags.cwd, 'cwd'),
args: args
args: args,
scriptFlags
};
}

export async function doc(opts: DocOptions) {
await run({
cwd: opts.cwd,
script: 'doc',
scriptArgs: opts.args
scriptArgs: opts.args,
scriptFlags: opts.scriptFlags
});
}
12 changes: 8 additions & 4 deletions src/commands/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ import { run } from './run';

export type FormatOptions = {|
cwd?: string,
args: options.Args
args: options.Args,
scriptFlags: Array<string>
|};

export function toFormatOptions(
args: options.Args,
flags: options.Flags
flags: options.Flags,
scriptFlags: Array<string>
): FormatOptions {
return {
cwd: options.string(flags.cwd, 'cwd'),
args: args
args: args,
scriptFlags
};
}

export async function format(opts: FormatOptions) {
await run({
cwd: opts.cwd,
script: 'format',
scriptArgs: opts.args
scriptArgs: opts.args,
scriptFlags: opts.scriptFlags
});
}
12 changes: 5 additions & 7 deletions src/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,24 @@ import { BOLT_VERSION } from '../constants';

export type InstallOptions = {|
cwd?: string,
pureLockfile: boolean
scriptFlags: Array<string>
|};

export function toInstallOptions(
args: options.Args,
flags: options.Flags
flags: options.Flags,
scriptFlags: Array<string>
): InstallOptions {
return {
cwd: options.string(flags.cwd, 'cwd'),
pureLockfile: options.boolean(flags.pureLockfile, 'pure-lockfile') || false
scriptFlags
};
}

export async function install(opts: InstallOptions) {
let cwd = opts.cwd || process.cwd();
let project = await Project.init(cwd);
let workspaces = await project.getWorkspaces();
let installFlags = [];

if (opts.pureLockfile) installFlags.push('--pure-lockfile');

logger.info(messages.validatingProject(), { emoji: '🔎', prefix: false });

Expand All @@ -51,7 +49,7 @@ export async function install(opts: InstallOptions) {
const yarnUserAgent = await yarn.userAgent();
const boltUserAgent = `bolt/${BOLT_VERSION} ${yarnUserAgent}`;

await processes.spawn('yarn', ['install', ...installFlags], {
await processes.spawn('yarn', ['install', ...opts.scriptFlags], {
cwd: project.pkg.dir,
tty: true,
env: { ...process.env, npm_config_user_agent: boltUserAgent }
Expand Down
Loading