diff --git a/src/constants/SsgScripts.ts b/src/constants/SsgScripts.ts index a19cb4be..7c3e2c71 100644 --- a/src/constants/SsgScripts.ts +++ b/src/constants/SsgScripts.ts @@ -1,4 +1,5 @@ export const SsgScripts = { folder: '/ssg-scripts', - astroContentCollection: 'astro-collections.mjs' + astroContentCollectionScript: 'astro.collections.mjs', + astroContentCollectionJSON: 'astro.collections.json' }; diff --git a/src/listeners/dashboard/SsgListener.ts b/src/listeners/dashboard/SsgListener.ts index 5f79408b..3460c386 100644 --- a/src/listeners/dashboard/SsgListener.ts +++ b/src/listeners/dashboard/SsgListener.ts @@ -8,6 +8,7 @@ import { Folders } from '../../commands'; import { SETTING_TAXONOMY_CONTENT_TYPES, SsgScripts } from '../../constants'; import { SettingsListener } from './SettingsListener'; import { Terminal } from '../../services'; +import { existsAsync, readFileAsync } from '../../utils'; export class SsgListener extends BaseListener { /** @@ -120,7 +121,7 @@ export class SsgListener extends BaseListener { const scriptPath = Uri.joinPath( Extension.getInstance().extensionPath, SsgScripts.folder, - SsgScripts.astroContentCollection + SsgScripts.astroContentCollectionScript ); const wsFolder = Folders.getWorkspaceFolder(); @@ -130,7 +131,7 @@ export class SsgListener extends BaseListener { } const tempLocation = Uri.joinPath(wsFolder, '/.frontmatter/temp'); - const tempScriptPath = Uri.joinPath(tempLocation, SsgScripts.astroContentCollection); + const tempScriptPath = Uri.joinPath(tempLocation, SsgScripts.astroContentCollectionScript); await workspace.fs.createDirectory(tempLocation); workspace.fs.copy(scriptPath, tempScriptPath, { overwrite: true }); @@ -139,9 +140,13 @@ export class SsgListener extends BaseListener { try { const result: string = await SsgListener.executeScript(fullScript, wsFolder?.fsPath || ''); if (result) { - let collections: AstroCollection[] = JSON.parse(result); - collections = collections.filter((c) => c.type === 'content'); - SsgListener.sendRequest(command as any, requestId, collections); + const tempJsonPath = Uri.joinPath(tempLocation, SsgScripts.astroContentCollectionJSON); + if (await existsAsync(tempJsonPath.fsPath)) { + const contents = await readFileAsync(tempJsonPath.fsPath, 'utf8'); + let collections: AstroCollection[] = JSON.parse(contents); + collections = collections.filter((c) => c.type === 'content'); + SsgListener.sendRequest(command as any, requestId, collections); + } } } catch (error) { Logger.error((error as Error).message); diff --git a/ssg-scripts/astro-collections.mjs b/ssg-scripts/astro.collections.mjs similarity index 95% rename from ssg-scripts/astro-collections.mjs rename to ssg-scripts/astro.collections.mjs index 0ddc9e24..7603d2f8 100644 --- a/ssg-scripts/astro-collections.mjs +++ b/ssg-scripts/astro.collections.mjs @@ -1,3 +1,5 @@ +import { writeFileSync } from "fs"; +import { join } from "path"; import { createServer } from "vite"; import zod from "zod"; @@ -206,7 +208,9 @@ const astroContentModulePlugin = () => { const collections = Object.entries(contentModule.collections ?? {}); const processedCollections = processCollection(collections); - console.log(JSON.stringify(processedCollections)); + writeFileSync(join(process.cwd(), `./.frontmatter/temp/astro.collections.json`), JSON.stringify(processedCollections)); + + console.log("Collections generated successfully"); process.exit(0); } catch (error) { console.log(error.message);