image: node:8.4.0

variables:
    NODE_ENV: development

cache:
    key: "$CI_COMMIT_REF_NAME"
    paths:
        - node_modules/
        - dist/

stages:
    - setup_env
    - build
    - test
    - deploy

setup_env:
    stage: setup_env
    script:
        - npm install

build:
    stage: build
    script:
        # make sure that cache have been well recovered
        - sh -c "[ -f ./node_modules/.bin/rollup ]"

        - npm run-script build

        # make sure that the build worked
        - sh -c "[ -f ./dist/WikiquoteApi.commonjs.js ]"
        - sh -c "[ -f ./dist/WikiquoteApi.browser.js ]"
        - sh -c "[ -f ./dist/WikiquoteApi.module.mjs ]"

test:source:
    stage: test
    script:
        - npm test

test:production:
    stage: test
    variables:
        NODE_ENV: production
    script:
        - npm test
    retry: 2
    allow_failure: true


publish to NPM:
    stage: deploy
    only:
        - /^v\d+\.\d+\.\d.*$/
    script:
        # make sure that cache have been well recovered
        - sh -c "[ -f ./dist/WikiquoteApi.commonjs.js ]"
        - sh -c "[ -f ./dist/WikiquoteApi.browser.js ]"
        - sh -c "[ -f ./dist/WikiquoteApi.module.mjs ]"

        - echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
        - npm publish
