Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

renamed setup to project_creator

  • Loading branch information...
commit 3ef88d7a6a1f49b870bab5b27416f60373470094 1 parent 6a44dbe
Lakshan Perera authored
Showing with 135 additions and 0 deletions.
  1. +53 −0 lib/project_creator.js
  2. +82 −0 spec/project_creator.js
53 lib/project_creator.js
View
@@ -0,0 +1,53 @@
+var fs = require("fs");
+var path = require("path");
+var child_process = require("child_process");
+
+module.exports = {
+
+ getDefaultTemplate: function() {
+ return path.join(__dirname, "../boilerplates/default");
+ },
+
+ createStructure: function(dir_path, template_path) {
+ var self = this;
+
+ // if template path is undefined, use the default bare template
+ template_path = template_path || self.getDefaultTemplate();
+
+ var createSiteDirectory = function(site_dir_path, callback) {
+ fs.stat(site_dir_path, function(err, stat) {
+ if (err || !stat.isDirectory()) {
+ return fs.mkdir(site_dir_path, callback);
+ } else {
+ return callback(null, site_dir_path);
+ }
+ });
+ };
+
+ var createSiteStructure = function(destination_path, source_path) {
+ var copy_command = "cp -r " + source_path + "/* " + destination_path;
+
+ child_process.exec(copy_command, function(err, stdout, stderr) {
+ if(err) {
+ throw err;
+ }
+
+ console.log("Created a new site in " + destination_path);
+ console.log("To get started, run: cd " + destination_path + "; punch s");
+ });
+ };
+
+ if (dir_path && dir_path !== ".") {
+ createSiteDirectory(dir_path, function(err) {
+ if (err) {
+ console.log("Failed to create the site in " + dir_path);
+ return false;
+ }
+ return createSiteStructure(dir_path, template_path);
+ });
+ } else {
+ createSiteStructure(process.cwd(), template_path);
+ }
+ }
+
+};
82 spec/project_creator.js
View
@@ -0,0 +1,82 @@
+var project_creator = require("../lib/project_creator.js");
+
+var fs = require("fs");
+var child_process = require("child_process");
+
+describe("creating a bare structure", function() {
+
+ it("create the site directory if needed", function() {
+ spyOn(fs, "stat").andCallFake(function(path, callback) {
+ return callback("error", null);
+ });
+
+ spyOn(fs, "mkdir");
+
+ project_creator.createStructure("site_path");
+
+ expect(fs.mkdir).toHaveBeenCalledWith("site_path", jasmine.any(Function));
+ });
+
+ it("create the structure using the given template", function() {
+ spyOn(fs, "stat").andCallFake(function(path, callback) {
+ return callback(null, { "isDirectory": function() { return true } });
+ });
+
+ spyOn(child_process, "exec");
+
+ project_creator.createStructure("site_path", "path/to/template");
+
+ expect(child_process.exec).toHaveBeenCalledWith("cp -r path/to/template/* site_path", jasmine.any(Function));
+ });
+
+ it("use the default template to create the structure if no template given", function() {
+ spyOn(fs, "stat").andCallFake(function(path, callback) {
+ return callback(null, { "isDirectory": function() { return true } });
+ });
+
+ spyOn(child_process, "exec");
+
+ spyOn(project_creator, "getDefaultTemplate").andReturn("default_template_path");
+
+ project_creator.createStructure("site_path");
+
+ expect(child_process.exec).toHaveBeenCalledWith("cp -r default_template_path/* site_path", jasmine.any(Function));
+ });
+
+ it("don't create the structure if site directory creation fails", function() {
+ spyOn(fs, "stat").andCallFake(function(path, callback) {
+ return callback("error", null);
+ });
+
+ spyOn(fs, "mkdir").andCallFake(function(path, callback) {
+ return callback("error");
+ });
+
+ spyOn(child_process, "exec");
+
+ project_creator.createStructure("site_path");
+
+ expect(child_process.exec).not.toHaveBeenCalled();
+
+ });
+
+ it("use the current path if no path is given", function(){
+ spyOn(process, "cwd").andReturn("current_path");
+
+ spyOn(child_process, "exec");
+
+ project_creator.createStructure(null, "path/to/template");
+
+ expect(child_process.exec).toHaveBeenCalledWith("cp -r path/to/template/* current_path", jasmine.any(Function));
+ });
+
+ it("use the current path if dot is given as the path", function(){
+ spyOn(process, "cwd").andReturn("current_path");
+ spyOn(child_process, "exec");
+
+ project_creator.createStructure(".", "path/to/template");
+
+ expect(child_process.exec).toHaveBeenCalledWith("cp -r path/to/template/* current_path", jasmine.any(Function));
+ });
+
+});
Please sign in to comment.
Something went wrong with that request. Please try again.