#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
const type_1 = require("@deepkit/type");
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const promises_1 = require("fs/promises");
const tiny_glob_1 = tslib_1.__importDefault(require("tiny-glob"));
const packageJsonPath = `package.json`;
if (!(0, fs_1.existsSync)(packageJsonPath)) {
    console.log('No package.json found in current directory.');
    process.exit(1);
}
const latestDKSFInfoStr = (0, child_process_1.execSync)('npm info @zyno-io/dk-server-foundation@latest --json').toString();
const latestDKSFInfo = JSON.parse(latestDKSFInfoStr);
(type_1.assert.Ω = [['latest', 'dist-tags', 'dependencies', 'PP&4!M4"P&&LM4#M']], (0, type_1.assert)(latestDKSFInfo));
const latestVersion = latestDKSFInfo['dist-tags'].latest;
applyUpdates()
    .then(() => process.exit(0))
    .catch(__assignType(err => {
    console.error(err);
    process.exit(1);
}, ['err', '', 'P"2!"/"']));
////
async function applyUpdates() {
    const rootPackageJson = await processPackageJson(packageJsonPath);
    if (rootPackageJson.workspaces) {
        for (const workspace of rootPackageJson.workspaces) {
            const matches = await (0, tiny_glob_1.default)(workspace);
            for (const match of matches) {
                const matchState = await (0, promises_1.stat)(match);
                if (matchState.isDirectory()) {
                    const packageJsonPath = `${match}/package.json`;
                    if ((0, fs_1.existsSync)(packageJsonPath)) {
                        await processPackageJson(packageJsonPath);
                    }
                }
            }
        }
    }
}
applyUpdates.__type = ['applyUpdates', 'P"/!'];
async function processPackageJson(path) {
    const packageJsonStr = await (0, promises_1.readFile)(path, 'utf8');
    const packageJson = JSON.parse(packageJsonStr);
    (type_1.assert.Ω = [['workspaces', 'dependencies', 'P&F4!8P&&LM4"8M']], (0, type_1.assert)(packageJson));
    if (packageJson.dependencies) {
        const currentVersion = packageJson.dependencies['@zyno-io/dk-server-foundation'];
        if (!currentVersion) {
            console.log(`${path}: No @zyno-io/dk-server-foundation dependency found in package.json.`);
            return packageJson;
        }
        if (currentVersion === latestVersion) {
            console.log(`${path}: DKSF already up to date.`);
        }
        else if (currentVersion === '*') {
            console.log(`${path}: DKSF set to '*'`);
        }
        else {
            console.log(`${path}: Updating @zyno-io/dk-server-foundation from ${currentVersion} to ${latestVersion}.`);
            packageJson.dependencies['@zyno-io/dk-server-foundation'] = latestVersion;
        }
        for (const dependency in latestDKSFInfo.dependencies) {
            if (dependency in packageJson.dependencies) {
                if (packageJson.dependencies[dependency] === '*') {
                    console.log(`${path}: ${dependency} is set to '*'.`);
                    continue;
                }
                if (packageJson.dependencies[dependency] !== latestDKSFInfo.dependencies[dependency]) {
                    console.log(`${path}: Updating ${dependency} from ${packageJson.dependencies[dependency]} to ${latestDKSFInfo.dependencies[dependency]}.`);
                    packageJson.dependencies[dependency] = latestDKSFInfo.dependencies[dependency];
                }
            }
        }
        const newPackageJsonStr = JSON.stringify(packageJson, undefined, 4);
        await (0, promises_1.writeFile)(path, `${newPackageJsonStr}\n`);
    }
    return packageJson;
}
processPackageJson.__type = ['path', 'processPackageJson', 'P&2!"/"'];
//# sourceMappingURL=dksf-update.js.map