Skip to content

Commit

Permalink
Merge pull request sintaxi#234 from zeke/templates
Browse files Browse the repository at this point in the history
Add support for Github-powered templates.
  • Loading branch information
sintaxi committed Feb 16, 2014
2 parents cb21ee0 + b868973 commit aaa2928
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 41 deletions.
64 changes: 38 additions & 26 deletions bin/harp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env node

var nodePath = require("path")
var program = require("commander")
var fse = require("fs-extra")
var pkg = require("../package.json")
var helpers = require("../lib/helpers")
var harp = require("../")
var nodePath = require("path")
var program = require("commander")
var fse = require("fs-extra")
var downloadRepo = require("download-github-repo")
var pkg = require("../package.json")
var helpers = require("../lib/helpers")
var harp = require("../")

var output = function(msg){
var v = pkg.version
Expand All @@ -23,13 +24,18 @@ program

program
.command("init [path]")
.usage("initializes a new Harp project in the current directory.\n Use the init command if it’s your first time using Harp and you want to create a new project.")
.usage("initializes a new Harp project in the current directory.\n See available boilerplates at https://github.com/harp-boilerplates")
.option("-b, --boilerplate <github-username/repo>", "use a github repo as a boilerplate", "harp-boilerplates/default")
.description("Initialize a new Harp project in current directory")
.action(function(path, program){
var projectPath = nodePath.resolve(process.cwd(), path || "")
var boilerplatePath = nodePath.resolve(__dirname, "..", "lib", "sample")
var boilerplatePath = nodePath.resolve(__dirname, "..", "lib", "default_boilerplate")
var repo = program.boilerplate

var done = function(err){
// Assume `harp-boilerplates` github org if boilerplate doesn't contain a slash
repo.match(/\//) || (repo = "harp-boilerplates/"+repo)

var done = function() {
console.log("Initialized project at", projectPath)
}

Expand All @@ -38,24 +44,30 @@ program

fse.readdir(projectPath, function(err, contents){
if(err) return err
if(contents.length == 0){
fse.readdir(boilerplatePath, function(err, contents){
if(err) return err
var total = contents.length
var count = 0
contents.forEach(function(i){
var fromPath = nodePath.resolve(boilerplatePath, i)
var toPath = nodePath.resolve(projectPath, i)
fse.copy(fromPath, toPath, function(err){
count++
if(count == total) done()
})
})
})
}else{
console.log("Sorry,", projectPath, " must be empty.")

if(contents.length !== 0){
console.log("Sorry,", projectPath, "must be empty.")
return
}
var count = 0

console.log("Downloading boilerplate: https://github.com/"+repo)

downloadRepo(repo, projectPath, function(err) {

if (!err) return done()

if (require('util').isError(err) && err['code'] === 'ENOTFOUND') {
console.error("You're not connected to the Internet, so we'll use the default boilerplate.")
fse.copy(boilerplatePath, projectPath, function(err){
if (err) return err
return done()
})
} else {
return console.error("Template not found:", "https://github.com/"+repo)
}

})

})
})
})
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
71 changes: 56 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,58 @@
"description": "Static web server with built in preprocessing",
"author": "Brock Whitten <[email protected]>",
"contributors": [
{ "name": "Brock Whitten", "email": "[email protected]" },
{ "name": "Rob Ellis", "email": "[email protected]" },
{ "name": "Jorge Pedret", "email": "[email protected]" },
{ "name": "Michael Brooks", "email": "[email protected]" },
{ "name": "Tommy-Carlos Williams", "email": "[email protected]" },
{ "name": "Darryl Pogue", "email": "[email protected]" },
{ "name": "Boris Mann", "email": "[email protected]" },
{ "name": "Kenneth Ormandy", "email": "[email protected]" },
{ "name": "Keith Yao", "email": "[email protected]" },
{ "name": "Eric Drechsel", "email": "[email protected]" },
{ "name": "Andrew Hobden", "email": "[email protected]" },
{ "name": "Max Melentiev", "email": "[email protected]" },
{ "name": "Remy Sharp", "email": "[email protected]" }
{
"name": "Brock Whitten",
"email": "[email protected]"
},
{
"name": "Rob Ellis",
"email": "[email protected]"
},
{
"name": "Jorge Pedret",
"email": "[email protected]"
},
{
"name": "Michael Brooks",
"email": "[email protected]"
},
{
"name": "Tommy-Carlos Williams",
"email": "[email protected]"
},
{
"name": "Darryl Pogue",
"email": "[email protected]"
},
{
"name": "Boris Mann",
"email": "[email protected]"
},
{
"name": "Kenneth Ormandy",
"email": "[email protected]"
},
{
"name": "Keith Yao",
"email": "[email protected]"
},
{
"name": "Eric Drechsel",
"email": "[email protected]"
},
{
"name": "Andrew Hobden",
"email": "[email protected]"
},
{
"name": "Max Melentiev",
"email": "[email protected]"
},
{
"name": "Remy Sharp",
"email": "[email protected]"
}
],
"homepage": "http://harpjs.com",
"bugs": "http://github.com/sintaxi/harp/issues",
Expand All @@ -31,13 +70,15 @@
"connect": "2.9.0",
"fs-extra": "0.6.4",
"async": "0.2.9",
"mime": "1.2.11"
"mime": "1.2.11",
"download-github-repo": "0.1.0"
},
"devDependencies": {
"mocha": "1.8.1",
"should": "1.2.2",
"request": "2.30.0",
"cheerio": "~0.13.1"
"cheerio": "~0.13.1",
"nixt": "~0.3.0"
},
"scripts": {
"test": "mocha --reporter spec"
Expand Down
52 changes: 52 additions & 0 deletions test/harp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var nixt = require('nixt')

describe("harp init", function() {

beforeEach(function(done){
nixt()
.run("rm -rf /tmp/harp && mkdir /tmp/harp")
.exist("/tmp/harp")
.end(done)
})

it("downloads the default boilerplate if it's not set", function(done) {
nixt()
.run('harp init /tmp/harp')
.stdout(/Downloading.*harp-boilerplates\/default/)
.stdout(/Initialized project at \/tmp\/harp/)
.exist('/tmp/harp/404.jade')
.exist('/tmp/harp/_layout.jade')
.exist('/tmp/harp/index.jade')
.exist('/tmp/harp/main.less')
.end(done)
})

it("defaults to the harp-boilerplates github org when given a shorthand pattern", function(done) {
nixt()
.run('harp init /tmp/harp -b hb-start')
.stdout(/Downloading.*harp-boilerplates\/hb-start/)
.exist('/tmp/harp/public')
.end(done)
})

it("honors -b option when given a user/repo pattern", function(done) {
nixt()
.run('harp init /tmp/harp -b zeke/harp-sample')
.stdout(/Downloading.*zeke\/harp-sample/)
.exist('/tmp/harp/README.md')
.exist('/tmp/harp/index.jade')
.end(done)
})

it("doesn't overwrite an existing directory", function(done) {
nixt()
.run('harp init /tmp/harp')
.end(function() {
nixt()
.run('harp init /tmp/harp')
.stdout(/Sorry,.*must be empty/)
.end(done)
})
})

})

0 comments on commit aaa2928

Please sign in to comment.