git commit 时如何对暂存提交文件进行tsc编译校验

621 阅读1分钟
  • 背景:在git commit 提交时强制检查提交的ts文件TSC编译是否存在错误

  • 在package.json中添加sh脚本命令 sh ./bin/tslint.sh

    "scripts": {
        "tslint:commit": "sh ./bin/tslint.sh",
        "tslint:exec": "tsc --project .tsconfig-lint.json --skipLibCheck --noEmit",
    },
    
  • 创建tslint.sh主文件

        #!/bin/bash -e
        # This file is for 在提交时强制检查提交的ts文件TSC编译是否存在错误
        # 在提交时使用tsc 检查 【<https://devpress.csdn.net/react/62ec0da389d9027116a10104.html】>
        # because ts has a opened bug for not can cross tsconfig.json 【<https://github.com/microsoft/TypeScript/issues/27379】>, So we do follow
        DIR=$(dirname "$0")
        
        # includes
        . ${DIR}/includes/env.sh
        echo "$@";
        
        # create tsc lint config file
        # because ts has a opened bug for not can cross tsconfig.json 【】
        TMP=.tsconfig-lint.json
        cat >${TMP} <<EOF
        {
          "extends": "./tsconfig.json",
          "compilerOptions": {
            "listFiles": false
          },
          "include": [
         EOF
         for file in "$@"; do
            echo "    \"$file\"," >> ${TMP}
         done
         cat >>${TMP} <<EOF
            "**/*.d.ts"
            ]
         }
         EOF
    
        # run tsc lint
        ${NPM} run tslint:exec > ./node_modules/.tslint.log
        TSC_ERROR=`tail -n +5 ./node_modules/.tslint.log`
        rm -f ./node_modules/.tslint.log
        
        # remove tsc config file
        rm -f ${TMP}
    
        # emit error if exists
        if [ "${TSC_ERROR}" != "" ]; then
          echoeol
          echo "${RED}⛔ Typescript complier lint (tsc) checksum failed!${NOCOLOR}"
          echoeol
          echo "!!!以下代码ts校验失败===》》》${TSC_ERROR}"
          echoeol
          exit 1
        fi
    
    
  • 创建 env.sh此文件

        #!/bin/bash
        
        # ----------------------------------
        # Colors
        # ----------------------------------
        if test -t 1; then
          if test -n "$(tput colors)" && test $(tput colors) -ge 8; then
            NOCOLOR="\033[0m"
            RED="\033[0;31m"
            GREEN="\033[0;32m"
            ORANGE="\033[0;33m"
            BLUE="\033[0;34m"
            PURPLE="\033[0;35m"
            CYAN="\033[0;36m"
            LIGHTGRAY="\033[0;37m"
            DARKGRAY="\033[1;30m"
            LIGHTRED="\033[1;31m"
            LIGHTGREEN="\033[1;32m"
            YELLOW="\033[1;33m"
            LIGHTBLUE="\033[1;34m"
            LIGHTPURPLE="\033[1;35m"
            LIGHTCYAN="\033[1;36m"
            WHITE="\033[1;37m"
          fi
        fi
    
        # ----------------------------------
        # npm or jnpm  or cnpm
        # ----------------------------------
        if [ "${NPM}" = "" ]; then
          NPM=`[ -f './node_modules/.npminstall.done' ] && echo "jnpm" || echo "npm"`
          if [ "${RUNNER_TYPE}" = "CI" ]; then
            NPM='npm'
          fi
            echo "> using ${LIGHTPURPLE}${NPM}${NOCOLOR} as building command tool."
            echo ''
        fi
    
        # ----------------------------------
        # echo
        # ----------------------------------
        echoeol() {
            echo " "
        }