1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2024-12-22 17:23:22 +01:00
portfolio/scripts/new.ts

37 lines
1002 B
TypeScript
Raw Permalink Normal View History

#!/usr/bin/env ts-node
2024-07-26 13:43:45 +02:00
import fs from 'node:fs'
import path from 'node:path'
import ora from 'ora'
2018-09-24 00:13:41 +02:00
import slugify from 'slugify'
2018-05-14 01:50:11 +02:00
const templatePath = path.join(process.cwd(), 'scripts', 'new.yml')
2018-05-14 01:50:11 +02:00
const template = fs.readFileSync(templatePath).toString()
2018-05-23 20:53:08 +02:00
const spinner = ora('Adding new project').start()
if (!process.argv[2]) {
spinner.fail('Use the format `npm run new -- Title of project`')
}
2018-05-14 01:50:11 +02:00
const title = process.argv[2]
2018-05-23 20:53:08 +02:00
spinner.text = `Adding '${title}'.`
2018-05-14 01:50:11 +02:00
2018-05-23 20:53:08 +02:00
const titleSlug = slugify(title, { lower: true })
const projects = path.join(process.cwd(), '_content', 'projects.yml')
2018-05-23 20:53:08 +02:00
const newContents = template
.split('TITLE')
.join(title)
.split('SLUG')
.join(titleSlug)
2018-05-14 01:50:11 +02:00
// prepend newContents to projects.yml file
fs.readFile(projects, 'utf8', (error, data) => {
if (error) spinner.fail(error.message)
fs.writeFile(projects, newContents + data, (error) => {
if (error) spinner.fail(error.message)
spinner.succeed(`Added '${title}' to top of projects.yml file.`)
})
2018-05-14 01:50:11 +02:00
})