Skip to content
This repository
Browse code

renamed setup to project_creator

  • Loading branch information...
commit 3ef88d7a6a1f49b870bab5b27416f60373470094 1 parent 6a44dbe
Lakshan Perera authored

Showing 2 changed files with 135 additions and 0 deletions. Show diff stats Hide diff stats

  1. +53 0 lib/project_creator.js
  2. +82 0 spec/project_creator.js
53 lib/project_creator.js
... ... @@ -0,0 +1,53 @@
  1 +var fs = require("fs");
  2 +var path = require("path");
  3 +var child_process = require("child_process");
  4 +
  5 +module.exports = {
  6 +
  7 + getDefaultTemplate: function() {
  8 + return path.join(__dirname, "../boilerplates/default");
  9 + },
  10 +
  11 + createStructure: function(dir_path, template_path) {
  12 + var self = this;
  13 +
  14 + // if template path is undefined, use the default bare template
  15 + template_path = template_path || self.getDefaultTemplate();
  16 +
  17 + var createSiteDirectory = function(site_dir_path, callback) {
  18 + fs.stat(site_dir_path, function(err, stat) {
  19 + if (err || !stat.isDirectory()) {
  20 + return fs.mkdir(site_dir_path, callback);
  21 + } else {
  22 + return callback(null, site_dir_path);
  23 + }
  24 + });
  25 + };
  26 +
  27 + var createSiteStructure = function(destination_path, source_path) {
  28 + var copy_command = "cp -r " + source_path + "/* " + destination_path;
  29 +
  30 + child_process.exec(copy_command, function(err, stdout, stderr) {
  31 + if(err) {
  32 + throw err;
  33 + }
  34 +
  35 + console.log("Created a new site in " + destination_path);
  36 + console.log("To get started, run: cd " + destination_path + "; punch s");
  37 + });
  38 + };
  39 +
  40 + if (dir_path && dir_path !== ".") {
  41 + createSiteDirectory(dir_path, function(err) {
  42 + if (err) {
  43 + console.log("Failed to create the site in " + dir_path);
  44 + return false;
  45 + }
  46 + return createSiteStructure(dir_path, template_path);
  47 + });
  48 + } else {
  49 + createSiteStructure(process.cwd(), template_path);
  50 + }
  51 + }
  52 +
  53 +};
82 spec/project_creator.js
... ... @@ -0,0 +1,82 @@
  1 +var project_creator = require("../lib/project_creator.js");
  2 +
  3 +var fs = require("fs");
  4 +var child_process = require("child_process");
  5 +
  6 +describe("creating a bare structure", function() {
  7 +
  8 + it("create the site directory if needed", function() {
  9 + spyOn(fs, "stat").andCallFake(function(path, callback) {
  10 + return callback("error", null);
  11 + });
  12 +
  13 + spyOn(fs, "mkdir");
  14 +
  15 + project_creator.createStructure("site_path");
  16 +
  17 + expect(fs.mkdir).toHaveBeenCalledWith("site_path", jasmine.any(Function));
  18 + });
  19 +
  20 + it("create the structure using the given template", function() {
  21 + spyOn(fs, "stat").andCallFake(function(path, callback) {
  22 + return callback(null, { "isDirectory": function() { return true } });
  23 + });
  24 +
  25 + spyOn(child_process, "exec");
  26 +
  27 + project_creator.createStructure("site_path", "path/to/template");
  28 +
  29 + expect(child_process.exec).toHaveBeenCalledWith("cp -r path/to/template/* site_path", jasmine.any(Function));
  30 + });
  31 +
  32 + it("use the default template to create the structure if no template given", function() {
  33 + spyOn(fs, "stat").andCallFake(function(path, callback) {
  34 + return callback(null, { "isDirectory": function() { return true } });
  35 + });
  36 +
  37 + spyOn(child_process, "exec");
  38 +
  39 + spyOn(project_creator, "getDefaultTemplate").andReturn("default_template_path");
  40 +
  41 + project_creator.createStructure("site_path");
  42 +
  43 + expect(child_process.exec).toHaveBeenCalledWith("cp -r default_template_path/* site_path", jasmine.any(Function));
  44 + });
  45 +
  46 + it("don't create the structure if site directory creation fails", function() {
  47 + spyOn(fs, "stat").andCallFake(function(path, callback) {
  48 + return callback("error", null);
  49 + });
  50 +
  51 + spyOn(fs, "mkdir").andCallFake(function(path, callback) {
  52 + return callback("error");
  53 + });
  54 +
  55 + spyOn(child_process, "exec");
  56 +
  57 + project_creator.createStructure("site_path");
  58 +
  59 + expect(child_process.exec).not.toHaveBeenCalled();
  60 +
  61 + });
  62 +
  63 + it("use the current path if no path is given", function(){
  64 + spyOn(process, "cwd").andReturn("current_path");
  65 +
  66 + spyOn(child_process, "exec");
  67 +
  68 + project_creator.createStructure(null, "path/to/template");
  69 +
  70 + expect(child_process.exec).toHaveBeenCalledWith("cp -r path/to/template/* current_path", jasmine.any(Function));
  71 + });
  72 +
  73 + it("use the current path if dot is given as the path", function(){
  74 + spyOn(process, "cwd").andReturn("current_path");
  75 + spyOn(child_process, "exec");
  76 +
  77 + project_creator.createStructure(".", "path/to/template");
  78 +
  79 + expect(child_process.exec).toHaveBeenCalledWith("cp -r path/to/template/* current_path", jasmine.any(Function));
  80 + });
  81 +
  82 +});

0 comments on commit 3ef88d7

Please sign in to comment.
Something went wrong with that request. Please try again.