1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2024-12-23 01:29:41 +01:00
portfolio/scripts/content/generateProjects.ts

39 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

import fs from 'node:fs'
import path from 'node:path'
import type { ProjectType } from '@/types/project'
import yaml from 'js-yaml'
import ora from 'ora'
2024-02-06 00:20:48 +01:00
import { transformProject } from './transformProject'
2024-02-06 00:20:48 +01:00
const contentDirectory = path.join(process.cwd(), '_content')
const projectsOriginal = yaml.load(
fs.readFileSync(`${contentDirectory}/projects.yml`, 'utf8')
) as ProjectType[]
const projectsOutput = 'generated/projects.json'
export async function generateProjects(): Promise<void> {
const spinner = ora('Generating projects content...\n').start()
const slugs = projectsOriginal.map(({ slug }: { slug: string }) => slug)
try {
const projects: ProjectType[] = []
for (const slug of slugs) {
spinner.text = `Generating content for ${slug}...\n`
const project = await transformProject(projectsOriginal, slug)
if (project) projects.push(project)
}
2024-02-06 00:23:46 +01:00
const dirPath = path.dirname(projectsOutput)
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true })
}
fs.writeFileSync(projectsOutput, JSON.stringify(projects, null, 2))
spinner.succeed(`Projects content written to ${projectsOutput}\n`)
} catch (error: unknown) {
spinner.fail((error as Error).message)
}
}