mirror of
https://github.com/kremalicious/astro-redirect-from.git
synced 2024-12-22 09:23:21 +01:00
Convert URL.pathname to proper File Path (#196)
* Convert URL to File Path * Update tests * Update path to cache directory in tests
This commit is contained in:
parent
dadd12d7e1
commit
574d3ae272
@ -1,5 +1,6 @@
|
|||||||
import fs from 'node:fs'
|
import fs from 'node:fs'
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
|
import url from 'node:url'
|
||||||
import type { AstroIntegration } from 'astro'
|
import type { AstroIntegration } from 'astro'
|
||||||
import { getRedirects } from './getRedirects.js'
|
import { getRedirects } from './getRedirects.js'
|
||||||
import { getMarkdownFiles, getSlugFromFilePath, writeJson } from './utils.js'
|
import { getMarkdownFiles, getSlugFromFilePath, writeJson } from './utils.js'
|
||||||
@ -47,11 +48,13 @@ export async function initPlugin(
|
|||||||
|
|
||||||
updateConfig({ redirects })
|
updateConfig({ redirects })
|
||||||
|
|
||||||
if (!fs.existsSync(config.cacheDir.pathname)) {
|
const cacheDirPath = url.fileURLToPath(config.cacheDir)
|
||||||
fs.mkdirSync(config.cacheDir.pathname, { recursive: true })
|
|
||||||
|
if (!fs.existsSync(cacheDirPath)) {
|
||||||
|
fs.mkdirSync(cacheDirPath, { recursive: true })
|
||||||
}
|
}
|
||||||
const redirectFilePath = path.join(
|
const redirectFilePath = path.join(
|
||||||
config.cacheDir.pathname, // Default is ./node_modules/.astro/
|
cacheDirPath, // Default is ./node_modules/.astro/
|
||||||
'redirect_from.json'
|
'redirect_from.json'
|
||||||
)
|
)
|
||||||
await writeJson(redirectFilePath, redirects)
|
await writeJson(redirectFilePath, redirects)
|
||||||
|
@ -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 { describe, expect, it, vi } from 'vitest'
|
||||||
import * as redirects from '../src/getRedirects'
|
import * as redirects from '../src/getRedirects'
|
||||||
import astroRedirectFrom, { type HookOptions, initPlugin } from '../src/index'
|
import astroRedirectFrom, { type HookOptions, initPlugin } from '../src/index'
|
||||||
@ -9,10 +12,14 @@ const mockLogger = {
|
|||||||
error: vi.fn()
|
error: vi.fn()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cacheDirPath = url.pathToFileURL(
|
||||||
|
path.resolve('node_modules/.astro/')
|
||||||
|
).href
|
||||||
|
|
||||||
const hookOptionsMock = {
|
const hookOptionsMock = {
|
||||||
logger: mockLogger,
|
logger: mockLogger,
|
||||||
config: {
|
config: {
|
||||||
cacheDir: { pathname: './node_modules/.astro/' }
|
cacheDir: new URL(cacheDirPath)
|
||||||
},
|
},
|
||||||
command: 'dev',
|
command: 'dev',
|
||||||
updateConfig: vi.fn()
|
updateConfig: vi.fn()
|
||||||
@ -54,6 +61,9 @@ describe('initPlugin', () => {
|
|||||||
const writeJsonSpy = vi.spyOn(utils, 'writeJson')
|
const writeJsonSpy = vi.spyOn(utils, 'writeJson')
|
||||||
writeJsonSpy.mockImplementation(() => Promise.resolve())
|
writeJsonSpy.mockImplementation(() => Promise.resolve())
|
||||||
|
|
||||||
|
vi.spyOn(fs, 'existsSync').mockReturnValue(true)
|
||||||
|
vi.spyOn(fs, 'mkdirSync').mockImplementation(() => undefined)
|
||||||
|
|
||||||
await initPlugin(hookOptionsMock)
|
await initPlugin(hookOptionsMock)
|
||||||
|
|
||||||
expect(hookOptionsMock.updateConfig).toBeCalledWith({
|
expect(hookOptionsMock.updateConfig).toBeCalledWith({
|
||||||
|
Loading…
Reference in New Issue
Block a user