2024-02-04 23:09:00 +01:00
|
|
|
import fs from 'fs'
|
|
|
|
import yaml from 'js-yaml'
|
|
|
|
import ora from 'ora'
|
2024-02-06 00:20:48 +01:00
|
|
|
import path from 'path'
|
|
|
|
import type ProjectType from '@/types/project'
|
|
|
|
import { transformProject } from './transformProject'
|
2024-02-04 23:09:00 +01:00
|
|
|
|
2024-02-06 00:20:48 +01:00
|
|
|
const contentDirectory = path.join(process.cwd(), '_content')
|
2024-02-04 23:09:00 +01:00
|
|
|
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 })
|
|
|
|
}
|
|
|
|
|
2024-02-04 23:09:00 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|