院长
院长
发布于 2024-04-11 / 77 阅读 / 2 评论 / 0 点赞

借助 Github Action 优化 js 文件注释

重构mio-chat前端的时候突然想把代码写的美观一些,除了定义一堆类,用上JsDocEslintPrettie以外,想起来之前在ap-plugin里看到的注释写法,以JsDoc的形式在每个文件开头注明文件的创建者,最后编辑者和最后编辑时间,还挺不错的,于是有了下面的Github Workflow文件:

name: Update LastUpdate Info

on: [push]

jobs:
  update-lastupdate:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Check commit message
        id: check_commit
        run: |
          commit_message=$(git log --format=%B -n 1 $GITHUB_SHA)
          if [[ $commit_message == *"update-lastupdate"* ]]; then
            echo "Skipping update operation as commit message contains 'update-lastupdate'."
            exit 0
          fi

      - name: Set up Git
        run: |
          git config --global user.email "[email protected]"
          git config --global user.name "Github Actions"

      - name: Update LastUpdate
        if: steps.check_commit.outcome == 'success'
        run: |
          current_time=$(date "+%Y-%m-%d %H:%M:%S")

          for file in $(find . -name "*.js"); do
            first_commit_info=$(git log --diff-filter=A --follow --format="%an <%ae>" -- $file | tail -1)
            if ! grep -q '\/\*\*' $file; then
              echo -e "/**\n * @author ${first_commit_info}\n * @lastEditor ${first_commit_info}\n * @lastEditTime ${current_time}\n */\n\n$(cat $file)" > $file
            else
              sed -i "/^\/\*\*$/,/^ \*\/$/ { s/\(@lastEditTime \).*/\1${current_time}/; s/\(@lastEditor \).*/\1${first_commit_info}/; }" $file
            fi
            git add $file
          done

          git commit -m "Update LastUpdate info" --allow-empty
          git push

应用时,需要把项目设置里的action的读写项目代码和提交PR的权限打开,然后就可以自动补齐注释了。


评论