"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.utils = void 0;
const path = require("path");
const fs = require("fs");
const os = require("os");
const commentJson = require("comment-json");
const _ = require("lodash");
// TODO:  Promote this to a shared pkg
class utils {
    /** Returns the instantiated object or array.
     * Returns undefined for both an empty or missing source file.
     * Uses JSON.parse and does not perform any explicit casting.
     * Throws encountered exceptions.  Resolves tilde (~) path prefixes.
     * @param pathName Fully qualified path and filename of the json file.
     * @param allowComments (default: false) Flag to use a json comment parser to support comments in the json file.
     */
    static deserializeJsonFile(pathName, allowComments = false) {
        try {
            let targetPathName = this.resolveHomeDir(pathName);
            if (!path.isAbsolute(targetPathName)) {
                throw new Error(`Cannot deserialize: the pathName [${pathName}] is not a fully qualified path.`);
            }
            if (fs.existsSync(targetPathName)) {
                let json = fs.readFileSync(targetPathName, { encoding: 'utf-8' });
                if (allowComments) {
                    let obj = commentJson.parse(json); // TSC error forced <unknown>
                    return obj;
                }
                else {
                    let obj = JSON.parse(json);
                    return obj;
                }
            }
            else {
                return undefined;
            }
        }
        catch (err) {
            throw err;
        }
    }
    /** Uses JSON.stringify() to write the given object to a file.  Resolves tilde (~) path prefixes.
     * @param subject The object to serialize.
     * @param pathName Fully-qualified path and filename (eg "/users/JoeCool/.config/app-config.json").
     * @param allowComments (default: false) Flag to use a json comment parser to support comments in the json file.
     */
    static serializeJsonFile(subject, pathName, allowComments = false) {
        let targetPathName = this.resolveHomeDir(pathName);
        if (!path.isAbsolute(targetPathName)) {
            throw new Error(`Cannot serialize: the pathName [${pathName}] is not a fully qaulified path`);
        }
        if (allowComments) {
            commentJson.stringify(subject, undefined, '  ');
        }
        else {
            fs.writeFileSync(targetPathName, JSON.stringify(subject, undefined, '  '));
        }
    }
    /** Replaces the leading tilde (~) in the given file path
     * with the absolute path to the os.homedir (eg: "/Users/JoeCool").
     * NOTE:  Relative paths will slide through this function untouched:
     * this function does not necessarily return an absolute path! */
    static resolveHomeDir(pathName) {
        if (pathName.startsWith("~")) {
            return path.join(os.homedir(), pathName.slice(2));
        }
        else {
            return pathName;
        }
    }
    /**  */
    static smashMergeConfg(defaults, configsInPriorityOrder) {
        if (!configsInPriorityOrder || configsInPriorityOrder.length == 0)
            return defaults;
        else {
            let ultimate = _.assignIn(defaults, ...configsInPriorityOrder.reverse());
            return ultimate;
        }
    }
}
exports.utils = utils;
//# sourceMappingURL=utils.js.map