"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const inquirer = require("inquirer");
const command_base_tenantless_1 = require("../../command-base-tenantless");
class DepRemove extends command_base_tenantless_1.CommandBaseTenantless {
    run() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                try {
                    this.bootstrap();
                    const { args, flags } = yield this.parse(DepRemove);
                    let projectRoot = this._ops.getConfigRoot();
                    let name = args.name;
                    let select = false;
                    let names = yield this._ops.getNpmDependencies(projectRoot);
                    if (!name || names.indexOf(name) < 0) {
                        let result = yield inquirer.prompt({ type: 'list', name: 'name', message: 'dependency to remove: (ctrl-c to cancel) ', choices: names });
                        if (!result.name) {
                            this._ui.userMessage("Operation cancelled: No work was done.");
                            this.exit(0);
                        }
                        else {
                            name = result.name;
                        }
                    }
                    // TODO:  If it's in source mode, unsource it first.
                    let subm = this._projCfg.submodules.find(x => x.packageName == name);
                    if (subm) {
                        yield this._ops.removeSubmodule(subm.folderName, flags.discard);
                    }
                    let cmd = `npm uninstall ${name}`;
                    let result = yield this.execAsync(cmd, { cwd: projectRoot });
                    if (result.stderr) {
                        if (this._ops.npmStdErrIsJustAWarning(result.stderr)) {
                            this._ui.userMessage(`Warnings found: \n${result.stderr}`);
                        }
                        else {
                            throw new Error(result.stderr); // NOTE:  this may contain just warnings - how do we diferentiate? A: we know it's npm, so each line starts with "npm WARN"
                        }
                    }
                    resolve();
                }
                catch (err) {
                    this.handleError(err);
                    reject(err);
                }
            }));
        });
    }
}
exports.default = DepRemove;
DepRemove.description = 'Removes a dependency from the project.';
DepRemove.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    discard: core_1.Flags.boolean({ char: 'd', description: 'If the dependency to remove is sourced into the project, setting this flag discards all uncommitted changes (staged and unstaged) before removing it.  Again, uncomitted changes are LOST when this flag is set.' }),
};
DepRemove.args = {
    name: core_1.Args.string({ description: 'The official unique name of the dependency package.' })
};
//# sourceMappingURL=remove.js.map