2015-12-09 4 views
5

私は、テストプロセスの主なツールとしてNPMスクリプトを使用し始めました。私のNPMスクリプトをより読みやすくするためのより良い方法がありますか?

私がもっと多くのNPMスクリプトを書いたので、注文と構造は一目で読むことがますます困難になっています。これは、わずか約8テストかそこらのためにある

{ 
    "scripts": { 
    "clean": "rm -rf report/*", 
    "report": "rm -rf report/; mkdir report", 
    "tests:mocha": "mocha test/spec/", 
    "all-tests:mocha": "npm run tests:mocha -- --recursive", 
    "all-tests:json": "npm run all-tests:mocha -- --reporter json", 
    "all-tests:jsonReport": "npm run all-tests:json > report/all-tests.json", 
    "all-admin:mocha": "mocha test/spec/admin.production.io/", 
    "all-admin-all:mocha": "npm run all-admin:mocha -- --recursive", 
    "all-admin:json": "npm run all-admin:mocha -- --reporter json", 
    "all-admin:jsonReport": "npm run all-admin:json > report/all-tests.json", 
    "google:mocha": "mocha test/spec/googleTest.js", 
    "google:spec": "npm run google:mocha -- --reporter spec --slow 0", 
    "google:json": "npm run google:mocha -- --reporter json", 
    "google:jsonReport": "npm run google:json > report/google-test.json", 
    "usersAll:mocha": "mocha test/spec/admin.production.io/dashboard/users/*.js", 
    "usersAll:spec": "npm run usersAll:mocha -- --reporter spec --slow 0", 
    "usersAll:json": "npm run usersAll:mocha -- --reporter json", 
    "usersAll:jsonReport": "npm run usersAll:jsonReport > report/admin-users-dashboard-all.json", 
    "orgsAll:mocha": "mocha test/spec/admin.production.io/dashboard/organizations/*.js", 
    "orgsAll:spec": "npm run orgsAll:mocha -- --reporter spec --slow 0", 
    "orgsAll:json": "npm run orgsAll:mocha -- --reporter json", 
    "orgsAll:jsonReport": "npm run orgsAll:json > report/admin-orgs-dashboard-all.json", 
    "users-orgs:mocha": "npm run users:spec; npm run orgs:spec", 
    "users-orgs-report": "npm run users:jsonReport; npm run orgs:jsonReport", 
    "pos-users:mocha": "mocha test/spec/admin.production.io/dashboard/users/positiveUserTest.js", 
    "pos-users:spec": "npm run pos-users:mocha -- --reporter spec --slow 0", 
    "pos-users:json": "npm run pos-users:mocha -- --reporter json", 
    "pos-users:jsonReport": "npm run pos-users:json > report/admin-users-dashboard-positive.json", 
    "pos-orgs:mocha": "mocha test/spec/admin.production.io/dashboard/organizations/positiveOrgsTests.js", 
    "pos-orgs:spec": "npm run pos-orgs:mocha -- --reporter spec --slow 0", 
    "pos-orgs:json": "npm run pos-orgs:mocha -- --reporter json", 
    "pos-orgs:jsonReport": "npm run pos-orgs:json > report/admin-users-dashboard-positive.json", 
    "alice-la:mocha": "mocha test/spec/admin.local.us/dashboard/alice/*.js", 
    "alice-la:jsonReport": "npm run alice-la:mocha -- --reporter json -- > report/local-admin-dashboard-alice-all-tests.json", 
    "a2361p-l:mocha": "mocha test/spec/admin.local.us/dashboard/alice/2361-p.js", 
    "a2361p-l:spec": "npm run a2361p-l:mocha -- --reporter spec --slow 0", 
    "a2361p-l:json": "npm run a2361p-l:mocha -- --reporter json", 
    "a2361p-l:jsonReport": "npm run a2361p-l:json > report/a2361p-l.json", 
    "a2361pf-l:mocha": "mocha test/spec/admin.local.us/dashboard/alice/2361-pf.js", 
    "a2361pf-l:spec": "npm run a2361pf-l:mocha -- --reporter spec --slow 0", 
    "a2361pf-l:json": "npm run a2361pf-l:mocha -- --reporter json", 
    "a2361pf-l:jsonReport": "npm run a2361pf-l:json > report/a2361pf-l.json", 
    "alice-pa:mocha": "mocha test/spec/admin.production.io/dashboard/alice/*.js", 
    "alice-pa:jsonReport": "npm run alice-pa:mocha -- --reporter json -- > report/production-admin-dashboard-alice-all-tests.json", 
    "a2361p-p:mocha": "mocha test/spec/admin.production.io/dashboard/alice/2361-p.js", 
    "a2361p-p:spec": "npm run a2361p-p:mocha -- --reporter spec --slow 0", 
    "a2361p-p:json": "npm run a2361p-p:mocha -- --reporter json", 
    "a2361p-p:jsonReport": "npm run a2361p-p:json > report/a2361p-p.json", 
    "a2361pf-p:mocha": "mocha test/spec/admin.production.io/dashboard/alice/2361-pf.js", 
    "a2361pf-p:spec": "npm run a2361pf-p:mocha -- --reporter spec --slow 0", 
    "a2361pf-p:json": "npm run a2361pf-p:mocha -- --reporter json", 
    "a2361pf-p:jsonReport": "npm run a2361pf-p:json > report/a2361pf-p.json", 
    "a2361:all": "npm run clean; npm run a2361p-l:mocha; npm run a2361p-l:spec; npm run a2361p-l:json; npm run a2361p-l:jsonReport; npm run a2361pf-l:mocha; npm run a2361pf-l:spec; npm run a2361pf-l:json; npm run a2361pf-l:jsonReport; npm run a2361p-p:mocha; npm run a2361p-p:spec; npm run a2361p-p:json; npm run a2361p-p:jsonReport; npm run a2361pf-p:mocha; npm run a2361pf-p:spec; npm run a2361pf-p:json; npm run a2361pf-p:jsonReport", 
    "test": "mocha" 
} 

:ここ

は私の現在のスクリプトは次のようになります。私は、junit形式のレポートを出力するためのスクリプトを書くことを計画しています。すでに目の疲れのように見え始めています。

npmスクリプトをより読みやすくするにはどうすればよいですか?

+0

たぶん、あなたは、それぞれのテストの小さな部分を定義する、の.jsファイルのカップルを作成することができます。ここでは

は、それはあなたのタスクを単純化する方法です。そして、1つのメインファイルが他のファイルをインポートし、JSON.Strignify(require( './ main.js')) – Ludo

+0

@ルドでpackage.jsonを「コンパイル」します。私はそれがプロジェクトの範囲と必要なnpmスクリプトの数に依存すると思います。私はスクリプトの一部をそのままリファクタリングし、不必要な行を減らしています。 JSを使ってpackage.jsonを正しく作成するのに熟練しているとは思えません。 – azemPC

+0

おそらくあなたはKonphyg(https://github.com/pgte/konphyg)を使うことができます。これは設定オブジェクトをかなり簡単に作成します。それよりも、fs.writeFileSync( "package.json"、JSON.stringify(conphyg.all()))を使用してpackage.jsonファイルを作成します。これは(https://github.com/exhuma/braindump/tree/master/jsonformat)それをあやつくかもしれません。 – Ludo

答えて

2

編集:別の解決策が掲載されるまでこれを最良の回答として選択します。

安いトリックとして、私は空のテストスクリプトをヘッダーとして機能させ、その下に適切なスクリプトをインデントしました。十分に十分な方法ではありませんが、全くインデントしない方が良いです。

警告:空のスクリプトを実行するとエラーが発生します。

{ 
    "utils": "", 
     "clean": "rm -rf report/*; rm -rf *.xml", 
     "report": "rm -rf report/; mkdir report", 
    "all-tests": "", 
     "tests:mocha": "mocha test/spec/", 
     "at:mocha": "npm run tests:mocha -- --recursive", 
     "at:junit": "npm run at:mocha -- --reporter mocha-junit-reporter", 
     "at:jReport": "MOCHA_FILE=./report/all-tests.xml npm run at:junit", 
    "all-prod-admin-tests": "",   
     "prod-tests:mocha": "mocha test/spec/admin.production.io/", 
     "apa:mocha": "npm run -prod-tests:mocha -- --recursive", 
     "apa:junit": "npm run apa:mocha -- --reporter mocha-junit-reporter", 
     "apa:jReport": "MOCHA_FILE=./report/all-prod-admin-tests.xml npm run apa:junit", 
    "all-local-admin-tests": "",   
     "local-tests:mocha": "mocha test/spec/admin.pclocal.us/", 
     "ala:mocha": "npm run -prod-tests:mocha -- --recursive", 
     "ala:junit": "npm run ala:mocha -- --reporter mocha-junit-reporter", 
     "ala:jReport": "MOCHA_FILE=./report/all-local-admin-tests.xml npm run ala:junit", 
    "google-example": "", 
     "google:mocha": "mocha test/spec/googleTest.js", 
     "google:spec": "npm run google:mocha -- --reporter spec --slow 0", 
     "google:junit": "npm run google:mocha -- --reporter mocha-junit-reporter", 
     "google:jReport": "MOCHA_FILE=./report/google.xml npm run google:junit", 
    "alice-local-admin-dashboard-tests": "", 
     "alda:mocha": "mocha test/spec/admin.pclocal.us/dashboard/alice/*.js", 
     "alda:junit": "npm run alda:mocha -- --reporter mocha-junit-reporter", 
     "alda:jReport": "MOCHA_FILE=./report/alice-local-admin-dashboard-tests-all.xml npm run alda:junit", 
     "2361-automated-test": "", 
      "local-admin-pass": "", 
       "a2361p-l:mocha": "mocha test/spec/admin.pclocal.us/dashboard/alice/2361-p.js", 
       "a2361p-l:spec": "npm run a2361p-l:mocha -- --reporter spec --slow 0", 
       "a2361p-l:junit": "npm run a2361p-l:mocha -- --reporter mocha-junit-reporter", 
       "a2361p-l:jReport": "MOCHA_FILE=./report/2361-local-pass.xml npm run a2361p-l:junit", 
      "local-admin-pass-fail": "", 
       "a2361pf-l:mocha": "mocha test/spec/admin.pclocal.us/dashboard/alice/2361-pf.js", 
       "a2361pf-l:spec": "npm run a2361pf-l:mocha -- --reporter spec --slow 0", 
       "a2361pf-l:junit": "npm run a2361pf-l:mocha -- --reporter mocha-junit-reporter", 
       "a2361pf-l:jReport": "MOCHA_FILE=./report/2361-local-pass-fail.xml npm run a2361pf-l:junit", 
    "alice-production-admin-dashboard-tests": "", 
     "apda:mocha": "mocha test/spec/admin.production.io/dashboard/alice/*.js", 
     "apda:junit": "npm run apda:mocha -- --reporter mocha-junit-reporter", 
     "apda:jReport": "MOCHA_FILE=./report/alice-production-admin-dashboard-tests-all.xml npm run apda:junit", 
     "2361-automated-test": "", 
      "prod-admin-pass": "", 
       "a2361p-p:mocha": "mocha test/spec/admin.production.io/dashboard/alice/2361-p.js", 
       "a2361p-p:spec": "npm run a2361p-p:mocha -- --reporter spec --slow 0", 
       "a2361p-p:junit": "npm run a2361p-p:mocha -- --reporter mocha-junit-reporter", 
       "a2361p-p:jReport": "MOCHA_FILE=./report/2361-prod-pass.xml npm run a2361p-p:junit", 
      "prod-admin-pass-fail": "", 
       "a2361pf-p:mocha": "mocha test/spec/admin.production.io/dashboard/alice/2361-pf.js", 
       "a2361pf-p:spec": "npm run a2361pf-p:mocha -- --reporter spec --slow 0", 
       "a2361pf-p:junit": "npm run a2361pf-p:mocha -- --reporter mocha-junit-reporter", 
       "a2361pf-p:jReport": "MOCHA_FILE=./report/2361-prod-pass-fail.xml npm run a2361pf-p:junit", 
    "test": "mocha" 
} 
1

モカ・テストのためのあなたのファイルパスをクリーンアップするために、あなたはoptsファイルにそれらを移動することを検討することがあります。あなたが好きだろう。もちろん、

test/spec/admin.pclocal.us/dashboard/alice/*.js 
--recursive 

mocha_optsoptsファイル名:

だから "alda:mocha": "mocha test/spec/admin.pclocal.us/dashboard/alice/*.js",aldaという名前 optsファイルが含まれてい "alda:mocha": "mocha --opts mocha_opts/alda になる可能性があります。それはあなたがそれらのうちのいくつかの再利用を得ることができるように

optsファイルは、コマンドライン引数で上書きすることができます場合に役立ちます

はまた、-Rは、--reporterの省略形です。

私はBabelを一括して使用しています。私はdb、client、およびapiサーバを同じプロジェクトに持っています。物事は少し外れてしまった。いくつかのシェル/ノードスクリプトと一緒にoptsが私を助けました。

"scripts": { 
    "test": "npm run client:test && npm run api:test", 
    "knex": "babel-node node_modules/.bin/knex --cwd db", 
    "api:test": "mocha --opts api/__dev__/mocha.unit", 
    "api:cover": "api/__dev__/api-cover.sh", 
    "api:tdd": "npm run -s db:cycle && npm run -s api:test -- -w -R min", 
    "api:start": "babel-node api/server", 
    "api:watch": "nodemon --exec npm run -s api:start", 
    "client:test": "mocha --opts client/__dev__/mocha.unit", 
    "client:tdd": "npm run -s client:test -- -w -R min", 
    "client:cover": "client/__dev__/client-cover.sh", 
    "client:build": "babel-node scripts/client-build.js", 
    "client:watch": "npm run -s client:build -- --watch", 
    "db:make": "npm run knex migrate:make", 
    "db:latest": "npm run -s knex migrate:latest", 
    "db:rollback": "npm run -s knex migrate:rollback", 
    "db:cycle": "npm run -s db:rollback && npm run -s db:latest" 
} 
+0

mocha.optsファイルにファイルパスを追加し、npmスクリプトでファイルパスを参照する際に問題が発生しました。ここで私はmocha.opts '--alda test/spec/admin.pclocal.us/dashboard/alice/*。js'に入れて、npmスクリプトは' 'alda:mocha":mocha - --opts - --alda "'私はaldaが未知の選択肢であるというエラーが出ています。 mocha.opts設定ファイルで 'mocha_opts/alda'とは何を表していますか? [ここ](https://mochajs.org/#usage)に記載されているオプションのみを使用できますか? – azemPC

+0

はい、私が知っている唯一のサポートされているオプションです。 'mocha_opts /'は、 'opts'ファイルを保存できるディレクトリの例です。 'alda'は私が与えたファイル名の例です。 – thebearingedge

+0

私の答えに 'opts'を追加しました – thebearingedge

0

のは、そのJSONファイルは、タスク(コメントはありません、ない変数)を構築保存するのに適した場所ではない、とpackage.jsonはすでに、特に雑然としている認識しましょう。

gulp-shelterは、ビルドスクリプトを合理化する良い方法です。bashとmakeを学ぶことがあなた、チーム、外部の貢献者にとってオプションではない場合は、ビルド・タスクをJSファイルに保存し、NPMスクリプトとgulpfileのすべての利点(簡潔な可読性と再利用性)を組み合わせることができます。

const gulp = require('gulp'); 
const shelter = require('gulp-shelter')(gulp); 

const testDir = 'test/spec/'; 

const clean = `rm -rf report/*`; 
const report = `${clean}; mkdir report`; 
const testsMocha = `mocha ${testDir}`; 
const allTestsMocha = `${testsMocha} --recursive`; 
const allTestsJson = `${allTestsMocha} --reporter json`; 
const allTestsJsonReport = `${allTestsJson} > report/all-tests`; 
... 

shelter({ 
    'clean': clean, 
    'report': report, 
    'all-tests:mocha': allTestsMocha, 
    'all-tests:json': allTestsJson, 
    'all-tests:jsonReport': allTestsJsonReport, 
    ... 
}); 
関連する問題