反应原生项目中的esformatter的Git和用法

时间:2017-01-19 08:07:15

标签: javascript git react-native

我想知道如何在提交之前对我的git项目中更改的所有js文件运行esformatter。 如果这不可能,请如何运行esformatter到所有js文件。

这是我的包json。我在devops方面真的很新。任何建议都表示赞赏,但我真的很想在提交之前实现自动化格式。非常感谢!

{
      "name": "asdasda",
      "version": "0.0.1",
      "private": true,
      "scripts": {
        "start": "node node_modules/react-native/local-cli/cli.js start",
        "precommit": "esformatter (to a changedfile using husky its possible?)"
      },
      "dependencies": {
        "@remobile/react-native-splashscreen": "^1.0.4",
        "babel-eslint": "^6.0.0",
        "eslint": "^3.13.1",
        "eslint-plugin-react": "^6.9.0",
        "eslint-plugin-react-native": "^2.2.1",
        "firebase": "^3.4.1",
        "husky": "^0.12.0",
        "moment": "^2.15.2",
        "react": "15.3.1",
        "react-native": "0.33.0"
      },
      "rnpm": {
        "assets": [
          "./assets"
        ]
      },
      "devDependencies": {
        "esformatter-jsx": "^7.4.1"
      },
      "esformatter": {
        "plugins": [
        "esformatter-jsx"
      ],
      // this is the section this plugin will use to store the settings for the jsx formatting 
      "jsx": {
        // whether to recursively format jsx expressions with esformatter 
        // set this to false if you don't want JSXExpressions to be formatted recursively, like when using problematic plugins 
        "formatJSXExpressions": true,
        // By default ObjectExpression and ArrayExpression in JSXExpressions are inlined, 
        // if false, the Expression might expand several lines 
        "JSXExpressionsSingleLine": true,
        // by default is true if set to false it works the same as esformatter-jsx-ignore 
        "formatJSX": true,
        // keep the node attributes on the same line as the open tag. Default is true. 
        // Setting this to false will put each one of the attributes on a single line 
        "attrsOnSameLineAsTag": true,
         // how many attributes should the node have before having to put each 
         // attribute in a new line. Default 1 
        "maxAttrsOnTag": 1,
        // if the attributes are going to be put each one on its own line, then keep the first 
        // on the same line as the open tag 
        "firstAttributeOnSameLine": false,
        // default to one space. Make it empty if you don't like spaces between JSXExpressionContainers 
        "spaceInJSXExpressionContainers": " ",
        // align the attributes with the first attribute (if the first attribute was kept on the same line as on the open tag) 
        "alignWithFirstAttribute": true,
        "htmlOptions": { // same as the ones passed to js-beautifier.html 
          "brace_style": "collapse",
          "indent_char": " ",
          "indent_size": 2,
          "max_preserve_newlines": 2,
          "preserve_newlines": true
          //wrap_line_length: 250 
        }
      }
    }
    }

1 个答案:

答案 0 :(得分:1)

您正在寻找预先提交Git Hook。我还没有测试过这个,但我认为你需要这样的东西:

#!/usr/bin/env bash
EXIT_CODE=0

ESFORMATTER_ERRORS=$( esformatter src/**/*.js | tee )
if  [[ $ESFORMATTER_ERRORS ]]; then
    echo "$ESFORMATTER_ERRORS"
    EXIT_CODE=1
fi

exit $EXIT_CODE