"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.baseGenerator = exports.baseGeneratorId = void 0;
const path = __importStar(require("node:path"));
const Generator = __importStar(require("@postxl/generator"));
const devDependencies = [
    { packageName: Generator.toPostXlPackageName('@postxl/cli') },
    { packageName: Generator.toPostXlPackageName('@postxl/generator') },
    { packageName: Generator.toPostXlPackageName('@postxl/generators') },
    { packageName: Generator.toPostXlPackageName('@postxl/schema') },
    { packageName: '@dotenvx/dotenvx', version: '1.51.2' },
    { packageName: '@eslint/eslintrc', version: '3.3.3' },
    { packageName: '@eslint/js', version: '9.39.2' },
    { packageName: '@typescript-eslint/eslint-plugin', version: '8.50.0' },
    { packageName: '@typescript-eslint/parser', version: '8.50.0' },
    { packageName: 'chokidar-cli', version: '3.0.0' },
    { packageName: 'eslint-plugin-react-hooks', version: '7.0.1' },
    { packageName: 'eslint-plugin-react', version: '7.37.5' },
    { packageName: 'eslint-plugin-simple-import-sort', version: '12.1.1' },
    { packageName: 'eslint-plugin-storybook', version: '10.1.10' },
    { packageName: 'eslint', version: '9.39.2' },
    { packageName: 'globals', version: '16.5.0' },
    { packageName: 'jest', version: '30.2.0' },
    { packageName: 'prettier', version: '3.7.4' },
    { packageName: 'rimraf', version: '6.1.2' },
    { packageName: 'ts-node', version: '10.9.2' },
];
const workspaceScripts = [
    { name: 'generate:project:pxl', command: 'node ../../packages/cli/dist/index.js generate' },
    {
        name: 'generate:watch',
        command: "chokidar '../../packages/**/dist/**' '../../generators/**/dist/**' '../../generators/**/template/**' './generate.ts' './postxl-schema.json' -c 'pnpm run generate:project:pxl -f -i'",
    },
    { name: 'pxl', command: 'node ../../packages/cli/dist/index.js' },
    { name: 'prettier:check', command: 'prettier --check "**/*.{ts,tsx}" --config ../../prettier.config.js' },
];
const standaloneScripts = [
    { name: 'generate:project:pxl', command: 'pnpm pxl generate' },
    { name: 'prettier:check', command: 'prettier --check "**/*.{ts,tsx}"' },
];
const baseScripts = [
    { name: 'lint', command: 'pnpm run -r lint' },
    { name: 'setup', command: './scripts/setup.sh' },
    { name: 'docker', command: './scripts/docker.sh' },
    { name: 'generate', command: 'pnpm run generate:project' },
    { name: 'generate:project', command: 'pnpm run generate:project:pxl' },
];
const pnpmWorkspaceYaml = `packages:
  - 'backend'
  - 'frontend'
  - 'e2e'
`;
const turboJson = `{
  "$schema": "https://turbo.build/schema.json",
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**"]
    }
  }
}
`;
exports.baseGeneratorId = Generator.toGeneratorInterfaceId('base');
exports.baseGenerator = {
    id: exports.baseGeneratorId,
    implementsInterface: [exports.baseGeneratorId],
    requires: [],
    register: (context) => {
        const isWorkspaceProject = context.schema.projectType === 'workspace';
        const base = {
            packageJson: {
                name: Generator.toPostXlPackageName(`@postxl/${context.schema.slug}`),
                description: context.schema.description,
                dependencies: [],
                devDependencies,
                scripts: [...baseScripts, ...(isWorkspaceProject ? workspaceScripts : standaloneScripts)],
                isInPostXlWorkspace: isWorkspaceProject,
            },
            dotEnvExamples: [],
        };
        return {
            ...context,
            base,
        };
    },
    generate: async (ctx) => {
        const vfs = new Generator.VirtualFileSystem();
        const templateContext = { schema: ctx.schema };
        const SONAR_PROJECT_PROPERTIES_FILENAME = 'sonar-project.properties';
        // Load the template, excluding files that need template substitution
        await vfs.loadFolder({
            diskPath: path.resolve(__dirname, './template'),
            filter: (filePath) => !filePath.endsWith(SONAR_PROJECT_PROPERTIES_FILENAME),
        });
        // Load file with template substitution
        const sonarProjectPropertiesContent = await Generator.generateFromTemplate({
            file: path.resolve(__dirname, './template', SONAR_PROJECT_PROPERTIES_FILENAME),
            context: templateContext,
        });
        if (sonarProjectPropertiesContent.isErr()) {
            throw new Error(`Failed to generate sonar-project.properties: ${sonarProjectPropertiesContent.unwrapErr().message}`);
        }
        vfs.write(`/${SONAR_PROJECT_PROPERTIES_FILENAME}`, sonarProjectPropertiesContent.unwrap());
        // write dynamic files
        vfs.write('/package.json', Generator.generatePackageJson(ctx.base.packageJson));
        const env = ctx.base.dotEnvExamples.join('\n');
        vfs.write('/.env.example', env);
        // We write the .env file so that the project can get started without the need for the developer to
        // copy the .env.example file. The .env file is not committed to the repository as it is part of the
        // .gitignore file.
        vfs.write('/.env', env);
        if (ctx.schema.projectType === 'standalone') {
            vfs.write('pnpm-workspace.yaml', pnpmWorkspaceYaml);
            vfs.write('turbo.json', turboJson);
        }
        ctx.vfs.insertFromVfs({ vfs, targetPath: '.' });
        return ctx;
    },
};
//# sourceMappingURL=base.generator.js.map