# You can use a Docker image from Docker Hub or your own container
# registry for your build environment.
image: node:9.11.1

pipelines:
  default:
    - step:
      name: Testing build
      caches:
        - node
      script:
        # Generates a .npmrc file configured for installing private modules:
        #
        # NPM_REGISTRY_URL: the full URL of your private registry
        #                   defaults to registry.npmjs.org.
        # NPM_TOKEN: secret token for installing private modules. This
        #            this token can be found in your .npmrc, after logging in.
        - printf "//`node -p \"require('url').parse(process.env.NPM_REGISTRY_URL || 'https://registry.npmjs.org').host\"`/:_authToken=${NPM_TOKEN}\nregistry=${NPM_REGISTRY_URL:-https://registry.npmjs.org}\n" >> ~/.npmrc
        # installs public and private moduless.
        - npm install
        # run the tests.
        - npm test
  branches:
    master:
      - step:
          name: Deploy to development
          caches:
            - node
          deployment: test
          script:
            - printf "//`node -p \"require('url').parse(process.env.NPM_REGISTRY_URL || 'https://registry.npmjs.org').host\"`/:_authToken=${NPM_TOKEN}\nregistry=${NPM_REGISTRY_URL:-https://registry.npmjs.org}\n" >> ~/.npmrc
            - npm install
            - npm test
  tags:
    '**':
      - step:
          name: Deploy to staging
          caches:
            - node
          deployment: staging
          script:
            - printf "//`node -p \"require('url').parse(process.env.NPM_REGISTRY_URL || 'https://registry.npmjs.org').host\"`/:_authToken=${NPM_TOKEN}\nregistry=${NPM_REGISTRY_URL:-https://registry.npmjs.org}\n" >> ~/.npmrc
            - npm install
            - npm test
      - step:
          name: Deploy to production
          caches:
            - node
          deployment: production
          trigger: manual
          script:
            - printf "//`node -p \"require('url').parse(process.env.NPM_REGISTRY_URL || 'https://registry.npmjs.org').host\"`/:_authToken=${NPM_TOKEN}\nregistry=${NPM_REGISTRY_URL:-https://registry.npmjs.org}\n" >> ~/.npmrc
            - npm install
            - npm test
            - git config --global user.email "info@rajeevsiewnath.com"
            - git config --global user.name "Rajeev Siewnath"
            - npm version from-git --allow-same-version --force
            - npm publish