var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import fs from 'fs';
import path from 'path';
import _ from 'lodash';
;
export class Packages {
    constructor(deep) {
        this.deep = deep;
    }
    select() {
        return __awaiter(this, void 0, void 0, function* () {
            const deep = this.deep;
            return yield deep.select({
                type_id: deep.idLocal('@deep-foundation/core', 'Package'),
                string: { value: { _neq: 'deep' } },
            });
        });
    }
    export() {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const deep = this.deep;
            const packager = deep.Packager();
            const { data: packages } = yield this.select();
            console.log('export packages', packages.map(p => p.id).join(', '));
            const results = {};
            for (let i = 0; i < packages.length; i++) {
                const p = packages[i];
                console.log('export package', `${p.id} ${(_a = p === null || p === void 0 ? void 0 : p.value) === null || _a === void 0 ? void 0 : _a.value}`);
                const pckg = yield packager.export({ packageLinkId: p.id });
                console.log('exported package', `${pckg.package.name} ${pckg.package.version}`);
                results[_.camelCase(`${pckg.package.name}@${pckg.package.version}`)] = pckg;
            }
            return results;
        });
    }
    write(address, pckgs) {
        var _a, _b;
        return __awaiter(this, void 0, void 0, function* () {
            const packages = Object.values(pckgs);
            const deep = this.deep;
            console.log('write packages', Object.keys(pckgs).join(','));
            for (let i = 0; i < packages.length; i++) {
                const p = packages[i];
                fs.writeFileSync(path.join(address, _.camelCase(`${(_a = p === null || p === void 0 ? void 0 : p.package) === null || _a === void 0 ? void 0 : _a.name}@${p.package.version}.json`)), JSON.stringify(p, null, 2), { encoding: 'utf-8' });
                console.log('writeed package', `${path.join(address, _.camelCase(`${(_b = p === null || p === void 0 ? void 0 : p.package) === null || _b === void 0 ? void 0 : _b.name}@${p.package.version}`))}`);
            }
        });
    }
    read(address) {
        return __awaiter(this, void 0, void 0, function* () {
            const deep = this.deep;
            const packager = deep.Packager();
            const pckgs = fs.readdirSync(address);
            console.log(`read packages from ${address} ${pckgs.join(', ')}`);
            const results = {};
            for (let i = 0; i < pckgs.length; i++) {
                if (pckgs[i].slice(-5) === '.json' && pckgs[i] != 'deep.config.json') {
                    console.log('read package', path.join(address, pckgs[i]));
                    const json = fs.readFileSync(path.join(address, pckgs[i]), { encoding: 'utf-8' });
                    try {
                        const pckg = JSON.parse(json);
                        results[_.camelCase(`${pckg.package.name}@${pckg.package.version}`)] = pckg;
                    }
                    catch (e) {
                        console.log('error read package', e);
                    }
                }
            }
            return results;
        });
    }
    import(pckgs) {
        return __awaiter(this, void 0, void 0, function* () {
            const deep = this.deep;
            const packager = deep.Packager();
            const packages = Object.values(pckgs);
            console.log(`import packages from ${Object.keys(pckgs).join(', ')}`);
            const results = {};
            for (let i = 0; i < packages.length; i++) {
                const p = packages[i];
                console.log(`import package ${_.camelCase(`${p.package.name}@${p.package.version}`)}`);
                results[_.camelCase(`${p.package.name}@${p.package.version}`)] = yield packager.import(p);
            }
            return results;
        });
    }
}
;
//# sourceMappingURL=packages.js.map