// @flow
import Project from '../../Project';
import * as options from '../../utils/options';
import * as yarn from '../../utils/yarn';
import { BoltError } from '../../utils/errors';

export type ProjectRunOptions = {
  cwd?: string,
  script: string,
  scriptArgs: options.Args
};

export function toProjectRunOptions(
  args: options.Args,
  flags: options.Flags
): ProjectRunOptions {
  let [script, ...scriptArgs] = args;
  return {
    cwd: options.string(flags.cwd, 'cwd'),
    script,
    scriptArgs
  };
}

export async function projectRun(opts: ProjectRunOptions) {
  let cwd = opts.cwd || process.cwd();
  let project = await Project.init(cwd);
  let validScript = await yarn.run(project.pkg, opts.script, opts.scriptArgs);

  if (!validScript) {
    throw new BoltError(
      `Package at "${project.pkg
        .dir}" does not have a script named "${opts.script}"`
    );
  }
}

@@ -129,16 +129,62 @@
 /yarn';%0A
+import * as logger from '../../utils/logger';%0A
 import %7B
@@ -701,22 +701,17 @@
 ;%0A  let 
-validS
+s
 cript = 
@@ -725,11 +725,17 @@
 arn.
-run
+getScript
 (pro
@@ -759,48 +759,140 @@
 ript
-, opts.scriptArgs);%0A%0A  if (!validScript)
+);%0A%0A  if (script) %7B%0A    logger.cmd(script, opts.scriptArgs);%0A    await yarn.run(project.pkg, opts.script, opts.scriptArgs);%0A  %7D else
  %7B%0A 

Fix project run throwing invalid error (#210)