Skip to content

Commit

Permalink
fix: add assets in public dir
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelgautier committed Oct 16, 2024
1 parent b083b98 commit 091ac6b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 22 additions & 3 deletions packages/adapters/astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import type { AstroIntegration } from 'astro';
import {
existsSync,
lstatSync,
mkdirSync,
readdirSync,
rmdirSync,
symlinkSync,
unlinkSync,
} from 'fs';
} from 'node:fs';
import { fileURLToPath } from 'node:url';
import { join } from 'path';

Expand All @@ -17,20 +19,30 @@ export { getRSS } from './rss.js';

type GalactiksOptions = object | undefined;

const symlinkDir = (targetPath: string, path: string) =>
const symlinkDir = (targetPath: string, path: string) => {
if (!existsSync(targetPath)) {
mkdirSync(path, { recursive: true });
}

readdirSync(path)
.filter((filename) => !filename.startsWith('.'))
.map((filename) => [join(targetPath, filename), join(path, filename)])
.filter(([target]) => !existsSync(target))
.forEach(([target, path]) => symlinkSync(path, target));
}

const removeDirSymbolicLinks = (path: string) => {
if (!existsSync(path)) {
return;
}

const removeDirSymbolicLinks = (path: string) =>
readdirSync(path)
.map((filename) => join(path, filename))
.filter((file) =>
lstatSync(file, { throwIfNoEntry: false })?.isSymbolicLink()
)
.forEach((file) => unlinkSync(file));
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default function createPlugin(_: GalactiksOptions): AstroIntegration {
Expand All @@ -41,6 +53,7 @@ export default function createPlugin(_: GalactiksOptions): AstroIntegration {

let assetsPath: string;
let publicPath: string;
let publicAssetsPath: string;

return {
name: '@galactiks/astro-integration',
Expand All @@ -65,6 +78,9 @@ export default function createPlugin(_: GalactiksOptions): AstroIntegration {
publicPath = fileURLToPath(config.publicDir);
galactiksConfig = setConfig('content.public', publicPath);

publicAssetsPath = join(publicPath, 'assets');
mkdirSync(publicAssetsPath, { recursive: true })

updateConfig({
site: galactiksConfig.webManifest.start_url,
trailingSlash,
Expand All @@ -78,9 +94,11 @@ export default function createPlugin(_: GalactiksOptions): AstroIntegration {

removeDirSymbolicLinks(assetsPath);
removeDirSymbolicLinks(publicPath);
removeDirSymbolicLinks(publicAssetsPath);

symlinkDir(assetsPath, galactiksConfigContentAssets);
symlinkDir(publicPath, galactiksConfigContentPublic);
symlinkDir(publicAssetsPath, galactiksConfigContentAssets);

if (command === 'dev') {
addWatchFile(galactiksConfig.content.generated);
Expand All @@ -91,6 +109,7 @@ export default function createPlugin(_: GalactiksOptions): AstroIntegration {

'astro:build:done': () => {
removeDirSymbolicLinks(assetsPath);
rmdirSync(publicAssetsPath, { recursive: true });
removeDirSymbolicLinks(publicPath);
},
},
Expand Down

0 comments on commit 091ac6b

Please sign in to comment.