有没有更好的方法来构建我的NPM脚本,使它们更具可读性?

时间:2015-12-09 20:18:29

标签: javascript node.js npm

我已经开始依赖NPM脚本作为我测试过程的主要工具。

随着我编写更多NPM脚本,订单和结构越来越难以一目了然。

以下是我当前的脚本:

{
  "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"
}

这仅适用于大约8次左右的测试。我计划编写脚本来将报告输出到junit格式,它已经开始看起来像眼睛疲劳。

如何构建我的npm脚本以使其更具可读性?

3 个答案:

答案 0 :(得分: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 :(得分:1)

要清理mocha测试的文件路径,您可以考虑将它们移至opts个文件。

所以 "alda:mocha": "mocha test/spec/admin.pclocal.us/dashboard/alice/*.js", 可能成为 "alda:mocha": "mocha --opts mocha_opts/alda 名为opts的{​​{1}}文件包含:

alda

当然,test/spec/admin.pclocal.us/dashboard/alice/*.js --recursive mocha_opts文件名可以是您想要的任何内容。

另外,opts-R的简写,如果有帮助的话

--reporter文件可以被命令行参数覆盖,因此您可以从中重用它们。

我现在使用Babel,并在同一个项目中使用我的db,client和api服务器。事情有点失控。除了一些shell / node脚本opts帮助了我。

opts

答案 2 :(得分:0)

让我们承认json文件不是存储构建任务的好地方(没有注释,没有变量),而且package.json已经特别杂乱。

gulp-shelter可能是合理化构建脚本的好方法,如果学习bash并且make不是您,您的团队和外部贡献者的选择。它允许您将构建任务存储在JS文件中,并结合NPM脚本和gulpfiles的所有优点:简洁的可读性和可重用性。

以下是如何简化您的任务:

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,
    ...
});