import chalk from 'chalk';
import BaseApplicationGenerator from '../../../base-application/index.js';
export default class NodeGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnBootstrapApplication();
            await this.dependsOnJHipster('begcode:java:build-tool');
        }
    }
    get composing() {
        return this.asComposingTaskGroup({
            async compose() {
                const { buildTool } = this.jhipsterConfigWithDefaults;
                if (buildTool === 'maven') {
                    await this.composeWithJHipster('begcode:maven:frontend-plugin');
                }
                else if (buildTool === 'gradle') {
                    await this.composeWithJHipster('begcode:gradle:node-gradle');
                }
            },
        });
    }
    get [BaseApplicationGenerator.COMPOSING]() {
        return this.delegateTasksToBlueprint(() => this.composing);
    }
    get preparing() {
        return this.asPreparingTaskGroup({
            async javaNodeBuildPaths({ application }) {
                const { buildToolMaven, srcMainWebapp, javaNodeBuildPaths, clientDistDir } = application;
                javaNodeBuildPaths.push(srcMainWebapp, 'package-lock.json', 'package.json');
                if (buildToolMaven) {
                    javaNodeBuildPaths.push(clientDistDir);
                }
            },
        });
    }
    get [BaseApplicationGenerator.PREPARING]() {
        return this.delegateTasksToBlueprint(() => this.preparing);
    }
    get postPreparing() {
        return this.asPostPreparingTaskGroup({
            sortBuildFiles({ application }) {
                const { javaNodeBuildPaths } = application;
                if (javaNodeBuildPaths) {
                    const files = [...new Set(javaNodeBuildPaths)];
                    javaNodeBuildPaths.splice(0, javaNodeBuildPaths.length, ...files.sort());
                }
            },
            useNpmWrapper({ application }) {
                if (application.useNpmWrapper) {
                    this.useNpmWrapperInstallTask();
                }
            },
        });
    }
    get [BaseApplicationGenerator.POST_PREPARING]() {
        return this.delegateTasksToBlueprint(() => this.postPreparing);
    }
    get writing() {
        return this.asWritingTaskGroup({
            async writing({ application }) {
                await this.writeFiles({
                    blocks: [
                        {
                            condition: (ctx) => ctx.useNpmWrapper && ctx.applicationType !== 'commonmodule',
                            renameTo: (data, path) => path.replace('_pnpm', ''),
                            templates: ['npmw_pnpm', 'npmw_pnpm.cmd'],
                        },
                    ],
                    context: application,
                });
            },
        });
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    useNpmWrapperInstallTask() {
        this.setFeatures({
            customInstallTask: async (preferredPm, defaultInstallTask) => {
                const buildTool = this.jhipsterConfigWithDefaults.buildTool;
                if ((preferredPm && preferredPm !== 'npm') || this.jhipsterConfig.skipClient || (buildTool !== 'gradle' && buildTool !== 'maven')) {
                    await defaultInstallTask();
                    return;
                }
                const npmCommand = process.platform === 'win32' ? 'npmw' : './npmw';
                try {
                    await this.spawn(npmCommand, ['install'], { preferLocal: true });
                }
                catch (error) {
                    this.log.error(chalk.red(`Error executing '${npmCommand} install', please execute it yourself. (${error.shortMessage})`));
                }
            },
        });
    }
}
