version: 2.1

defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: cimg/node:18.4.0
  resource_class: medium

jobs:
  assemble:
    <<: *defaults
    steps:
      - checkout
      - run:
          name: build
          command: |
            npm install 
            npm install -g typescript
            tsc --build tsconfig.json 
          
      - run:
          name: test - typescript
          command: npm test -- -w=2
      - run: 
          name: test - javascript
          command: |
            ./node_modules/.bin/mocha src/test/filesTestJS.js
            ./node_modules/.bin/mocha src/test/transfersTestJS.js
      - run: 
          name: show src files 
          command: ls -Rlt src
      - persist_to_workspace:
          root: ~/repo
          paths: .


  deploy:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/repo
      - run: 
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc
      - run:
          name: Publish package
          command: npm publish --access public

  

workflows:
  version: 2

  verify_codegen:
    jobs:
      - assemble:
        filters:
          branches:
            only: /^codegen-.*$/

  deploy:
      jobs:
        - assemble:
            filters:
              branches:
                ignore: /.*/
              tags:
                only: /^v[0-9]+.*$/ 
        - deploy:
            requires:
              - assemble
            filters:
              branches:
                ignore: /.*/
              tags:
                only: /^v[0-9]+.*$/
