Skip to content

Commit

Permalink
fix cli decoding/encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
beeequeue committed Feb 1, 2025
1 parent b38fb55 commit 6eb7e1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import colors from "tinyrainbow"

import { decodeGmd } from "../decode.ts"
import { encodeGmd } from "../encode.ts"
import type { GMD } from "../types.ts"

import { checkIfDir, findCommonPathStart, getOutputPath } from "./path-utils.ts"
import { logError } from "./utils.ts"
import { fromJson, logError, toJson } from "./utils.ts"

type Options = {
help?: boolean
Expand Down Expand Up @@ -105,8 +104,8 @@ for (const inputFilePath of uniqueInputFiles) {
const data = fs.readFileSync(inputFilePath)
const output =
command === "decode"
? JSON.stringify(decodeGmd(data), null, 2)
: encodeGmd(JSON.parse(data.toString("utf8")) as GMD)
? toJson(decodeGmd(data))
: encodeGmd(fromJson(data.toString("utf8")))

const outputPath = getOutputPath(inputFilePath, commonDirIndex, args.out)

Expand Down
28 changes: 28 additions & 0 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Buffer } from "node:buffer"

import colors from "tinyrainbow"

import type { GMD } from "../types.js"

const indent = (str: string, spaces: number): string => {
const lines = str.split("\n")
if (lines.length <= 1) return str
Expand All @@ -21,3 +25,27 @@ export const logError = <Exit extends boolean>(

return undefined as never
}

export const toJson = (input: GMD): string => {
const data = {
filename: input.filename,
version: input.version,
language: input.language,
unknownData: input.unknownData.toString("base64url"),
entries: input.entries,
} satisfies Omit<GMD, "unknownData"> & { unknownData: string }

return JSON.stringify(data, null, 2)
}

export const fromJson = (input: string): GMD => {
const data = JSON.parse(input) as Omit<GMD, "unknownData"> & { unknownData: string }

return {
filename: data.filename,
version: data.version,
language: data.language,
unknownData: Buffer.from(data.unknownData, "base64url"),
entries: data.entries,
}
}

0 comments on commit 6eb7e1f

Please sign in to comment.