Skip to content

Commit

Permalink
keep json encoding inside load and save asset functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fain182 committed Aug 1, 2024
1 parent 85e2f19 commit acd8322
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
7 changes: 3 additions & 4 deletions src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ export interface AssetSource {
export async function getAsset(filePath: string): Promise<Asset | undefined> {
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;
}

Expand Down Expand Up @@ -107,7 +106,7 @@ export async function createAsset(
}
}

videoConfig.saveAsset(assetConfigPath, JSON.stringify(newAssetDetails));
videoConfig.saveAsset(assetConfigPath, newAssetDetails);

return newAssetDetails;
}
Expand All @@ -130,7 +129,7 @@ export async function updateAsset(

newAssetDetails = transformAsset(transformers, newAssetDetails);

await saveAsset(assetConfigPath, JSON.stringify(newAssetDetails));
await saveAsset(assetConfigPath, newAssetDetails);

return newAssetDetails;
}
Expand Down
9 changes: 5 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<string>;
saveAsset: (path: string, jsonAsset: string) => Promise<void>;
loadAsset: (path: string) => Promise<Asset>;
saveAsset: (path: string, asset: Asset) => Promise<void>;
};

export type ProviderConfig = {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit acd8322

Please sign in to comment.