- Populate requested pages for scoped reads and all pages for document-wide operations - Export whole FIG documents by default while keeping explicit page exports partial
61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
import { writeFile } from 'node:fs/promises'
|
|
import { basename, extname, resolve } from 'node:path'
|
|
|
|
import { defineCommand } from 'citty'
|
|
|
|
import { BUILTIN_IO_FORMATS, IORegistry } from '@open-pencil/core/io'
|
|
|
|
import { requireFile } from '#cli/app-client'
|
|
import { ok, printError } from '#cli/format'
|
|
import { loadDocument, populateWholeDocument } from '#cli/headless'
|
|
|
|
const io = new IORegistry(BUILTIN_IO_FORMATS)
|
|
|
|
function writableFormatIds(): string[] {
|
|
return io.listWritableFormats().map((format) => format.id)
|
|
}
|
|
|
|
function defaultOutput(file: string, format: string): string {
|
|
const base = basename(file, extname(file))
|
|
return resolve(`${base}.${format.toLowerCase()}`)
|
|
}
|
|
|
|
export default defineCommand({
|
|
meta: { description: 'Convert a document to another writable format' },
|
|
args: {
|
|
file: {
|
|
type: 'positional',
|
|
description: 'Input document file path',
|
|
required: true
|
|
},
|
|
output: {
|
|
type: 'string',
|
|
alias: 'o',
|
|
description: 'Output file path (default: <name>.<format>)',
|
|
required: false
|
|
},
|
|
format: {
|
|
type: 'string',
|
|
alias: 'f',
|
|
description: 'Output format: fig (default: fig)',
|
|
default: 'fig'
|
|
}
|
|
},
|
|
async run({ args }) {
|
|
const format = args.format.toLowerCase()
|
|
const writableFormats = writableFormatIds()
|
|
if (!writableFormats.includes(format)) {
|
|
printError(`Invalid format "${args.format}". Use ${writableFormats.join(', ')}.`)
|
|
process.exit(1)
|
|
}
|
|
|
|
const file = requireFile(args.file)
|
|
const graph = await loadDocument(file)
|
|
populateWholeDocument(graph)
|
|
const result = await io.writeDocument(format, graph)
|
|
const output = args.output ? resolve(args.output) : defaultOutput(file, format)
|
|
await writeFile(output, result.data as Uint8Array)
|
|
console.log(ok(`Converted ${file} → ${output}`))
|
|
}
|
|
})
|