<!--

    Copyright © 2016-2026 The Thingsboard Authors

    Licensed 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.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.thingsboard</groupId>
        <version>4.4.0-SNAPSHOT</version>
        <artifactId>msa</artifactId>
    </parent>
    <groupId>org.thingsboard.msa</groupId>
    <artifactId>js-executor</artifactId>
    <packaging>pom</packaging>

    <name>ThingsBoard JavaScript Executor Microservice</name>
    <url>https://thingsboard.io</url>
    <description>Service executing JavaScript functions in sandboxed environment</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <main.dir>${basedir}/../..</main.dir>
        <docker.name>tb-js-executor</docker.name>
        <pkg.name>tb-js-executor</pkg.name>
        <pkg.type>js</pkg.type>
        <pkg.process-resources.phase>process-resources</pkg.process-resources.phase>
        <pkg.package.phase>package</pkg.package.phase>
        <pkg.linux.dist>${project.build.directory}/package/linux</pkg.linux.dist>
        <pkg.win.dist>${project.build.directory}/package/windows</pkg.win.dist>
        <docker.push-arm-amd-image.phase>pre-integration-test</docker.push-arm-amd-image.phase>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.sun.winsw</groupId>
            <artifactId>winsw</artifactId>
            <classifier>bin</classifier>
            <type>exe</type>
            <scope>provided</scope>
        </dependency>
        <!--
            Reactor-only ordering dep (NOT a real classpath dependency).
            Forces `mvn -T<n>` to serialize this module after web-ui so that
            no two `yarn install` / `yarn run pkg` invocations can overlap on
            the same agent. Concurrent yarn 1.x processes share ~/.cache/yarn
            and have intermittently produced `tsc: not found` failures during
            yarn pkg (incomplete typescript extraction in node_modules).
            The chain is: ui-ngx -> msa/web-ui -> msa/js-executor.
            type=pom + provided + wildcard exclusions keep nothing on the classpath.
        -->
        <dependency>
            <groupId>org.thingsboard.msa</groupId>
            <artifactId>web-ui</artifactId>
            <version>${project.version}</version>
            <type>pom</type>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <configuration>
                    <installDirectory>target</installDirectory>
                    <workingDirectory>${basedir}</workingDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>install node and yarn</id>
                        <goals>
                            <goal>install-node-and-yarn</goal>
                        </goals>
                        <configuration>
                            <nodeVersion>v22.22.2</nodeVersion>
                            <yarnVersion>v1.22.22</yarnVersion>
                        </configuration>
                    </execution>
                    <execution>
                        <id>yarn install</id>
                        <goals>
                            <goal>yarn</goal>
                        </goals>
                        <configuration>
                            <arguments>install --non-interactive --check-files --network-concurrency 4 --network-timeout 100000 --mutex network</arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>yarn pkg</id>
                        <goals>
                            <goal>yarn</goal>
                        </goals>
                        <phase>compile</phase>
                        <configuration>
                            <arguments>--mutex network run pkg</arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>yarn test</id>
                        <goals>
                            <goal>yarn</goal>
                        </goals>
                        <phase>test</phase>
                        <configuration>
                            <skip>${maven.test.skip}</skip>
                            <arguments>--mutex network run test</arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>${basedir}/node_modules</directory>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-src-root</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/src</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}</directory>
                                    <includes>
                                        <include>package.json</include>
                                        <include>yarn.lock</include>
                                    </includes>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.thingsboard</groupId>
                <artifactId>gradle-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>build-docker-image</id>
                        <phase>pre-integration-test</phase>
                        <goals><goal>exec</goal></goals>
                        <configuration>
                            <skip>${dockerfile.skip}</skip>
                            <executable>docker</executable>
                            <workingDirectory>${project.build.directory}</workingDirectory>
                            <arguments>
                                <argument>build</argument>
                                <argument>-t</argument>
                                <argument>${docker.repo}/${docker.name}:latest</argument>
                                <argument>.</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>tag-docker-image</id>
                        <phase>pre-integration-test</phase>
                        <goals><goal>exec</goal></goals>
                        <configuration>
                            <skip>${dockerfile.skip}</skip>
                            <executable>docker</executable>
                            <arguments>
                                <argument>tag</argument>
                                <argument>${docker.repo}/${docker.name}:latest</argument>
                                <argument>${docker.repo}/${docker.name}:${project.version}</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>yarn-start</id>
            <activation>
                <property>
                    <name>yarn-start</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.github.eirslett</groupId>
                        <artifactId>frontend-maven-plugin</artifactId>
                        <configuration>
                            <installDirectory>target</installDirectory>
                            <workingDirectory>${basedir}</workingDirectory>
                        </configuration>
                        <executions>
                            <execution>
                                <id>yarn start</id>
                                <goals>
                                    <goal>yarn</goal>
                                </goals>

                                <configuration>
                                    <arguments>start</arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>push-docker-image</id>
            <activation>
                <property>
                    <name>push-docker-image</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>push-latest-docker-image</id>
                                <phase>pre-integration-test</phase>
                                <goals><goal>exec</goal></goals>
                                <configuration>
                                    <executable>docker</executable>
                                    <arguments>
                                        <argument>push</argument>
                                        <argument>${docker.repo}/${docker.name}:latest</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>push-version-docker-image</id>
                                <phase>pre-integration-test</phase>
                                <goals><goal>exec</goal></goals>
                                <configuration>
                                    <executable>docker</executable>
                                    <arguments>
                                        <argument>push</argument>
                                        <argument>${docker.repo}/${docker.name}:${project.version}</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    <repositories>
        <repository>
            <id>jenkins</id>
            <name>Jenkins Repository</name>
            <url>https://repo.jenkins-ci.org/releases</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>
