2016-02-26 169 views
13

我正在使用gulp任務來測試一個項目。這使用以下工具運行任務:爲npm測試運行多個命令

  • 噶(異步)
  • 量角器(衍生進程)
  • ESlint(使用gulp-eslint
  • HTMLHint(使用gulp-htmlhint
  • Stylelint(使用gulp-postcss

如果其中任何任務失敗,任務將失敗。

所有這些工具都有非常好的cli接口。所以我決定使用npm測試腳本來運行這些工具。

簡單地說,讓我們說所有的工具只需在沒有任何標誌的情況下調用它們即可運行。那麼這是可以做到用:

{ 
    ... 
    "scripts": { 
    "test": "karma && protractor && eslint && htmlhint && stylelint" 
    }, 
    ... 
} 

但是,這意味着如果karma失敗,沒有其他工具將運行。

是否可以創建所有這些工具都可以運行的設置,但如果任何命令失敗,npm test將會失敗?

"scripts": { 
    "test": "karma ; protractor ; eslint ; htmlhint ; stylelint" 
}, 

將運行所有的命令,如果你有一個UNIX/OSX殼:

回答

4

npm-run-all也可以處理這個問題很好

您可以運行多個NPM併發命令,如下錯誤繼續:

npm-run-all --parallel --continue-on-error karma protractor eslint htmlhint stylelint

選項書面文檔中:

-p, --parallel <tasks> - Run a group of tasks in parallel. 
          e.g. 'npm-run-all -p foo bar' is similar to 
           'npm run foo & npm run bar'. 
-c, --continue-on-error - Set the flag to continue executing other tasks 
          even if a task threw an error. 'run-p' itself 
          will exit with non-zero code if one or more tasks 
          threw error(s). 
+1

我現在更喜歡這個。 –

11

package.json腳本標籤是由你的shell,你希望shell都跑,這樣你就可以運行該命令。

爲了能夠像指定一樣保留exit_code,您需要有一個單獨的腳本來運行這些命令。也許是這樣的:

#!/bin/bash 

EXIT_STATUS=0 

function check_command { 
    "[email protected]" 
    local STATUS=$? 
    if [ $STATUS -ne 0 ]; then 
     echo "error with $1 ($STATUS)" >&2 
     EXIT_STATUS=$STATUS 
    fi 
} 

check_command karma 
check_command protractor 
check_command eslint 
check_command htmlhint 
check_command stylelint 
exit $EXIT_STATUS 
+0

這將運行所有的命令,但它會與最後一個命令的代碼存在退出,忽略任何先前的退出代碼。所以在這種情況下,任何失敗的測試都將被忽略。 –

+0

我已經添加了一個例子。 – bolav

+0

請注意,第一個示例中的'test'腳本不支持Windows,';'語法在PowerShell中無效。 –

7

concurrently是可以處理這個問題一個很好的庫。它可以從npm安裝。

npm install --save-dev concurrently 

當每一個部分進行排序作爲一個單獨的腳本中,package.json腳本部分看起來有點像這樣:

{ 
    ... 
    "scripts": { 
    "test": "concurrently 'npm run karma' 'npm run protractor' 'npm run eslint' 'npm run htmlhint' 'npm run stylelint'", 
    "karma": "karma start --single-run", 
    "protractor": "protractor", 
    "eslint": "eslint .", 
    "htmlhint": "htmlhint 'app/**/*.html'", 
    "stylelint": "stylelint 'app/**/*.css'", 
    }, 
    ... 
} 
+2

''''測試':'同時'npm運行karma''npm運行量角器''npm運行eslint''npm運行htmlhint''npm運行stylelint'「'''應該'''''測試」:「同時\「npm run karma \」\ npm運行量角器\「\」npm運行eslint \「\」npm運行htmlhint \「\」npm運行stylelint \「」'''在windows上。 – lfree