Created
February 1, 2016 07:12
-
-
Save dustinboston/90d7726322c837619a83 to your computer and use it in GitHub Desktop.
Publishing workflow for Grunt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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