import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { CONFIG_FILE } from './constants.js';
export async function writeConfigFile(environment, targetDir, options) {
    const persistedOptions = {
        ...options,
        version: 1,
        existingAddOns: options.chosenAddOns.map((addOn) => addOn.id),
    };
    delete persistedOptions.addOns;
    delete persistedOptions.chosenAddOns;
    await environment.writeFile(resolve(targetDir, CONFIG_FILE), JSON.stringify(persistedOptions, null, 2));
}
export async function readConfigFile(targetDir) {
    try {
        const configFile = resolve(targetDir, CONFIG_FILE);
        const config = await readFile(configFile, 'utf8');
        return JSON.parse(config);
    }
    catch {
        return null;
    }
}
