/* Version 2.3.0 */
def dependentJobs = []

/* Add more lines for each dependent job */

node {
    def GIT_REV


    stage('Clone repository') {
        /* Let's make sure we have the repository cloned to our workspace */
        checkout(
            [
                $class: 'GitSCM',
                branches: [[name: '*/master']],
                doGenerateSubmoduleConfigurations: false,
                extensions: [
                    [
                        $class: 'SubmoduleOption',
                        disableSubmodules: false,
                        parentCredentials: true,
                        recursiveSubmodules: true,
                        reference: '',
                        trackingSubmodules: false
                    ],
                    [
                        $class: 'PathRestriction',
                        excludedRegions: 'package.*\\.json'
                    ]
                ],
                submoduleCfg: [],
                userRemoteConfigs: [
                    [url: 'git@github.com:quantotto/hydra_js_sdk.git']
                ]
            ]
        )
    }

    stage('npm build') {
        withNPM(npmrcConfig: 'quantotto-npmrc') {
            sh '''git config user.name "Quantotto Inc."
            git config user.email "quantottoinc@gmail.com"
            git config --global push.default matching
            npm config set git-tag-version=false
            npm version patch
            npm install'''
        }
    }

    stage('commit VERSION') {
        sh '''git commit -am "[jenkins-new-package-json]"
        git push origin HEAD:master'''
    }

    stage('Publish to npm') {
        withNPM(npmrcConfig: 'quantotto-npmrc') {
            sh 'npm publish'
        }
    }
}

stage('run dependent projects') {
    for (String jobName in dependentJobs) {
        triggerJob(jobName)
    }
}

def triggerJob(jobName) {
    build(
        job: jobName,
        parameters: [],
    )
}
