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
992 B
TypeScript
Raw Normal View History

#!/usr/bin/env ts-node
2018-09-24 00:13:41 +02:00
import fs from 'fs'
import ora from 'ora'
2018-09-24 00:13:41 +02:00
import path from 'path'
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
})