Skip to content

Commit

Permalink
#703 - fix pnpm support in astro script
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Nov 9, 2023
1 parent cef607f commit 8db76ab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [#694](https://github.com/estruyf/vscode-front-matter/issues/694): Start terminal session from the folder where the `frontmatter.json` file is located
- [#696](https://github.com/estruyf/vscode-front-matter/issues/696): Close the local server terminal on restart
- [#699](https://github.com/estruyf/vscode-front-matter/issues/699): Changing border theme variable for the dashboard header
- [#703](https://github.com/estruyf/vscode-front-matter/issues/703): Fix retrieval of Astro Collections for `pnpm` projects

## [9.3.1] - 2023-10-27

Expand Down
24 changes: 23 additions & 1 deletion src/listeners/dashboard/SsgListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SETTING_TAXONOMY_CONTENT_TYPES, SsgScripts } from '../../constants';
import { SettingsListener } from './SettingsListener';
import { Terminal } from '../../services';
import { existsAsync, readFileAsync } from '../../utils';
import { join } from 'path';

export class SsgListener extends BaseListener {
/**
Expand Down Expand Up @@ -133,7 +134,28 @@ export class SsgListener extends BaseListener {
const tempLocation = Uri.joinPath(wsFolder, '/.frontmatter/temp');
const tempScriptPath = Uri.joinPath(tempLocation, SsgScripts.astroContentCollectionScript);
await workspace.fs.createDirectory(tempLocation);
workspace.fs.copy(scriptPath, tempScriptPath, { overwrite: true });

// Check if the workspace uses pnpm
if (await existsAsync(Uri.joinPath(wsFolder, 'node_modules/.pnpm').fsPath)) {
const vitePackageFiles = await workspace.findFiles(
`**/node_modules/.pnpm/vite@*/node_modules/vite/package.json`
);
if (vitePackageFiles.length > 0) {
const vitePackageFile = vitePackageFiles[0];
const vitePackage = JSON.parse(await readFileAsync(vitePackageFile.fsPath, 'utf8')) as {
main: string;
};
const viteFolder = vitePackageFile.fsPath.replace('/package.json', '');
const vitePath = join(viteFolder, vitePackage.main).replace(wsFolder.fsPath, '../..');

// Update the vite reference, as it is not a direct dependency of the project
let scriptContents = await readFileAsync(scriptPath.fsPath, 'utf8');
scriptContents = scriptContents.replace(`"vite"`, `"${vitePath}"`);
await workspace.fs.writeFile(tempScriptPath, Buffer.from(scriptContents, 'utf8'));
}
} else {
workspace.fs.copy(scriptPath, tempScriptPath, { overwrite: true });
}

const fullScript = `node "${tempScriptPath.fsPath}" "${contentConfigFile.fsPath}"`;

Expand Down
2 changes: 1 addition & 1 deletion ssg-scripts/astro.collections.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { writeFileSync } from "fs";
import { join } from "path";
import { createServer } from "vite";
import zod from "zod";
import zod from "astro/zod";

const {
ZodDefault,
Expand Down

0 comments on commit 8db76ab

Please sign in to comment.