samhuri.net/bin/projects.js
2012-04-28 09:01:43 -07:00

79 lines
2.1 KiB
JavaScript
Executable file

#!/usr/bin/env node
var fs = require('fs')
, path = require('path')
, mustache = require('mustache')
, rootDir = path.join(__dirname, '..')
, projectFile = path.join(rootDir, process.argv[2])
, templateDir = path.join(rootDir, 'templates', 'proj')
, targetDir = path.join(rootDir, process.argv[3])
try {
fs.mkdirSync(targetDir, 0775)
}
catch (e) {
if (e.code != 'EEXIST') throw e
}
function main() {
var ctx = {}
fs.readFile(path.join(templateDir, 'project.html'), function(err, html) {
if (err) throw err
ctx.template = html.toString()
fs.readFile(projectFile, function(err, json) {
if (err) throw err
var projects = JSON.parse(json).projects
, index = path.join(targetDir, 'index.html')
// write project index
fs.readFile(path.join(templateDir, 'index.html'), function(err, tpl) {
if (err) throw err
fs.mkdir(targetDir, 0775, function(err) {
if (err && err.code !== 'EEXIST') throw err
fs.unlink(index, function(err) {
if (err && err.code !== 'ENOENT') throw err
var vals = { projects: projects }
, html = mustache.to_html(tpl.toString(), vals)
fs.writeFile(index, html, function(err) {
if (err) throw err
console.log('* (project index)')
})
})
})
})
// write project pages
ctx.n = 0
projects.forEach(function(project) {
ctx.n += 1
buildProject(project.name, project, ctx)
})
})
})
}
function buildProject(name, project, ctx) {
var dir = path.join(targetDir, name)
, index = path.join(dir, 'index.html')
try {
fs.mkdirSync(dir, 0775)
}
catch (e) {
if (e.code != 'EEXIST') throw e
}
fs.unlink(index, function(err) {
if (err && err.code !== 'ENOENT') throw err
project.name = name
fs.writeFile(index, mustache.to_html(ctx.template, project), function(err) {
if (err) console.error('error: ', err.message)
ctx.n -= 1
console.log('* ' + name + (err ? ' (failed)' : ''))
if (ctx.n === 0) console.log('done projects')
})
})
}
if (module == require.main) main()