"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const lib_1 = require("../../lib");
const angular_utility_1 = require("../../lib/angular-utility");
const input_1 = require("../../lib/input");
const utility_1 = require("../../lib/utility");
const shell_1 = require("../../lib/shell");
const NEW_PACKAGE_SCRIPTS = {
    "deploy:playstore": "bundle exec fastlane android deploy",
    "deploy:playstore:beta": "bundle exec fastlane android beta",
    "deploy:playstore:debug": "bundle exec fastlane android debug",
    "android:release": "ionic cordova build android --prod --release -- -- --keystore=$CORDOVA_ANDROID_KEYSTORE_PATH --storePassword=$CORDOVA_ANDROID_KEYSTORE_PASSWORD --alias=$CORDOVA_ANDROID_KEYSTORE_ALIAS --password=$CORDOVA_ANDROID_KEY_PASSWORD"
};
/**
 * https://github.com/bamlab/fastlane-plugin-cordova
 * https://ionic.zone/fastlane/build-your-project-with-cordova-plugin
 * @param _options
 */
function default_1(_options) {
    let fastlaneConfig;
    return schematics_1.chain([
        angular_utility_1.toPromiseRule(async (tree, _context) => {
            if (!shell_1.shell.hasCommand('gem -h')) {
                throw new Error(`Please install ruby to integrate with fastlane. See this link: https://www.ruby-lang.org/fr/documentation/installation/`);
            }
            // if (!shell.hasCommand('fastlane -h')) {
            //     if (!await input.confirm('Fastlane tool is not installed. Do you want to install fastlane now ?')) {
            //         throw new Error(`fastlane installation is required to continue`)
            //     }
            //     await shell.execSpawn(`gem install fastlane -NV`);
            // }
        }),
        angular_utility_1.toPromiseRule(async (tree, _context) => {
            let project = lib_1.loadProject(tree, _context);
            let appleId;
            let teamId;
            fastlaneConfig = {
                type: 'FastlaneIntegration'
            };
            if (await input_1.input.confirm('Do you want to publish on Android Play Store ? ')) {
                fastlaneConfig.android = {
                    enabled: true
                };
            }
            if (await input_1.input.confirm('Do you want to add iOS App Store ? ')) {
                appleId = await input_1.input.string('apple identifier');
                teamId = await input_1.input.string('Team identifier');
                fastlaneConfig.iOS = {
                    enabled: true,
                    appleId,
                    teamId
                };
            }
            fastlaneConfig = project.appConfigBuilder.addFastlaneIntegration(fastlaneConfig);
            project.save();
        }),
        angular_utility_1.toPromiseRule(async (tree, _context) => {
            await shell_1.shell.execSpawn('fastlane init');
            await shell_1.shell.execSpawn('fastlane add_plugin cordova');
        }),
        (tree, _context) => {
            let project = lib_1.loadProject(tree, _context);
            let sourceTemplates = schematics_1.url('files');
            let appConfig = project.appConfigSchema.app;
            // console.info('fastlaneConfig', fastlaneConfig);
            return angular_utility_1.applyWithOverwriteRule(sourceTemplates, [
                schematics_1.template(Object.assign(Object.assign({}, project.toTemplate()), { shortDescription: appConfig.description, fullDescription: appConfig.description, fastlaneConfig })),
            ], {
                overwrite: _options.overwrite,
                format: true
            });
        },
        (tree, _context) => {
            utility_1.updatePackageFile(tree, (config) => {
                if (!config.scripts) {
                    config.scripts = {};
                }
                for (let scriptName in NEW_PACKAGE_SCRIPTS) {
                    if (!(scriptName in config.scripts)) {
                        config.scripts[scriptName] = NEW_PACKAGE_SCRIPTS[scriptName];
                    }
                }
                return config;
            });
        }
    ]);
}
exports.default = default_1;
//# sourceMappingURL=index.js.map