'use strict'; const core = require('@animespace/core'); const fs = require('fs-extra'); const path = require('pathe'); const node_child_process = require('node:child_process'); const openEditor = require('open-editor'); const breadc = require('breadc'); const color = require('@breadc/color'); const os = require('node:os'); const local = require('@animespace/local'); const bangumi = require('@animespace/bangumi'); const animegarden = require('@animespace/animegarden'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } const fs__default = /*#__PURE__*/_interopDefaultCompat(fs); const path__default = /*#__PURE__*/_interopDefaultCompat(path); const openEditor__default = /*#__PURE__*/_interopDefaultCompat(openEditor); const os__default = /*#__PURE__*/_interopDefaultCompat(os); const version = "0.1.0-beta.20"; const description = "Create your own Anime Space"; async function loop(fn, duration) { let timestamp; core.onDeath(() => { clearTimeout(timestamp); }); const time = parseDuration(duration); return new Promise(() => { const wrapper = async () => { await fn(); timestamp = setTimeout(wrapper, time); }; wrapper(); }); } function parseDuration(text) { const s = /^(\d+)s$/.exec(text); if (s) { return +s[1] * 1e3; } const m = /^(\d+)m$/.exec(text); if (m) { return +m[1] * 60 * 1e3; } const h = /^(\d+)h$/.exec(text); if (h) { return +h[1] * 60 * 60 * 1e3; } return 10 * 60 * 1e3; } const pluginLoader = { async animegarden(entry) { return animegarden.AnimeGarden(entry); }, async bangumi(entry) { return bangumi.Bangumi(entry); }, async local(entry) { return local.Local(entry); } }; async function makeSystem(_root) { const root = _root ?? inferRoot(); const space = await core.loadSpace(root, pluginLoader); const system = core.createSystem(space); return system; } function inferRoot() { try { const envRoot = process.env.ANIMESPACE_ROOT; if (envRoot) { return path__default.resolve(envRoot); } } finally { return path__default.resolve(os__default.homedir(), ".animespace"); } } async function makeCliApp(system) { const app = breadc.breadc("anime", { version, description, plugins: [ { onPreCommand(parsed) { if (parsed.options?.proxy) { core.proxy.enable = true; } } } ] }).option("--proxy", { description: "Enable HTTP/HTTPS proxy" }); registerApp(system, app); for (const plugin of system.space.plugins) { await plugin.command?.(system, app); } return app; } function registerApp(system, app) { const isTTY = !!process?.stdout?.isTTY; app.command("space", "Display the space directory").option("--open", "Open space in your editor").action(async (options) => { const root = system.space.root; const cmds = options["--"]; if (cmds.length > 0) { if (isTTY) { system.printSpace(); } node_child_process.execSync(cmds.join(" "), { cwd: root.path, stdio: "inherit" }); } else if (options.open) { try { openEditor__default([root.path]); } catch (error) { console.log(root); } } else { console.log(root); } return root; }); app.command("run [...args]", "Run command in the space directory").action(async (command, args) => { const pkgJson = await fs__default.readJSON(system.space.root.resolve("package.json").path).catch(() => void 0); const env = { ...process.env }; env.PATH = [ ...(process.env.PATH ?? "").split(path__default.delimiter), system.space.root.resolve("node_modules/.bin").path ].join(path__default.delimiter); if (pkgJson.scripts && command in pkgJson.scripts) { const cmd = pkgJson.scripts[command]; if (isTTY) { system.printSpace(); } node_child_process.execSync(cmd + " " + args.join(" "), { cwd: system.space.root.path, stdio: "inherit", env }); } else { if (isTTY) { system.printSpace(); } node_child_process.execSync(command + " " + args.join(" "), { cwd: system.space.root.path, stdio: "inherit", env }); } }); app.command("watch", "Watch anime system update").option("-d, --duration