name: npm publish
on:
  release:
    types: [published]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Install Node
        uses: actions/setup-node@v3
        with:
          node-version: '18.x'
          registry-url: 'https://registry.npmjs.org'
          always-auth: true

      - name: Install Dependencies
        run: yarn

      - name: Build & Test
        run: yarn build && yarn test

      - name: Publish npm packages
        run: |
          yarn publish --access public
          sed -i -e 's#"@stellar/js-xdr"#"js-xdr"#' package.json
          yarn publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Publish npm package under old scope
        run: |
          V=$(cat package.json | jq '.version' | sed -e 's/\"//g')
          echo "Deprecating js-xdr@$V"
          npm deprecate js-xdr@"<= $V" "⚠️ This package has moved to @stellar/js-xdr! 🚚"
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
