"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenerateCommand = void 0;
const schematics_executor_1 = require("@nativescript/schematics-executor");
const yok_1 = require("../common/yok");
class GenerateCommand {
    constructor($logger, $options, $errors) {
        this.$logger = $logger;
        this.$options = $options;
        this.$errors = $errors;
        this.allowedParameters = [];
    }
    execute(_rawArgs) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                yield (0, schematics_executor_1.run)(this.executionOptions);
            }
            catch (error) {
                this.$errors.fail(error.message);
            }
        });
    }
    canExecute(rawArgs) {
        return __awaiter(this, void 0, void 0, function* () {
            this.setExecutionOptions(rawArgs);
            this.validateExecutionOptions();
            return true;
        });
    }
    validateExecutionOptions() {
        if (!this.executionOptions.schematic) {
            this.$errors.failWithHelp(`The generate command requires a schematic name to be specified.`);
        }
    }
    setExecutionOptions(rawArgs) {
        const options = this.parseRawArgs(rawArgs);
        this.executionOptions = Object.assign(Object.assign({}, options), { logger: this.$logger, directory: process.cwd() });
    }
    parseRawArgs(rawArgs) {
        const collection = this.$options.collection;
        const schematic = rawArgs.shift();
        const { options, args } = parseSchematicSettings(rawArgs);
        return {
            collection,
            schematic,
            schematicOptions: options,
            schematicArgs: args,
        };
    }
}
exports.GenerateCommand = GenerateCommand;
/**
 * Converts an array of command line arguments to options for the executed schematic.
 * @param rawArgs The command line arguments. They should be in the format 'key=value' for strings or 'key' for booleans.
 */
function parseSchematicSettings(rawArgs) {
    const [optionStrings, args] = partition(rawArgs, (item) => item.includes("="));
    const options = optionStrings
        .map((o) => o.split("=")) // split to key and value pairs
        .map(([key, ...value]) => [key, value.join("=")]) // concat the value arrays if there are multiple = signs
        .reduce((obj, [key, value]) => {
        return Object.assign(Object.assign({}, obj), { [key]: value });
    }, {});
    return { options, args };
}
/**
 * Splits an array into two groups based on a predicate.
 * @param array The array to split.
 * @param predicate The condition to be used for splitting.
 */
function partition(array, predicate) {
    return array.reduce(([pass, fail], item) => {
        return predicate(item)
            ? [[...pass, item], fail]
            : [pass, [...fail, item]];
    }, [[], []]);
}
yok_1.injector.registerCommand("generate", GenerateCommand);
//# sourceMappingURL=generate.js.map