name: Yarn Install Build Test and Lint

inputs:
  cache_id:
    description: ID to use in order to cache yarn install
    required: true
  working_dir:
    description: Directory in which to perform the yarn tasks
    required: true
  skip_test:
    description: If true, testing is skipped. This should be used when the tests are end-to-end and thus need a locally running test-validator with deployed Rust programs.
    required: false
    default: false

  build_core:
    description: If not false it will build the core package before running checks
    required: false
    default: true
  build_auction:
    description: If true it will build the auction package before running checks
    required: false
    default: false
  build_token_metadata:
    description: If true it will build the token_metadata package before running checks
    required: false
    default: false
  build_token_vault:
    description: If true it will build the token_vault package before running checks
    required: false
    default: false

runs:
  using: composite
  steps:
    - uses: actions/cache@v2
      with:
        path: '**/node_modules'
        key: ${{ inputs.cache_id }}-${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}

    ##############
    # Prepare Core and Dependencies
    ##############
    
    # All contract SDKs depend on mpl-core so we need to build at least that
    # We avoid running build in the project root to avoid a check for a contract failing due to
    # an issue in another contract which would cause a build failure.
    # For the metaplex contract this is unfortunately not entirely possible as it depends on core, token-metadat,
    # token-vault and auction

    - name: Install and Build core
      if: inputs.build_core == 'true'
      run: |
        echo 'Install and Build core: yarn install'
        yarn install
        echo 'Install and Build core: yarn build'
        yarn build
      working-directory: ./core/js 
      shell: bash

    - name: Install and Build auction
      if: inputs.build_auction == 'true'
      run: |
        echo 'Install and Build auction: yarn install'
        yarn install
        echo 'Install and Build auction: yarn build'
        yarn build
      working-directory: ./auction/js 
      shell: bash

    - name: Install and Build token-metadata
      if: inputs.build_token_metadata == 'true'
      run: |
        echo 'Install and Build token-metadata: yarn install'
        yarn install
        echo 'Install and Build token-metadata: yarn build'
        yarn build
      working-directory: ./token-metadata/js 
      shell: bash

    - name: Install and Build token-vault
      if: inputs.build_token_vault == 'true'
      run: |
        echo 'Install and Build token-vault: yarn install'
        yarn install
        echo 'Install and Build token-vault: yarn build'
        yarn build
      working-directory: ./token-vault/js 
      shell: bash

    ##############
    # Verify Contract 
    ##############
    - name: Install modules
      run: yarn install
      working-directory: ${{ inputs.working_dir }}
      shell: bash

    - name: Build TypeScript 
      run: yarn build
      working-directory: ${{ inputs.working_dir }}
      shell: bash

    - name: Test TypeScript
      if: inputs.skip_test == 'false'
      run: yarn test
      working-directory: ${{ inputs.working_dir }}
      shell: bash

    - name: Lint TypeScript 
      run: yarn lint
      working-directory: ${{ inputs.working_dir }}
      shell: bash
