Signed-off-by: Avior <github@avior.me>
This commit is contained in:
parent
f112ac6b50
commit
b4e122b617
2
.gitignore
vendored
2
.gitignore
vendored
@ -27,3 +27,5 @@ coverage/
|
|||||||
|
|
||||||
# Playwright
|
# Playwright
|
||||||
/playwright/
|
/playwright/
|
||||||
|
|
||||||
|
/src/route.ts
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { defineConfig } from 'astro/config'
|
import { defineConfig } from 'astro/config'
|
||||||
import tailwind from "@astrojs/tailwind"
|
import tailwind from "@astrojs/tailwind"
|
||||||
import node from "@astrojs/node"
|
import node from "@astrojs/node"
|
||||||
|
import routing from './hooks/routing'
|
||||||
|
|
||||||
// const faviconHook = {
|
// const faviconHook = {
|
||||||
// name: 'Favicon',
|
// name: 'Favicon',
|
||||||
@ -36,7 +37,7 @@ export default defineConfig({
|
|||||||
output: 'server',
|
output: 'server',
|
||||||
|
|
||||||
// Add TailwindCSS
|
// Add TailwindCSS
|
||||||
integrations: [tailwind()],
|
integrations: [tailwind(), routing()],
|
||||||
|
|
||||||
// prefetch links
|
// prefetch links
|
||||||
prefetch: {
|
prefetch: {
|
||||||
|
199
hooks/routing.ts
Normal file
199
hooks/routing.ts
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
import type { AstroIntegration } from 'astro'
|
||||||
|
import fs from 'node:fs/promises'
|
||||||
|
|
||||||
|
const baseFile = `
|
||||||
|
import { objectLoop } from '@dzeio/object-util'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a Route with \`[param]\` elements with data in them
|
||||||
|
*
|
||||||
|
* limits: currently does not support \`[...param]\`
|
||||||
|
*
|
||||||
|
* @param url {string} the url to format
|
||||||
|
* @param params {Record<string, string | number>} parameters to add to the URL (in [] first else in the query)
|
||||||
|
*
|
||||||
|
* @returns the URL formatted with the params
|
||||||
|
*/
|
||||||
|
export function formatRoute<T extends string>(url: T, params?: Record<string, string | number>): string {
|
||||||
|
let result: string = url
|
||||||
|
|
||||||
|
// early return if there are no params
|
||||||
|
if (!params) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// external queries for the URL
|
||||||
|
let externalQueries = ''
|
||||||
|
|
||||||
|
// loop through the parameters
|
||||||
|
objectLoop(params, (value, key) => {
|
||||||
|
const search = \`[\${key}]\`
|
||||||
|
value = encodeURI(value.toString())
|
||||||
|
if (!result.includes(search)) {
|
||||||
|
externalQueries += \`\${encodeURI(key)}=\${value}&\`
|
||||||
|
} else {
|
||||||
|
result = result.replace(search, value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// add items to the external queries if they are set
|
||||||
|
if (externalQueries) {
|
||||||
|
externalQueries = '?' + externalQueries.slice(0, externalQueries.length - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result + externalQueries
|
||||||
|
}`.trim()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate the file that contains every routes
|
||||||
|
*
|
||||||
|
* @param output the output file location
|
||||||
|
*/
|
||||||
|
async function updateRoutes(output: string, routes: Array<string>) {
|
||||||
|
let file = baseFile
|
||||||
|
file += `\n\nexport type Routes = ${routes.map((it) => `'${it}'`).join(' | ')}`
|
||||||
|
|
||||||
|
file += '\n\nexport default function route(route: Routes, query?: Record<string, string | number>) {'
|
||||||
|
file += '\n\treturn formatRoute(route, query)'
|
||||||
|
file += '\n}\n'
|
||||||
|
|
||||||
|
await fs.writeFile(output, file)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* format the path back to an url usable by the app
|
||||||
|
*
|
||||||
|
* @param path the path to format
|
||||||
|
* @returns the path formatted as a URL
|
||||||
|
*/
|
||||||
|
function formatPath(basePath: string, path: string, removeExtension = true): string {
|
||||||
|
// remove the base path
|
||||||
|
path = path.replace(basePath, '')
|
||||||
|
|
||||||
|
// remove the extension if asked
|
||||||
|
if (removeExtension) {
|
||||||
|
const lastDot = path.lastIndexOf('.')
|
||||||
|
path = path.slice(0, lastDot)
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove the index from the element
|
||||||
|
if (path.endsWith('/index')) {
|
||||||
|
path = path.replace('/index', '')
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle the `/` endpoint
|
||||||
|
if (path === '') {
|
||||||
|
path = '/'
|
||||||
|
}
|
||||||
|
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get every files recursivelly in a specific directory
|
||||||
|
*
|
||||||
|
* @param path the path to search
|
||||||
|
* @returns the list of files recursivelly in the specific directory
|
||||||
|
*/
|
||||||
|
async function getFiles(path: string): Promise<Array<string>> {
|
||||||
|
const dir = await fs.readdir(path)
|
||||||
|
let files: Array<string> = []
|
||||||
|
for (const file of dir) {
|
||||||
|
if (file.startsWith('_')) continue
|
||||||
|
const filePath = path + '/' + file
|
||||||
|
if ((await fs.stat(filePath)).isDirectory()) {
|
||||||
|
files = files.concat(await getFiles(filePath))
|
||||||
|
} else {
|
||||||
|
files.push(filePath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
|
||||||
|
let publicFolder!: string
|
||||||
|
let srcFolder!: string
|
||||||
|
let pagesFolder!: string
|
||||||
|
let outputFile!: string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* launch the integration
|
||||||
|
* @returns the routng integration
|
||||||
|
*/
|
||||||
|
const integration: () => AstroIntegration = () => ({
|
||||||
|
name: 'Routing',
|
||||||
|
hooks: {
|
||||||
|
'astro:config:setup': async ({ config }) => {
|
||||||
|
publicFolder = config.publicDir.toString().replace('file:///', '')
|
||||||
|
srcFolder = config.srcDir.toString().replace('file:///', '')
|
||||||
|
if (process.platform !== 'win32') {
|
||||||
|
srcFolder = '/' + srcFolder
|
||||||
|
publicFolder = '/' + publicFolder
|
||||||
|
}
|
||||||
|
pagesFolder = srcFolder + 'pages'
|
||||||
|
outputFile = srcFolder + 'route.ts'
|
||||||
|
|
||||||
|
// get the files list
|
||||||
|
const files = (await Promise.all([
|
||||||
|
await getFiles(pagesFolder).then((ev) => ev.map((it) => formatPath(pagesFolder, it))),
|
||||||
|
await getFiles(publicFolder).then((ev) => ev.map((it) => formatPath(publicFolder, it, false)))
|
||||||
|
])).flat()
|
||||||
|
await updateRoutes(outputFile, files)
|
||||||
|
},
|
||||||
|
'astro:server:setup': async ({ server }) => {
|
||||||
|
|
||||||
|
// get the files list
|
||||||
|
const files = (await Promise.all([
|
||||||
|
await getFiles(pagesFolder).then((ev) => ev.map((it) => formatPath(pagesFolder, it))),
|
||||||
|
await getFiles(publicFolder).then((ev) => ev.map((it) => formatPath(publicFolder, it, false)))
|
||||||
|
])).flat()
|
||||||
|
|
||||||
|
// watch FS changes for new files to add them to the route list
|
||||||
|
server.watcher.on('add', (path) => {
|
||||||
|
path = path.replace(/\\/g, '/')
|
||||||
|
|
||||||
|
// ignore files starting with '_'
|
||||||
|
const filename = path.slice(path.lastIndexOf('/') + 1)
|
||||||
|
if (filename.startsWith('_')) return
|
||||||
|
|
||||||
|
let removeExtension = true
|
||||||
|
let folder = pagesFolder
|
||||||
|
if(path.startsWith(publicFolder)) {
|
||||||
|
removeExtension = false
|
||||||
|
folder = publicFolder
|
||||||
|
} else if (!path.startsWith(folder)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// format the path
|
||||||
|
path = formatPath(folder, path, removeExtension)
|
||||||
|
|
||||||
|
// update the router
|
||||||
|
files.push(path)
|
||||||
|
updateRoutes(outputFile, files)
|
||||||
|
})
|
||||||
|
|
||||||
|
// watch FS changes for removed files to remove them from the list
|
||||||
|
server.watcher.on('unlink', (path) => {
|
||||||
|
path = path.replace(/\\/g, '/')
|
||||||
|
let removeExtension = true
|
||||||
|
let folder = pagesFolder
|
||||||
|
if(path.startsWith(publicFolder)) {
|
||||||
|
removeExtension = false
|
||||||
|
folder = publicFolder
|
||||||
|
}
|
||||||
|
path = formatPath(folder, path, removeExtension)
|
||||||
|
|
||||||
|
const index = files.indexOf(path)
|
||||||
|
|
||||||
|
files.splice(index, 1)
|
||||||
|
updateRoutes(outputFile, files)
|
||||||
|
})
|
||||||
|
|
||||||
|
// run the script once
|
||||||
|
await updateRoutes(outputFile, files)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
export default integration
|
Loading…
x
Reference in New Issue
Block a user