From acd832224ebf3c4245b1f293bd93a95760e36bb5 Mon Sep 17 00:00:00 2001 From: Pietro Campagnano Date: Thu, 1 Aug 2024 17:44:20 +0200 Subject: [PATCH] keep json encoding inside load and save asset functions --- src/assets.ts | 7 +++---- src/config.ts | 9 +++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/assets.ts b/src/assets.ts index b1784f2..b6846bc 100644 --- a/src/assets.ts +++ b/src/assets.ts @@ -48,8 +48,7 @@ export interface AssetSource { export async function getAsset(filePath: string): Promise { const { loadAsset } = await getVideoConfig(); const assetConfigPath = await getAssetConfigPath(filePath); - const jsonAsset = await loadAsset(assetConfigPath); - const asset = JSON.parse(jsonAsset); + const asset = await loadAsset(assetConfigPath); return asset; } @@ -107,7 +106,7 @@ export async function createAsset( } } - videoConfig.saveAsset(assetConfigPath, JSON.stringify(newAssetDetails)); + videoConfig.saveAsset(assetConfigPath, newAssetDetails); return newAssetDetails; } @@ -130,7 +129,7 @@ export async function updateAsset( newAssetDetails = transformAsset(transformers, newAssetDetails); - await saveAsset(assetConfigPath, JSON.stringify(newAssetDetails)); + await saveAsset(assetConfigPath, newAssetDetails); return newAssetDetails; } diff --git a/src/config.ts b/src/config.ts index b130f96..7342873 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,6 +5,7 @@ import { stat, readFile, writeFile, mkdir } from 'node:fs/promises'; import { pathToFileURL } from 'node:url'; import nextConfig from 'next/config.js'; import type { NextConfig } from 'next'; +import { Asset } from './assets'; // @ts-ignore const getConfig = nextConfig.default; @@ -27,8 +28,8 @@ export type VideoConfigComplete = { /* An optional function to generate the local asset path for remote sources. */ remoteSourceAssetPath?: (url: string) => string; - loadAsset: (path: string) => Promise; - saveAsset: (path: string, jsonAsset: string) => Promise; + loadAsset: (path: string) => Promise; + saveAsset: (path: string, asset: Asset) => Promise; }; export type ProviderConfig = { @@ -72,10 +73,10 @@ export const videoConfigDefault: VideoConfigComplete = { const asset = JSON.parse(file.toString()); return asset; }, - saveAsset: async (assetPath, jsonAsset) => { + saveAsset: async (assetPath, asset) => { try { await mkdir(path.dirname(assetPath), { recursive: true }); - await writeFile(assetPath, jsonAsset, { + await writeFile(assetPath, JSON.stringify(asset), { flag: 'wx', }); } catch (err: any) {