image: node:14

definitions:
  steps:
    - step: &lint
        name: Linting
        caches: 
          - node
        script:
          - echo "Linting Started..."
          - npm install
          - npm run lint:fix
          - echo "Linting for all .js files in repository completed"

    - step: &test
        name: Testing
        caches:
          - node
        script:
          - echo "Testing Started..."
          - apt-get update && apt-get install -y
          - apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libgdk-pixbuf2.0-dev libglib2.0-0 libgtk-3-0 libgtk-3-dev libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libnss3 libnss3-dev libxss-dev lsb-release xdg-utils wget
          - npm run test -- -u
          - echo "Testing completed"

    - step: &build-package
        name: Build npm package
        caches: 
          - node
        script:
          - echo "NPM build Started..."
          - npm version patch -m "Upgrade package version to %s [skip ci]"
          - git push 
          - npm run build:rollup
          - npm run build:types
          - npm run build:documentation
          - npm run build:storybook
          - ls -lR dist # identify all files inside dist directory
          - echo "NPM package build completed"
        artifacts:
          - dist/**

    - step: &publish-package
        name: Publish npm package
        caches: 
          - node
        deployment: production
        script:
          - pipe: atlassian/npm-publish:0.3.2
            variables:
              NPM_TOKEN: ${NPM_TOKEN} 

pipelines:
  pull-requests:
    '**':
      - step: *lint
      - step: *test

  branches:
    master:
      - step: *lint
      - step: *build-package
      - step: *test
      - step: *publish-package