|
| 1 | +import type { DecodeOptions, EncodeOptions } from '../../src' |
| 2 | +import * as fsp from 'node:fs/promises' |
| 3 | +import * as path from 'node:path' |
| 4 | +import process from 'node:process' |
| 5 | +import { defineCommand, runMain } from 'citty' |
| 6 | +import { consola } from 'consola' |
| 7 | +import { version } from '../../package.json' with { type: 'json' } |
| 8 | +import { decode, DELIMITERS, encode } from '../../src' |
| 9 | + |
| 10 | +const main = defineCommand({ |
| 11 | + meta: { |
| 12 | + name: 'toon', |
| 13 | + description: 'TOON CLI — Convert between JSON and TOON formats', |
| 14 | + version, |
| 15 | + }, |
| 16 | + args: { |
| 17 | + input: { |
| 18 | + type: 'positional', |
| 19 | + description: 'Input file path', |
| 20 | + required: true, |
| 21 | + }, |
| 22 | + output: { |
| 23 | + type: 'string', |
| 24 | + description: 'Output file path', |
| 25 | + alias: 'o', |
| 26 | + }, |
| 27 | + encode: { |
| 28 | + type: 'boolean', |
| 29 | + description: 'Encode JSON to TOON (auto-detected by default)', |
| 30 | + alias: 'e', |
| 31 | + }, |
| 32 | + decode: { |
| 33 | + type: 'boolean', |
| 34 | + description: 'Decode TOON to JSON (auto-detected by default)', |
| 35 | + alias: 'd', |
| 36 | + }, |
| 37 | + delimiter: { |
| 38 | + type: 'string', |
| 39 | + description: 'Delimiter for arrays: comma (,), tab (\\t), or pipe (|)', |
| 40 | + default: ',', |
| 41 | + }, |
| 42 | + indent: { |
| 43 | + type: 'string', |
| 44 | + description: 'Indentation size', |
| 45 | + default: '2', |
| 46 | + }, |
| 47 | + lengthMarker: { |
| 48 | + type: 'boolean', |
| 49 | + description: 'Use length marker (#) for arrays', |
| 50 | + default: false, |
| 51 | + }, |
| 52 | + strict: { |
| 53 | + type: 'boolean', |
| 54 | + description: 'Enable strict mode for decoding', |
| 55 | + default: true, |
| 56 | + }, |
| 57 | + }, |
| 58 | + async run({ args }) { |
| 59 | + const input = args.input || args._[0] |
| 60 | + if (!input) { |
| 61 | + throw new Error('Input file path is required') |
| 62 | + } |
| 63 | + |
| 64 | + const inputPath = path.resolve(input) |
| 65 | + const outputPath = args.output ? path.resolve(args.output) : undefined |
| 66 | + |
| 67 | + // Parse and validate indent |
| 68 | + const indent = Number.parseInt(args.indent || '2', 10) |
| 69 | + if (Number.isNaN(indent) || indent < 0) { |
| 70 | + throw new Error(`Invalid indent value: ${args.indent}`) |
| 71 | + } |
| 72 | + |
| 73 | + // Validate delimiter |
| 74 | + const delimiter = args.delimiter || ',' |
| 75 | + if (!Object.values(DELIMITERS).includes(delimiter as any)) { |
| 76 | + throw new Error(`Invalid delimiter "${delimiter}". Valid delimiters are: comma (,), tab (\\t), pipe (|)`) |
| 77 | + } |
| 78 | + |
| 79 | + const mode = detectMode(inputPath, args.encode, args.decode) |
| 80 | + |
| 81 | + try { |
| 82 | + if (mode === 'encode') { |
| 83 | + await encodeToToon({ |
| 84 | + input: inputPath, |
| 85 | + output: outputPath, |
| 86 | + delimiter: delimiter as ',' | '\t' | '|', |
| 87 | + indent, |
| 88 | + lengthMarker: args.lengthMarker === true ? '#' : false, |
| 89 | + }) |
| 90 | + } |
| 91 | + else { |
| 92 | + await decodeToJson({ |
| 93 | + input: inputPath, |
| 94 | + output: outputPath, |
| 95 | + indent, |
| 96 | + strict: args.strict !== false, |
| 97 | + }) |
| 98 | + } |
| 99 | + } |
| 100 | + catch (error) { |
| 101 | + consola.error(error) |
| 102 | + process.exit(1) |
| 103 | + } |
| 104 | + }, |
| 105 | +}) |
| 106 | + |
| 107 | +function detectMode( |
| 108 | + inputFile: string, |
| 109 | + encodeFlag?: boolean, |
| 110 | + decodeFlag?: boolean, |
| 111 | +): 'encode' | 'decode' { |
| 112 | + // Explicit flags take precedence |
| 113 | + if (encodeFlag) |
| 114 | + return 'encode' |
| 115 | + if (decodeFlag) |
| 116 | + return 'decode' |
| 117 | + |
| 118 | + // Auto-detect based on file extension |
| 119 | + if (inputFile.endsWith('.json')) |
| 120 | + return 'encode' |
| 121 | + if (inputFile.endsWith('.toon')) |
| 122 | + return 'decode' |
| 123 | + |
| 124 | + // Default to encode |
| 125 | + return 'encode' |
| 126 | +} |
| 127 | + |
| 128 | +async function encodeToToon(config: { |
| 129 | + input: string |
| 130 | + output?: string |
| 131 | + delimiter: ',' | '\t' | '|' |
| 132 | + indent: number |
| 133 | + lengthMarker: '#' | false |
| 134 | +}) { |
| 135 | + const jsonContent = await fsp.readFile(config.input, 'utf-8') |
| 136 | + |
| 137 | + let data: unknown |
| 138 | + try { |
| 139 | + data = JSON.parse(jsonContent) |
| 140 | + } |
| 141 | + catch (error) { |
| 142 | + throw new Error(`Failed to parse JSON: ${error instanceof Error ? error.message : String(error)}`) |
| 143 | + } |
| 144 | + |
| 145 | + const encodeOptions: EncodeOptions = { |
| 146 | + delimiter: config.delimiter, |
| 147 | + indent: config.indent, |
| 148 | + lengthMarker: config.lengthMarker, |
| 149 | + } |
| 150 | + |
| 151 | + const toonOutput = encode(data, encodeOptions) |
| 152 | + |
| 153 | + if (config.output) { |
| 154 | + await fsp.writeFile(config.output, toonOutput, 'utf-8') |
| 155 | + const relativeInputPath = path.relative(process.cwd(), config.input) |
| 156 | + const relativeOutputPath = path.relative(process.cwd(), config.output) |
| 157 | + consola.success(`Encoded \`${relativeInputPath}\` → \`${relativeOutputPath}\``) |
| 158 | + } |
| 159 | + else { |
| 160 | + console.log(toonOutput) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +async function decodeToJson(config: { |
| 165 | + input: string |
| 166 | + output?: string |
| 167 | + indent: number |
| 168 | + strict: boolean |
| 169 | +}) { |
| 170 | + const toonContent = await fsp.readFile(config.input, 'utf-8') |
| 171 | + |
| 172 | + let data: unknown |
| 173 | + try { |
| 174 | + const decodeOptions: DecodeOptions = { |
| 175 | + indent: config.indent, |
| 176 | + strict: config.strict, |
| 177 | + } |
| 178 | + data = decode(toonContent, decodeOptions) |
| 179 | + } |
| 180 | + catch (error) { |
| 181 | + throw new Error(`Failed to decode TOON: ${error instanceof Error ? error.message : String(error)}`) |
| 182 | + } |
| 183 | + |
| 184 | + const jsonOutput = JSON.stringify(data, undefined, config.indent) |
| 185 | + |
| 186 | + if (config.output) { |
| 187 | + await fsp.writeFile(config.output, jsonOutput, 'utf-8') |
| 188 | + const relativeInputPath = path.relative(process.cwd(), config.input) |
| 189 | + const relativeOutputPath = path.relative(process.cwd(), config.output) |
| 190 | + consola.success(`Decoded \`${relativeInputPath}\` → \`${relativeOutputPath}\``) |
| 191 | + } |
| 192 | + else { |
| 193 | + console.log(jsonOutput) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +runMain(main) |
0 commit comments