/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

configure([project(":solr:packaging"), project(":solr:solr-ref-guide"), project(":solr:webapp")]) {
    apply plugin: libs.plugins.nodegradle.node.get().pluginId

    def npmRegistry = "${ -> propertyOrEnvOrDefault("solr.npm.registry", "SOLR_NPM_REGISTRY", '') }"
    if (!npmRegistry.isEmpty()) {
        tasks.npmSetup {
            args.addAll(['--registry', npmRegistry])
        }

        afterEvaluate {
            tasks.withType(NpmTask).each { npmTask ->
                npmTask.environment.put('NPM_CONFIG_REGISTRY', npmRegistry)
            }
            tasks.withType(NpxTask).each { npxTask ->
                npxTask.environment.put('NPM_CONFIG_REGISTRY', npmRegistry)
            }
        }
    }

    project.ext {
        nodeProjectDir = layout.projectDirectory.dir(".gradle/node")
    }

    node {
        download = true
        version = libs.versions.nodejs.get()

        def nodeDistUrl = "${ -> propertyOrEnvOrDefault("solr.node.distUrl", "SOLR_NODE_DIST_URL", '') }"
        if (!nodeDistUrl.isEmpty()) {
            distBaseUrl = nodeDistUrl
        }

        // The directory where Node.js is unpacked (when download is true)
        workDir = file("${project.ext.nodeProjectDir.getAsFile().path}/nodejs")

        // The directory where npm is installed (when a specific version is defined)
        npmWorkDir = file("${project.ext.nodeProjectDir.getAsFile().path}/npm")

        // The Node.js project directory location
        // This is where the package.json file and node_modules directory are located
        // By default it is at the root of the current project
        nodeProjectDir = project.ext.nodeProjectDir
    }
}
