From 1bad1f96a66122de0daf0457eb3cbf873d087517 Mon Sep 17 00:00:00 2001 From: Bhargav Ponnapalli Date: Thu, 15 Jun 2017 20:25:05 +0530 Subject: [PATCH] fix: scriptName early error (#147) * Fail gracefully on non-existent scripts * Add test case to handle missing scripts --- src/index.js | 2 +- src/index.test.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 640783e..f32a105 100644 --- a/src/index.js +++ b/src/index.js @@ -42,7 +42,7 @@ function runPackageScripts({scriptConfig, scripts, options = {}}) { function runPackageScript({scriptConfig, options, input}) { const [scriptPrefix, ...args] = input.split(' ') const scripts = getScriptsFromConfig(scriptConfig, scriptPrefix) - const {scriptName, script} = getScriptToRun(scripts, scriptPrefix) + const {scriptName, script} = getScriptToRun(scripts, scriptPrefix) || {} if (!isString(script)) { return Promise.reject({ message: chalk.red( diff --git a/src/index.test.js b/src/index.test.js index b19307b..fd6ef46 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -151,6 +151,23 @@ test('runs the default script if no scripts provided', () => { }) }) +test('returns a log object when a script does not exist', () => { + const {runPackageScript} = setup() + const scriptConfig = {lint: {script: 42}} + return runPackageScript({scriptConfig, scripts: ['dev']}).catch(error => { + expect(error).toEqual({ + message: chalk.red( + oneLine` + Scripts must resolve to strings. + There is no script that can be + resolved from "dev" + `, + ), + ref: 'missing-script', + }) + }) +}) + test('an error from the child process logs an error', () => { const ERROR = {message: 'there was an error', code: 2} const badCommand = 'bad'