"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePackageJson = exports.getClientPackage = exports.CLIVersion = void 0;
const utils_1 = require("@syftdata/common/lib/utils");
const fs = require("fs");
const path = require("path");
exports.CLIVersion = require('../../package.json').version;
function getPackagePath(pkg) {
    try {
        const pkgPath = require.resolve(`${pkg}/package.json`);
        if (pkgPath != null) {
            return path.dirname(pkgPath);
        }
    }
    catch (e) { }
    return null;
}
async function getClientPackage() {
    const json = await new Promise((resolve, reject) => {
        const dir = getPackagePath('@syftdata/client');
        if (dir === null) {
            (0, utils_1.logFatal)(':warning: @syftdata/client package is not found.');
            reject(new Error('no @syftdata/client package'));
            return;
        }
        try {
            const filepath = path.join(dir, 'package.json');
            const json = JSON.parse(fs.readFileSync(filepath, 'utf8'));
            resolve({
                dir,
                path: filepath,
                json
            });
        }
        catch (e) {
            (0, utils_1.logUnknownError)(':star: Failed to read @syftdata/client package.json.', e);
            reject(e);
        }
    });
    return json;
}
exports.getClientPackage = getClientPackage;
function updatePackageJson(json) {
    try {
        fs.writeFileSync(json.path, JSON.stringify(json.json, null, 2), 'utf8');
    }
    catch (e) {
        (0, utils_1.logError)(':star: Failed to update package.json.');
    }
}
exports.updatePackageJson = updatePackageJson;
