name: Static Analysis

on:
  pull_request:
  push:
    branches:
      - main

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

permissions:
  contents: write
  pull-requests: write
  actions: read

jobs:
  npm-fmt-check:
    runs-on: 'ubuntu-22.04'
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'npm'
      - run: npm install
      - run: npm run fmt:check

  npm-build-wasm:
    uses: ./.github/workflows/build-wasm.yml

  npm-tsc:
    runs-on: ubuntu-latest
    needs: npm-build-wasm

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'npm'
      - run: npm install

      - name: Download all artifacts
        uses: actions/download-artifact@v4

      - name: Copy prepared wasm
        run: |
          ls -R prepared-wasm
          cp prepared-wasm/kcl_wasm_lib_bg.wasm public
          mkdir rust/kcl-wasm-lib/pkg
          cp prepared-wasm/kcl_wasm_lib* rust/kcl-wasm-lib/pkg

      - name: Copy prepared ts-rs bindings
        run: |
          ls -R prepared-ts-rs-bindings
          mkdir rust/kcl-lib/bindings
          cp -r prepared-ts-rs-bindings/* rust/kcl-lib/bindings/

      - run: npm run tsc

  npm-lint:
    runs-on: ubuntu-latest
    needs: npm-build-wasm

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'npm'
      - run: npm install

      - name: Download all artifacts
        uses: actions/download-artifact@v4

      - name: Copy prepared wasm
        run: |
          ls -R prepared-wasm
          cp prepared-wasm/kcl_wasm_lib_bg.wasm public
          mkdir rust/kcl-wasm-lib/pkg
          cp prepared-wasm/kcl_wasm_lib* rust/kcl-wasm-lib/pkg

      - name: Copy prepared ts-rs bindings
        run: |
          ls -R prepared-ts-rs-bindings
          mkdir rust/kcl-lib/bindings
          cp -r prepared-ts-rs-bindings/* rust/kcl-lib/bindings/

      - run: npm run lint

  npm-circular-dependencies:
    runs-on: ubuntu-latest
    needs: npm-build-wasm

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'npm'
      - run: npm install

      - name: Download all artifacts
        uses: actions/download-artifact@v4

      - name: Copy prepared wasm
        run: |
          ls -R prepared-wasm
          cp prepared-wasm/kcl_wasm_lib_bg.wasm public
          mkdir rust/kcl-wasm-lib/pkg
          cp prepared-wasm/kcl_wasm_lib* rust/kcl-wasm-lib/pkg

      - name: Copy prepared ts-rs bindings
        run: |
          ls -R prepared-ts-rs-bindings
          mkdir rust/kcl-lib/bindings
          cp -r prepared-ts-rs-bindings/* rust/kcl-lib/bindings/

      - run: npm run circular-deps:diff

  python-codespell:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Run codespell
        uses: crate-ci/typos@v1.32.0
