jointrashposs/scripts/get-static-endpoints.ts
かっこかり 1734b7bff6
ホスティングサービスをVercelに変更 (#32)
* vercel initial

* [ci skip] add gitignore

* fix config

* fix

* (add) prerendered routes を自動生成

* fix config

* fix path

* (fix) trailing slash
2023-12-01 07:39:47 +09:00

30 lines
1020 B
TypeScript

import fs from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
export function getStaticEndpoints(): string[] {
const __dirname = dirname(fileURLToPath(import.meta.url))
const dir = resolve(`${__dirname}/../pages`);
const files = getFiles(dir);
const filtered = files
.filter((file) => !file.includes('slug')) // exclude dynamic content
.map((file) => file.split('pages')[1])
.map((file) => file.replaceAll('\\', '/'))
.map((file) => {
return (file.endsWith('index.vue') ? file.replace(/\/index.vue$/, '') : file.split('.vue')[0]) + '/';
});
return filtered;
}
/**
* recursively get all files from /pages folder
*/
function getFiles(dir: string): string[] {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
const files = dirents.map((dirent) => {
const res = resolve(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
})
return files.flat();
}