diff --git a/src/cli.js b/src/cli.js index 15f0b857..ea4984ba 100644 --- a/src/cli.js +++ b/src/cli.js @@ -12,7 +12,7 @@ import * as options from './utils/options'; const commandMap = { ADD: { add: true }, BIN: { bin: true }, - CACHE: { cache: true }, + BUILD: { build: true }, CACHE_CLEAN: { clean: true }, CACHE_DIR: { dir: true }, CACHE_LIST: { list: true, ls: true }, @@ -103,7 +103,7 @@ function runCommandFromCli(args: options.Args, flags: options.Flags) { return commands.add(commands.toAddOptions(commandArgs, flags)); } else if (commandMap.BIN[command]) { return commands.bin(commands.toBinOptions(commandArgs, flags)); - } else if (commandMap.CACHE[command]) { + } else if (commandMap.BUILD[command]) { return commands.build(commands.toBuildOptions(commandArgs, flags)); } else if (commandMap.CACHE_CLEAN[command]) { if (commandMap.CACHE_DIR[command]) { diff --git a/src/commands/__tests__/build.test.js b/src/commands/__tests__/build.test.js index a91bc68b..e7325487 100644 --- a/src/commands/__tests__/build.test.js +++ b/src/commands/__tests__/build.test.js @@ -1,4 +1,39 @@ // @flow import { build, toBuildOptions } from '../build'; +import { copyFixtureIntoTempDir } from 'jest-fixtures'; +import * as boltRun from '../run'; -test('bolt build'); +jest.mock('../run'); + +describe('bolt build', () => { + it('should call run with build script with no flags or arguments', async () => { + await build(toBuildOptions([], { '--': [] })); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: undefined, + script: 'build', + scriptArgs: [], + scriptFlags: [] + }); + }); + + it('should call run with build script with flags and arguments', async () => { + await build( + toBuildOptions(['Babel'], { + compress: true, + target: 'Node8', + cwd: 'dummyPattern/dummyPath', + '--': [] + }) + ); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: 'dummyPattern/dummyPath', + script: 'build', + scriptArgs: ['Babel'], + scriptFlags: [ + '--compress', + '--target=Node8', + '--cwd=dummyPattern/dummyPath' + ] + }); + }); +}); diff --git a/src/commands/__tests__/check.test.js b/src/commands/__tests__/check.test.js index 429168cf..f596d394 100644 --- a/src/commands/__tests__/check.test.js +++ b/src/commands/__tests__/check.test.js @@ -1,4 +1,39 @@ // @flow import { check, toCheckOptions } from '../check'; +import { copyFixtureIntoTempDir } from 'jest-fixtures'; +import * as boltRun from '../run'; -test('bolt check'); +jest.mock('../run'); + +describe('bolt check', () => { + it('should call run with build script with no flags or arguments', async () => { + await check(toCheckOptions([], { '--': [] })); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: undefined, + script: 'check', + scriptArgs: [], + scriptFlags: [] + }); + }); + + it('should call run with build script with flags and arguments', async () => { + await check( + toCheckOptions(['Babel'], { + compress: true, + target: 'Node8', + cwd: 'dummyPattern/dummyPath', + '--': [] + }) + ); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: 'dummyPattern/dummyPath', + script: 'check', + scriptArgs: ['Babel'], + scriptFlags: [ + '--compress', + '--target=Node8', + '--cwd=dummyPattern/dummyPath' + ] + }); + }); +}); diff --git a/src/commands/__tests__/doc.test.js b/src/commands/__tests__/doc.test.js index c9368d71..27c938fe 100644 --- a/src/commands/__tests__/doc.test.js +++ b/src/commands/__tests__/doc.test.js @@ -1,4 +1,39 @@ // @flow import { doc, toDocOptions } from '../doc'; +import { copyFixtureIntoTempDir } from 'jest-fixtures'; +import * as boltRun from '../run'; -test('bolt doc'); +jest.mock('../run'); + +describe('bolt doc', () => { + it('should call run with build script with no flags or arguments', async () => { + await doc(toDocOptions([], { '--': [] })); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: undefined, + script: 'doc', + scriptArgs: [], + scriptFlags: [] + }); + }); + + it('should call run with build script with flags and arguments', async () => { + await doc( + toDocOptions(['Babel'], { + compress: true, + target: 'Node8', + cwd: 'dummyPattern/dummyPath', + '--': [] + }) + ); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: 'dummyPattern/dummyPath', + script: 'doc', + scriptArgs: ['Babel'], + scriptFlags: [ + '--compress', + '--target=Node8', + '--cwd=dummyPattern/dummyPath' + ] + }); + }); +}); diff --git a/src/commands/__tests__/format.test.js b/src/commands/__tests__/format.test.js index 1f4bef63..89b662dd 100644 --- a/src/commands/__tests__/format.test.js +++ b/src/commands/__tests__/format.test.js @@ -1,4 +1,39 @@ // @flow import { format, toFormatOptions } from '../format'; +import { copyFixtureIntoTempDir } from 'jest-fixtures'; +import * as boltRun from '../run'; -test('bolt format'); +jest.mock('../run'); + +describe('bolt format', () => { + it('should call run with build script with no flags or arguments', async () => { + await format(toFormatOptions([], { '--': [] })); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: undefined, + script: 'format', + scriptArgs: [], + scriptFlags: [] + }); + }); + + it('should call run with build script with flags and arguments', async () => { + await format( + toFormatOptions(['Babel'], { + compress: true, + target: 'Node8', + cwd: 'dummyPattern/dummyPath', + '--': [] + }) + ); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: 'dummyPattern/dummyPath', + script: 'format', + scriptArgs: ['Babel'], + scriptFlags: [ + '--compress', + '--target=Node8', + '--cwd=dummyPattern/dummyPath' + ] + }); + }); +}); diff --git a/src/commands/__tests__/lint.test.js b/src/commands/__tests__/lint.test.js index 4aadae95..af832022 100644 --- a/src/commands/__tests__/lint.test.js +++ b/src/commands/__tests__/lint.test.js @@ -1,4 +1,39 @@ // @flow import { lint, toLintOptions } from '../lint'; +import { copyFixtureIntoTempDir } from 'jest-fixtures'; +import * as boltRun from '../run'; -test('bolt lint'); +jest.mock('../run'); + +describe('bolt lint', () => { + it('should call run with build script with no flags or arguments', async () => { + await lint(toLintOptions([], { '--': [] })); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: undefined, + script: 'lint', + scriptArgs: [], + scriptFlags: [] + }); + }); + + it('should call run with build script with flags and arguments', async () => { + await lint( + toLintOptions(['Babel'], { + compress: true, + target: 'Node8', + cwd: 'dummyPattern/dummyPath', + '--': [] + }) + ); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: 'dummyPattern/dummyPath', + script: 'lint', + scriptArgs: ['Babel'], + scriptFlags: [ + '--compress', + '--target=Node8', + '--cwd=dummyPattern/dummyPath' + ] + }); + }); +}); diff --git a/src/commands/__tests__/test.test.js b/src/commands/__tests__/test.test.js index 98ac850d..1e73b851 100644 --- a/src/commands/__tests__/test.test.js +++ b/src/commands/__tests__/test.test.js @@ -1,4 +1,39 @@ // @flow import { test as test_, toTestOptions } from '../test'; +import { copyFixtureIntoTempDir } from 'jest-fixtures'; +import * as boltRun from '../run'; -test('bolt test'); +jest.mock('../run'); + +describe('bolt lint', () => { + it('should call run with build script with no flags or arguments', async () => { + await test_(toTestOptions([], { '--': [] })); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: undefined, + script: 'test', + scriptArgs: [], + scriptFlags: [] + }); + }); + + it('should call run with build script with flags and arguments', async () => { + await test_( + toTestOptions(['Babel'], { + compress: true, + target: 'Node8', + cwd: 'dummyPattern/dummyPath', + '--': [] + }) + ); + expect(boltRun.run).toHaveBeenCalledWith({ + cwd: 'dummyPattern/dummyPath', + script: 'test', + scriptArgs: ['Babel'], + scriptFlags: [ + '--compress', + '--target=Node8', + '--cwd=dummyPattern/dummyPath' + ] + }); + }); +}); diff --git a/src/commands/build.js b/src/commands/build.js index 399b4d7f..c49fcf0a 100644 --- a/src/commands/build.js +++ b/src/commands/build.js @@ -5,7 +5,8 @@ import { run } from './run'; export type BuildOptions = {| cwd?: string, - args: options.Args + args: options.Args, + scriptFlags: Array |}; export function toBuildOptions( @@ -14,7 +15,8 @@ export function toBuildOptions( ): BuildOptions { return { cwd: options.string(flags.cwd, 'cwd'), - args: args + args: args, + scriptFlags: options.toScriptFlags(flags) }; } @@ -22,6 +24,7 @@ export async function build(opts: BuildOptions) { await run({ cwd: opts.cwd, script: 'build', - scriptArgs: opts.args + scriptArgs: opts.args, + scriptFlags: opts.scriptFlags }); } diff --git a/src/commands/check.js b/src/commands/check.js index c3f9edaa..f55030c7 100644 --- a/src/commands/check.js +++ b/src/commands/check.js @@ -5,7 +5,8 @@ import { run } from './run'; export type CheckOptions = {| cwd?: string, - args: options.Args + args: options.Args, + scriptFlags: Array |}; export function toCheckOptions( @@ -14,7 +15,8 @@ export function toCheckOptions( ): CheckOptions { return { cwd: options.string(flags.cwd, 'cwd'), - args: args + args: args, + scriptFlags: options.toScriptFlags(flags) }; } @@ -22,6 +24,7 @@ export async function check(opts: CheckOptions) { await run({ cwd: opts.cwd, script: 'check', - scriptArgs: opts.args + scriptArgs: opts.args, + scriptFlags: opts.scriptFlags }); } diff --git a/src/commands/doc.js b/src/commands/doc.js index 7dac4dfb..8cab5a61 100644 --- a/src/commands/doc.js +++ b/src/commands/doc.js @@ -5,7 +5,8 @@ import { run } from './run'; export type DocOptions = {| cwd?: string, - args: options.Args + args: options.Args, + scriptFlags: Array |}; export function toDocOptions( @@ -14,7 +15,8 @@ export function toDocOptions( ): DocOptions { return { cwd: options.string(flags.cwd, 'cwd'), - args: args + args: args, + scriptFlags: options.toScriptFlags(flags) }; } @@ -22,6 +24,7 @@ export async function doc(opts: DocOptions) { await run({ cwd: opts.cwd, script: 'doc', - scriptArgs: opts.args + scriptArgs: opts.args, + scriptFlags: opts.scriptFlags }); } diff --git a/src/commands/format.js b/src/commands/format.js index 17c2869a..a3487a92 100644 --- a/src/commands/format.js +++ b/src/commands/format.js @@ -5,7 +5,8 @@ import { run } from './run'; export type FormatOptions = {| cwd?: string, - args: options.Args + args: options.Args, + scriptFlags: Array |}; export function toFormatOptions( @@ -14,7 +15,8 @@ export function toFormatOptions( ): FormatOptions { return { cwd: options.string(flags.cwd, 'cwd'), - args: args + args: args, + scriptFlags: options.toScriptFlags(flags) }; } @@ -22,6 +24,7 @@ export async function format(opts: FormatOptions) { await run({ cwd: opts.cwd, script: 'format', - scriptArgs: opts.args + scriptArgs: opts.args, + scriptFlags: opts.scriptFlags }); } diff --git a/src/commands/lint.js b/src/commands/lint.js index 77f8a7bd..cfb4de29 100644 --- a/src/commands/lint.js +++ b/src/commands/lint.js @@ -5,7 +5,8 @@ import { run } from './run'; export type LintOptions = {| cwd?: string, - args: options.Args + args: options.Args, + scriptFlags: Array |}; export function toLintOptions( @@ -14,7 +15,8 @@ export function toLintOptions( ): LintOptions { return { cwd: options.string(flags.cwd, 'cwd'), - args: args + args: args, + scriptFlags: options.toScriptFlags(flags) }; } @@ -22,6 +24,7 @@ export async function lint(opts: LintOptions) { await run({ cwd: opts.cwd, script: 'lint', - scriptArgs: opts.args + scriptArgs: opts.args, + scriptFlags: opts.scriptFlags }); } diff --git a/src/commands/run.js b/src/commands/run.js index eab1f1f3..05466f71 100644 --- a/src/commands/run.js +++ b/src/commands/run.js @@ -8,7 +8,8 @@ import { BoltError } from '../utils/errors'; export type RunOptions = {| cwd?: string, script: string, - scriptArgs: options.Args + scriptArgs: options.Args, + scriptFlags: Array |}; export function toRunOptions( @@ -19,7 +20,8 @@ export function toRunOptions( return { cwd: options.string(flags.cwd, 'cwd'), script, - scriptArgs + scriptArgs, + scriptFlags: options.toScriptFlags(flags) }; } @@ -30,7 +32,7 @@ export async function run(opts: RunOptions) { if (script) { logger.cmd(script, opts.scriptArgs); - await yarn.run(pkg, opts.script, opts.scriptArgs); + await yarn.run(pkg, opts.script, opts.scriptArgs, opts.scriptFlags); } else { throw new BoltError( `Package at "${pkg.dir}" does not have a script named "${opts.script}"` diff --git a/src/commands/test.js b/src/commands/test.js index 871f158a..9a9e3b63 100644 --- a/src/commands/test.js +++ b/src/commands/test.js @@ -5,7 +5,8 @@ import { run } from './run'; export type TestOptions = {| cwd?: string, - args: options.Args + args: options.Args, + scriptFlags: Array |}; export function toTestOptions( @@ -14,7 +15,8 @@ export function toTestOptions( ): TestOptions { return { cwd: options.string(flags.cwd, 'cwd'), - args: args + args: args, + scriptFlags: options.toScriptFlags(flags) }; } @@ -22,6 +24,7 @@ export async function test(opts: TestOptions) { await run({ cwd: opts.cwd, script: 'test', - scriptArgs: opts.args + scriptArgs: opts.args, + scriptFlags: opts.scriptFlags }); } diff --git a/src/utils/__tests__/options.test.js b/src/utils/__tests__/options.test.js index d9931127..1898d636 100644 --- a/src/utils/__tests__/options.test.js +++ b/src/utils/__tests__/options.test.js @@ -4,4 +4,21 @@ import * as options from '../options'; describe('options', () => { test('string'); test('boolean'); + + describe('toScriptFlags', () => { + it('returns empty array if there were no custom flags passes', async () => { + const scriptFlags = await options.toScriptFlags({ + '--': [] + }); + expect(scriptFlags).toEqual([]); + }); + it('returns flags with value with the custom flags that are passed', async () => { + const scriptFlags = await options.toScriptFlags({ + '--': [], + coverage: true, + target: 'Node9' + }); + expect(scriptFlags).toEqual(['--coverage', '--target=Node9']); + }); + }); }); diff --git a/src/utils/options.js b/src/utils/options.js index 832c4161..6e30702c 100644 --- a/src/utils/options.js +++ b/src/utils/options.js @@ -47,3 +47,18 @@ export function toDependency(dependencyString: string): Dependency { } return version ? { name, version } : { name }; } + +export function toScriptFlags(flags: Flags) { + let scriptFlags = []; + + Object.keys(flags).map(flag => { + if (flag === '--') return; + if (typeof flags[flag] === 'string') { + scriptFlags.push(`--${flag}=${flags[flag]}`); + } else { + scriptFlags.push(`--${flag}`); + } + }); + + return scriptFlags; +} diff --git a/src/utils/yarn.js b/src/utils/yarn.js index bed551a7..0c600c37 100644 --- a/src/utils/yarn.js +++ b/src/utils/yarn.js @@ -53,7 +53,8 @@ export async function add( export async function run( pkg: Package, script: string, - args: Array = [] + args: Array = [], + flags: Array = [] ) { const localYarn = path.join(await getLocalBinPath(), 'yarn'); let spawnArgs = ['run', '-s', script]; @@ -62,6 +63,10 @@ export async function run( spawnArgs = spawnArgs.concat('--', args); } + if (flags.length) { + spawnArgs = spawnArgs.concat(flags); + } + await processes.spawn(localYarn, spawnArgs, { cwd: pkg.dir, pkg: pkg,