"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseTemplate = void 0;
const path_1 = __importDefault(require("path"));
const debug_1 = __importDefault(require("debug"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const determine_author_1 = __importDefault(require("./determine-author"));
// eslint-disable-next-line @typescript-eslint/no-var-requires
const currentForgeVersion = require('../package.json').version;
const d = (0, debug_1.default)('electron-forge:template:base');
const tmplDir = path_1.default.resolve(__dirname, '../tmpl');
class BaseTemplate {
    constructor() {
        this.templateDir = tmplDir;
        this.requiredForgeVersion = currentForgeVersion;
    }
    get devDependencies() {
        const packageJSONPath = path_1.default.join(this.templateDir, 'package.json');
        if (fs_extra_1.default.pathExistsSync(packageJSONPath)) {
            const packageDevDeps = fs_extra_1.default.readJsonSync(packageJSONPath).devDependencies;
            if (packageDevDeps) {
                return Object.entries(packageDevDeps).map(([packageName, version]) => {
                    if (version === 'ELECTRON_FORGE/VERSION') {
                        version = `^${currentForgeVersion}`;
                    }
                    return `${packageName}@${version}`;
                });
            }
        }
        return [];
    }
    async initializeTemplate(directory, { copyCIFiles }) {
        return [
            {
                title: 'Copying starter files',
                task: async () => {
                    d('creating directory:', path_1.default.resolve(directory, 'src'));
                    await fs_extra_1.default.mkdirs(path_1.default.resolve(directory, 'src'));
                    const rootFiles = ['_gitignore', 'forge.config.js'];
                    if (copyCIFiles) {
                        d(`Copying CI files is currently not supported - this will be updated in a later version of Forge`);
                    }
                    const srcFiles = ['index.css', 'index.js', 'index.html', 'preload.js'];
                    for (const file of rootFiles) {
                        await this.copy(path_1.default.resolve(tmplDir, file), path_1.default.resolve(directory, file.replace(/^_/, '.')));
                    }
                    for (const file of srcFiles) {
                        await this.copy(path_1.default.resolve(tmplDir, file), path_1.default.resolve(directory, 'src', file));
                    }
                },
            },
            {
                title: 'Initializing package.json',
                task: async () => {
                    await this.initializePackageJSON(directory);
                },
            },
        ];
    }
    async copy(source, target) {
        d(`copying "${source}" --> "${target}"`);
        await fs_extra_1.default.copy(source, target);
    }
    async copyTemplateFile(destDir, basename) {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        await this.copy(path_1.default.join(this.templateDir, basename), path_1.default.resolve(destDir, basename));
    }
    async initializePackageJSON(directory) {
        const packageJSON = await fs_extra_1.default.readJson(path_1.default.resolve(__dirname, '../tmpl/package.json'));
        packageJSON.productName = packageJSON.name = path_1.default.basename(directory).toLowerCase();
        packageJSON.author = await (0, determine_author_1.default)(directory);
        packageJSON.scripts.lint = 'echo "No linting configured"';
        d('writing package.json to:', directory);
        await fs_extra_1.default.writeJson(path_1.default.resolve(directory, 'package.json'), packageJSON, { spaces: 2 });
    }
    async updateFileByLine(inputPath, lineHandler, outputPath) {
        const fileContents = (await fs_extra_1.default.readFile(inputPath, 'utf8')).split('\n').map(lineHandler).join('\n');
        await fs_extra_1.default.writeFile(outputPath || inputPath, fileContents);
        if (outputPath !== undefined) {
            await fs_extra_1.default.remove(inputPath);
        }
    }
}
exports.BaseTemplate = BaseTemplate;
exports.default = new BaseTemplate();
//# sourceMappingURL=data:application/json;base64, 