#!/usr/bin/env groovy

pipeline {
    agent {
        label 'slave-group-release'
    }

    parameters {
        string(name: 'version', defaultValue: '0.0.0', description: 'Release version')
        string(name: 'branch', defaultValue: 'main', description: 'Branch to release from')
    }

    options {
        timeout(time: 60, unit: 'MINUTES')
    }

    stages {
        stage('Checkout') {
            steps {
                sh "git checkout ${branch}"
                sh "git remote -v"
            }
        }

        stage('Install release package') {
            steps {
                nodejs(nodeJSInstallationName: 'Node 14') {
                    sh 'rm -drf node_modules/'
                    sh 'npm config ls'
                    sh 'npm install'
                    sh 'npm install npm-release -g'
                }
            }
        }

        stage('Release') {
            steps {
                nodejs(nodeJSInstallationName: 'Node 14') {
                    withCredentials([string(credentialsId: 'npmauthtoken', variable: 'NPM_AUTH_TOKEN')]) {
                        sh './set-npm-auth-token.sh'
                    }
                    sh "npm-release ${version}"
                }
            }
        }

        stage('Generate JS API docs and upload') {
            steps {
                nodejs(nodeJSInstallationName: 'Node 14') {
                    sh './node_modules/.bin/jsdoc lib/*.js'
                    sh 'mv out apidocs'
                    sh 'mkdir 1.0'
                    sh 'mv apidocs 1.0'
                    sh 'rsync -rv --protocol=28 1.0 infinispan@filemgmt.jboss.org:/docs_htdocs/infinispan/hotrod-clients/javascript'
                    sh 'mv 1.0 out'
                }
            }
        }

    }
}
