pipeline {
    agent any
    tools { nodejs 'NodeJS' }
    environment {
        DEPLOY_DIR = '/var/www/dashboard'
    }
    stages {
        stage('Install') {
            steps {
                sh 'npm install --legacy-peer-deps'
            }
        }
        stage('Build') {
             steps {
                sh 'CI=false npm run build'
             }
        }
        stage('Deploy') {
            steps {
                sh """
                    rm -rf ${DEPLOY_DIR}/*
                    cp -r ${WORKSPACE}/build/* ${DEPLOY_DIR}/
                    sudo systemctl reload nginx
                """
            }
        }
    }
    post {
        always { cleanWs() }
        success { echo 'Dashboard deployed successfully!' }
        failure { echo 'Dashboard deployment failed!' }
    }
}