Skip to content

Instantly share code, notes, and snippets.

@dustinboston
Created February 1, 2016 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinboston/90d7726322c837619a83 to your computer and use it in GitHub Desktop.
Save dustinboston/90d7726322c837619a83 to your computer and use it in GitHub Desktop.
Publishing workflow for Grunt
fs = require('fs')
# Transform sentence case to a filename
fileize = (date, title, extension) ->
characters = /("|'|!|\?|:|\s\z)/ig
whitespace = /\s/ig
"#{date}-#{title.replace(characters, '').replace(whitespace, '-').toLowerCase()}.#{extension}"
module.exports = (grunt) ->
grunt.initConfig
pkg: grunt.file.readJSON('package.json')
grunt.registerTask 'do', 'Create new content with magic', (type, title) ->
return unless arguments.length is 2
options =
data:
type: type
title: title
datetime: grunt.template.today 'isoDateTime'
date: grunt.template.today 'yyyy-mm-dd'
id: require('crypto').randomBytes(3).toString('hex')
archetype = "#{__dirname}/archetypes/#{type}.md"
unless fs.existsSync(archetype)
return grunt.fail.fatal "Content archetype '#{type}' does not exist"
template = grunt.file.read archetype, encoding: 'utf8'
processed = grunt.template.process template, options
filename = fileize options.data.date, title, 'md'
filepath = "#{__dirname}/content/#{type}/#{filename}"
if fs.existsSync(filepath)
return grunt.fail.fatal "The file #{filepath} already exists"
grunt.file.write filepath, processed, encoding: 'utf8'
grunt.log.write "Created content at #{filepath}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment