import { lstat, mkdir } from 'fs/promises';
import { dirname } from 'path';
import { execa } from 'execa';
export async function generateKeyStore(keyStoreFile, { packageName }) {
    try {
        const stat = await lstat(keyStoreFile);
        if (stat.isFile()) {
            return { info: `KeyStore '${keyStoreFile}' already exists. Leaving unchanged.` };
        }
        throw new Error(`${keyStoreFile} is not a file`);
    }
    catch {
    }
    await mkdir(dirname(keyStoreFile), { recursive: true });
    const javaHome = process.env.JAVA_HOME;
    const keytoolCmd = javaHome ? `${javaHome}/bin/keytool` : 'keytool';
    try {
        const result = await execa(keytoolCmd, [
            '-genkey',
            '-noprompt',
            '-storetype',
            'PKCS12',
            '-keyalg',
            'RSA',
            '-alias',
            'selfsigned',
            '-keystore',
            keyStoreFile,
            '-storepass',
            'password',
            '-keypass',
            'password',
            '-keysize',
            '2048',
            '-validity',
            '99999',
            '-dname',
            `CN=Java Hipster, OU=Development, O=${packageName}, L=, ST=, C=`,
        ]);
        return { info: [...result.stderr.split('\n').filter(line => line), `KeyStore '${keyStoreFile}' generated successfully.`] };
    }
    catch (error) {
        return { debug: error, warning: `Failed to create a KeyStore with 'keytool': ${error.message}` };
    }
}
