diff --git a/package.json b/package.json index 5de0fb6..3f4e713 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "zeed", "type": "module", - "version": "0.20.2", + "version": "0.20.3", "description": "🌱 Simple foundation library", "author": { "name": "Dirk Holtwick", @@ -65,16 +65,16 @@ "watch": "nr build -- --watch src" }, "devDependencies": { - "@antfu/eslint-config": "^2.13.0", + "@antfu/eslint-config": "^2.16.1", "@antfu/ni": "^0.21.12", - "@types/node": "^20.12.6", - "@vitest/coverage-v8": "^1.4.0", + "@types/node": "^20.12.8", + "@vitest/coverage-v8": "^1.5.3", "esbuild": "^0.20.2", - "eslint": "^9.0.0", + "eslint": "^9.1.1", "tsup": "^8.0.2", "typedoc": "^0.25.13", - "typescript": "^5.4.4", - "vite": "^5.2.8", - "vitest": "^1.4.0" + "typescript": "^5.4.5", + "vite": "^5.2.10", + "vitest": "^1.5.3" } } diff --git a/src/common/data/json.spec.ts b/src/common/data/json.spec.ts index 02e9b17..c943125 100644 --- a/src/common/data/json.spec.ts +++ b/src/common/data/json.spec.ts @@ -1,4 +1,4 @@ -import jsonParse, { jsonStringifySafe, jsonStringifySorted } from './json' +import { jsonParse, jsonStringifySafe, jsonStringifySorted } from './json' describe('convert', () => { it('should jsonStringify', () => { diff --git a/src/common/data/json.ts b/src/common/data/json.ts index a5167c6..465b197 100644 --- a/src/common/data/json.ts +++ b/src/common/data/json.ts @@ -96,7 +96,8 @@ function jsonParseTransform(key: string, value: any): any { return value } -export default function jsonParse(val: string): any { +/** @deprecated a safe parser? */ +export function jsonParse(val: string): any { if (typeof val !== 'string') return val diff --git a/src/node/fs.ts b/src/node/fs.ts index 7d0edd5..5c8c569 100644 --- a/src/node/fs.ts +++ b/src/node/fs.ts @@ -1,7 +1,7 @@ import { mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises' import { dirname, join as joinPath, normalize } from 'node:path' import process from 'node:process' -import { isUint8Array } from '../common' +import { isUint8Array, jsonStringifySorted, toUint8Array } from '../common' /** Try to use `~` for HOME folder if possible */ export function toHumanReadableFilePath(path: string) { @@ -57,12 +57,36 @@ export async function readText(...parts: string[]): Promise return await readFile(path, 'utf-8') } +export async function readJson(...parts: string[]): Promise { + const content = await readText(...parts) + if (content != null) { + try { + return JSON.parse(content) + } + catch (err) {} + } +} + +export async function readBin(...parts: string[]): Promise { + const path = joinPath(...parts) + if (await exists(path)) + return toUint8Array(await readFile(path)) +} + +/** @deprecated use readJson or readBin */ +export async function readData(...parts: string[]): Promise { + const path = joinPath(...parts) + if (await exists(path)) + return await readFile(path) +} + export async function writeText(path: string, content: string, createFolders = false): Promise { if (createFolders) await ensureFolderForFile(path) await writeFile(path, content, 'utf-8') } +/** @deprecated use writeBin or writeJson */ export async function writeData(path: string, content: object | Uint8Array, createFolders = false): Promise { if (createFolders) await ensureFolderForFile(path) @@ -70,4 +94,19 @@ export async function writeData(path: string, content: object | Uint8Array, crea await writeFile(path, data) } -// todo: writeBinary, readBinary +export async function writeBin(path: string, content: Uint8Array, info: { + createFolders?: boolean +} = {}): Promise { + const { createFolders = false } = info + if (createFolders) + await ensureFolderForFile(path) + await writeFile(path, content) +} + +export async function writeJson(path: string, content: T, info: { + createFolders?: boolean + pretty?: boolean +} = {}): Promise { + const { createFolders = false, pretty = false } = info + await writeText(path, jsonStringifySorted(content, pretty ? 2 : undefined), createFolders) +}