{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["// oxlint-disable no-console\nimport { randomBytes } from 'node:crypto'\nimport { createServer } from 'node:http'\nimport path from 'node:path'\nimport process from 'node:process'\n\nimport { cac } from 'cac'\nimport next from 'next'\nimport open from 'open'\nimport c from 'picocolors'\n\ninterface Options {\n  open?: boolean\n  port: number\n  filter?: string\n}\n\nfunction run() {\n  const source = path.dirname(import.meta.resolve('@yeger/turbo-graph-ui').replace('file://', ''))\n  const hostname = 'localhost'\n  const defaultPort = 29312\n\n  const cli = cac('turbo-graph')\n    .option('-o, --open', 'Open the visualizer in the default browser.')\n    .option('-p, --port <port>', 'Port of the visualizer.', {\n      default: defaultPort,\n    })\n    .option('-f, --filter <filter>', 'Apply a filter to the visualization.')\n    .help()\n\n  cli.command('[...tasks]', 'Visualize the specified tasks').action(startServer)\n\n  cli.parse()\n\n  function startServer(tasks: string[], options: Options) {\n    const authCode = randomBytes(18).toString('base64url')\n    // Must be set before the Next.js server starts, as its route handlers read it at request time\n    process.env.TURBO_GRAPH_AUTH_CODE = authCode\n\n    const app = next({ dev: false, dir: source, hostname, port: options.port })\n    const handle = app.getRequestHandler()\n\n    void app.prepare().then(() => {\n      createServer(handle)\n        .once('error', (err) => {\n          console.error(err)\n          process.exit(1)\n        })\n        .listen(options.port, hostname, () => {\n          const url = `http://${hostname}:${options.port}`\n          const params = new URLSearchParams({ code: authCode })\n          console.log(`${c.green('turbo-graph:')} Listening on ${c.cyan(url)}`)\n          console.log(`${c.green('turbo-graph:')} Auth code: ${c.bold(authCode)}`)\n          console.log(`${c.green('turbo-graph:')} Open ${c.cyan(`${url}/?${params}`)}`)\n          if (options.open) {\n            for (const task of tasks) {\n              params.append('task', task)\n            }\n            if (options.filter) {\n              params.set('filter', options.filter)\n            }\n            void open(`${url}/?${params}`)\n          } else if (tasks.length > 0 || options.filter) {\n            console.log(\n              `${c.yellow('turbo-graph:')} Use ${c.cyan(\n                '--open',\n              )} to open the visualizer in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