import * as fs from 'fs/promises' import * as path from 'path' import ora from 'ora' import { getCliClient } from '../cli-client.js' import { fileManifestForRange, packAsset, packPolicyForType, parsePackDependencies, } from '../pack.js' import { packResult } from '../output.js' import type { AssetType } from '../schemas.js' export interface PackCommandOptions { type?: AssetType cwd?: string out?: string baseUrl?: string /** npm dependencies, each `name@range` (range defaults to `*`). */ npm?: string[] /** asset dependencies, each `name@range` (range defaults to `*`). */ asset?: string[] /** skill dependencies, each `label=source` passed to `skills add`. */ skill?: string[] } export async function packCommand(zipFilter: string, opts: PackCommandOptions): Promise { const spinner = ora({ text: 'Preparing pack', isEnabled: Boolean(process.stderr.isTTY), isSilent: !process.stderr.isTTY, }).start() try { const cwd = opts.cwd ?? process.cwd() // A client is only used to omit unchanged installed dependency files (templates); it needs no // auth for public assets and is never called for non-template packs, which stay fully offline. const { client } = await getCliClient({ baseUrl: opts.baseUrl }) const packed = await packAsset(zipFilter, { cwd, dependencies: parsePackDependencies({ npm: opts.npm, asset: opts.asset, skill: opts.skill, }), policy: packPolicyForType(opts.type), fetchFileManifest: fileManifestForRange(client.asset), }) const out = opts.out ? path.resolve(cwd, opts.out) : defaultPackPath(packed.sourcePath) spinner.text = `Writing ${path.relative(cwd, out) || out}` await fs.mkdir(path.dirname(out), { recursive: true }) await fs.writeFile(out, packed.zip) spinner.stop() console.log(packResult(out, packed)) } catch (err) { spinner.stop() throw err } } function defaultPackPath(sourcePath: string): string { const ext = path.extname(sourcePath) if (!ext) return path.join(path.dirname(sourcePath), `${path.basename(sourcePath)}-src.zip`) return path.join(path.dirname(sourcePath), `${path.basename(sourcePath, ext)}.packed${ext}`) }