"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path = require("path");
const fs = require("fs");
const core_1 = require("@oclif/core");
const codiac_ops_common_1 = require("@codiac.io/codiac-ops-common");
const command_base_tenantless_1 = require("../../command-base-tenantless");
const ops_1 = require("../../ops");
// import { inOrange } from "@codiac.io/cli-tools";
class DepList 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(DepList);
                    let projectRoot = ops_1.CliPathWalker.getProjectRoot(undefined, false); // this._ops.getConfigRoot();
                    // TODO: Move this into LocalOps as readPackageDependencies()
                    let pj = (yield this._ops.readNpmPackageJson(projectRoot));
                    let pkgLockFile = path.join(projectRoot, "package-lock.json");
                    let pl;
                    if (fs.existsSync(pkgLockFile)) {
                        this.logger.debug(`Found package-lock.json at ${pkgLockFile}`);
                        pl = yield codiac_ops_common_1.JsonUtils.deserializeJsonFile(pkgLockFile);
                    }
                    // Add actual installed version (from package-lock.json)
                    let deps = [];
                    if (pj && pj.dependencies) {
                        for (let name in pj.dependencies) {
                            // ASSEMBLE dep object
                            // this._ui.userMessage(`${name}: ${pj.dependencies[name]}... `)
                            let dep = {
                                name: name,
                                versionSpec: pj.dependencies[name],
                                version: pj.dependencies[name]
                            };
                            dep.sourced = this._projCfg.submodules.findIndex(x => x.packageName == name) >= 0;
                            let hasLockDeps = (pl && pl.dependencies == undefined);
                            // this.logger.trace(`hasLockDeps: ${hasLockDeps}`)
                            if (pl && pl.dependencies && pl.dependencies[name]) {
                                // this.logger.trace(`Package-lock dep:  ${pl.dependencies[name]}`)
                                let plDep = pl.dependencies[name];
                                dep.version = plDep.version;
                            }
                            deps.push(dep);
                            // this._ui.userMessage(` ... as ${JSON.stringify(dep)}`)
                        }
                    }
                    // RENDER
                    // cli.table(deps, { name: { }, version: {}, sourced: {} })
                    this._ui.userMessage(`${deps.length} dependencies for ${this._styler.hilite(this._projCfg.name)}:`);
                    let width = 15;
                    deps.forEach(d => width = (d.name.length > width) ? d.name.length : width);
                    let index = 0;
                    for (let dep of deps) {
                        let line = `  ${dep.name.padEnd(width)}  ${dep.version}${(dep.sourced) ? "  (sourced)" : ""}`;
                        if (index % 2 > 0)
                            line = this._styler.colorGood(line);
                        else
                            line = this._styler.hilite(line);
                        index++;
                        this._ui.userMessage(line);
                    }
                    // let select = false;
                    // let names: string[] = await this._ops.getNpmDependencies(projectRoot);
                    // console.log(`${names.length} dependencies for ${this._projCfg.name}:`)
                    // if (names.length > 0) {
                    //   for (let index in names) {
                    //     let name = names[index];
                    //     let sourced = "";
                    //     if (this._projCfg.submodules.find(x => x.packageName == name))
                    //     {
                    //       sourced = ", (sourced)";
                    //     }
                    //     console.log(`  ${name}${sourced}`);
                    //   }
                    // }
                    resolve();
                }
                catch (err) {
                    this.handleError(err);
                    reject(err);
                }
            }));
        });
    }
    hasPackageName(submodule, name) {
        console.log(`Matching pkg: =${submodule.packageName}= against =${name}=`);
        return submodule.packageName == name;
    }
}
exports.default = DepList;
DepList.description = 'Shows the dependencies for the project.';
DepList.flags = {
    help: core_1.Flags.help({ char: 'h' })
};
DepList.args = {};
//# sourceMappingURL=list.js.map