diff --git a/src/index.ts b/src/index.ts index 4f713fa..acd2043 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import fs from 'node:fs' import path from 'node:path' +import url from 'node:url' import type { AstroIntegration } from 'astro' import { getRedirects } from './getRedirects.js' import { getMarkdownFiles, getSlugFromFilePath, writeJson } from './utils.js' @@ -47,11 +48,13 @@ export async function initPlugin( updateConfig({ redirects }) - if (!fs.existsSync(config.cacheDir.pathname)) { - fs.mkdirSync(config.cacheDir.pathname, { recursive: true }) + const cacheDirPath = url.fileURLToPath(config.cacheDir) + + if (!fs.existsSync(cacheDirPath)) { + fs.mkdirSync(cacheDirPath, { recursive: true }) } const redirectFilePath = path.join( - config.cacheDir.pathname, // Default is ./node_modules/.astro/ + cacheDirPath, // Default is ./node_modules/.astro/ 'redirect_from.json' ) await writeJson(redirectFilePath, redirects) diff --git a/test/index.test.ts b/test/index.test.ts index c704f9b..8ead21b 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,3 +1,6 @@ +import fs from 'node:fs' +import path from 'node:path' +import url from 'node:url' import { describe, expect, it, vi } from 'vitest' import * as redirects from '../src/getRedirects' import astroRedirectFrom, { type HookOptions, initPlugin } from '../src/index' @@ -9,10 +12,14 @@ const mockLogger = { error: vi.fn() } +const cacheDirPath = url.pathToFileURL( + path.resolve('node_modules/.astro/') +).href + const hookOptionsMock = { logger: mockLogger, config: { - cacheDir: { pathname: './node_modules/.astro/' } + cacheDir: new URL(cacheDirPath) }, command: 'dev', updateConfig: vi.fn() @@ -54,6 +61,9 @@ describe('initPlugin', () => { const writeJsonSpy = vi.spyOn(utils, 'writeJson') writeJsonSpy.mockImplementation(() => Promise.resolve()) + vi.spyOn(fs, 'existsSync').mockReturnValue(true) + vi.spyOn(fs, 'mkdirSync').mockImplementation(() => undefined) + await initPlugin(hookOptionsMock) expect(hookOptionsMock.updateConfig).toBeCalledWith({