|
1 | 1 | import { defineConfig } from 'astro/config' |
| 2 | +import path from 'node:path' |
| 3 | +import { symlink } from 'node:fs/promises' |
| 4 | +import { fileURLToPath } from 'node:url' |
2 | 5 | import starlight from '@astrojs/starlight' |
3 | 6 | import starlightLinksValidator from 'starlight-links-validator' |
4 | 7 | import starlightFullViewMode from 'starlight-fullview-mode' |
@@ -273,7 +276,8 @@ export default defineConfig({ |
273 | 276 | ] |
274 | 277 | } |
275 | 278 | ] |
276 | | - }) |
| 279 | + }), |
| 280 | + specSymlink() |
277 | 281 | ], |
278 | 282 | redirects: { |
279 | 283 | '/link-tag': '/tools/link-tag', |
@@ -319,3 +323,59 @@ export default defineConfig({ |
319 | 323 | port: 1100 |
320 | 324 | } |
321 | 325 | }) |
| 326 | + |
| 327 | +/** |
| 328 | + * Create a symlink to the WM spec folder build time (to /public/specification), |
| 329 | + * so that it works on Linux/Mac as well as Windows. |
| 330 | + * |
| 331 | + * Ideally, we wanted a relative symlink public/specification -> |
| 332 | + * ../specification, but that caused platform compatibility issues. So we create |
| 333 | + * absolute symlinks at build time, and ignore them from git. |
| 334 | + * |
| 335 | + * @returns {import('astro').AstroIntegration} |
| 336 | + */ |
| 337 | +function specSymlink() { |
| 338 | + return { |
| 339 | + name: 'spec-symlink', |
| 340 | + hooks: { |
| 341 | + 'astro:config:done': async ({ config, logger }) => { |
| 342 | + const { publicDir, root } = config |
| 343 | + const isWindows = process.platform === 'win32' |
| 344 | + const target = path.join(fileURLToPath(root), 'specification') |
| 345 | + const link = path.join(fileURLToPath(publicDir), 'specification') |
| 346 | + try { |
| 347 | + await symlink(target, link, isWindows ? 'junction' : 'dir') |
| 348 | + logger.info(`Created symlink from ${link} to ${target}`) |
| 349 | + } catch (error) { |
| 350 | + if (error.code === 'EEXIST') return |
| 351 | + throw error |
| 352 | + } |
| 353 | + }, |
| 354 | + 'astro:server:setup': ({ server }) => { |
| 355 | + // rewrite to index.html during dev |
| 356 | + server.middlewares.use((req, res, next) => { |
| 357 | + const base = '/specification' |
| 358 | + if (!req.url?.startsWith(base)) { |
| 359 | + next() |
| 360 | + return |
| 361 | + } |
| 362 | + |
| 363 | + if (!req.url.endsWith('/') && !req.url.includes('.')) { |
| 364 | + res.statusCode = 302 |
| 365 | + res.setHeader('Location', req.url + '/') |
| 366 | + res.end() |
| 367 | + return |
| 368 | + } |
| 369 | + |
| 370 | + const { pathname } = new URL(req.url, `http://${req.headers.host}`) |
| 371 | + if (pathname === `${base}/`) { |
| 372 | + req.url = `${base}/index.html` |
| 373 | + } else if (pathname === `${base}/flows/`) { |
| 374 | + req.url = `${base}/flows/index.html` |
| 375 | + } |
| 376 | + next() |
| 377 | + }) |
| 378 | + } |
| 379 | + } |
| 380 | + } |
| 381 | +} |
0 commit comments