"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const path_1 = require("path");
const FileApi_1 = require("./FileApi");
const FilePacker_1 = require("./FilePacker");
const PackageVersion_1 = require("./PackageVersion");
const fileApi = new FileApi_1.default(".");
class Packer {
    package;
    config;
    async run(args) {
        const root = args[2];
        if (root) {
            process.chdir(root);
        }
        // const files = await FileApi.instance.readDir(
        //     ".",
        //     (f) =>
        //         /^src/i.test(f.path) &&
        //         !/^node\_modules/.test(f.path) && (f.isDirectory || /\.(js|html|xaml)$/.test(f.name)), true);
        // for (const iterator of files) {
        //     console.log(iterator.path);
        // }
        this.package = JSON.parse(await fileApi.readString("package.json"));
        const config = this.config =
            (0, fs_1.existsSync)("waconfig.json")
                ? JSON.parse(await fileApi.readString("waconfig.json"))
                : {};
        if (this.package.dependencies["@web-atoms/core"]) {
            PackageVersion_1.default.isV2 = true;
        }
        this.package.pack = config.pack || [];
        const packFiles = this.package.pack;
        // search for all files with text @web-atoms-pack: true
        const list = await fileApi.readDir(".", (f) => {
            if (f.isDirectory) {
                return false;
            }
            if (".js" !== f.ext) {
                return false;
            }
            if (/\.(pack|min)$/.test(f.name)) {
                return false;
            }
            if (/node\_modules/.test(f.dir)) {
                return false;
            }
            const text = (0, fs_1.readFileSync)((0, path_1.format)(f), { encoding: "utf8" });
            const packRequire = /require\(\s*\"\@web\-atoms\/core\/dist\/Pack\"\s*\)/;
            if (packRequire.test(text)) {
                return true;
            }
            if (/\/\/\s+\@web\-atoms\-pack\:\s+true/.test(text)) {
                return true;
            }
            if (text.includes(`"@web-atoms/core/dist/Pack"`)) {
                return true;
            }
            return false;
        }, true);
        for (const iterator of list) {
            if (iterator.name.endsWith(".pack")) {
                continue;
            }
            const name = `${iterator.dir}/${iterator.name}`.replaceAll("\\", "/");
            packFiles.push(name);
        }
        for (const iterator of packFiles) {
            console.log(`Packing: ${iterator}`);
        }
        if (!packFiles) {
            return;
        }
        const tasks = packFiles.map(async (file) => {
            const packer = new FilePacker_1.default(".", file, this.package);
            await packer.pack();
            return file;
        });
        return await Promise.all(tasks);
    }
}
exports.default = Packer;
//# sourceMappingURL=Packer.js.map