Skip to content

Commit

Permalink
fixes 31, adding npm install postinstall
Browse files Browse the repository at this point in the history
  • Loading branch information
dbashford committed Jan 30, 2015
1 parent 0964450 commit 16ef8d6
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 49 deletions.
58 changes: 46 additions & 12 deletions lib/command/impl/new.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec, fs, logger, newSkeleton, path, retrieveRegistry, rimraf, windowsDrive, wrench, _cleanup, _cloneGitHub, _isGitHub, _isSystemPath, _moveDirectoryContents;
var exec, fs, logger, newSkeleton, path, retrieveRegistry, rimraf, windowsDrive, wrench, _cleanup, _cloneGitHub, _isGitHub, _isSystemPath, _moveDirectoryContents, _runNPMInstall, _success;

exec = require('child_process').exec;

Expand All @@ -16,6 +16,12 @@ logger = null;

windowsDrive = /^[A-Za-z]:\\/;

_success = function() {
logger.success("New skeleton project successfully created!");
logger.info("Inside the new project folder, run 'mimosa watch' to build and watch assets.");
return logger.info("If the new project has server integration, add a '--server' or '-s' flag to serve assets.");
};

_isSystemPath = function(str) {
return windowsDrive.test(str) || str.indexOf("/") === 0;
};
Expand All @@ -37,21 +43,47 @@ _cloneGitHub = function(skeletonName, directory) {
_moveDirectoryContents(inPath, directory);
logger.info("Cleaning up...");
_cleanup(directory);
return rimraf(inPath, function(err) {
if (err) {
if (process.platform === 'win32') {
logger.warn("A known Windows/Mimosa has made the directory at [[ " + inPath + " ]] unremoveable. You will want to clean that up. Apologies!");
return logger.success("Skeleton successfully cloned from GitHub.");
return _runNPMInstall(directory, function() {
return rimraf(inPath, function(err) {
if (err) {
if (process.platform === 'win32') {
logger.warn("A known Windows/Mimosa has made the directory at [[ " + inPath + " ]] unremoveable. You will want to clean that up. Apologies!");

This comment has been minimized.

Copy link
@Anachron

Anachron Mar 2, 2015

Contributor

@dbashford "A known Windows/Mimosa bug ..." 👍

This comment has been minimized.

Copy link
@dbashford

dbashford Mar 2, 2015

Author Owner

Open an issue? Is easier to track. =)

return _success();
} else {
return logger.error("An error occurred cleaning up the temporary holding directory", err);
}
} else {
return logger.error("An error occurred cleaning up the temporary holding directory", err);
return _success();
}
} else {
return logger.success("Skeleton successfully cloned from GitHub.");
}
});
});
});
};

_runNPMInstall = function(directory, cb) {
var currentDir, packageJSON;
currentDir = process.cwd();
process.chdir(directory);
packageJSON = path.join(directory, "package.json");
if (!fs.existsSync(packageJSON)) {
return cb();
}
logger.info("Running npm install inside project directory...");
return exec("npm install", function(err, sout, serr) {
if (err) {
logger.error(err);
} else {
console.log(sout);
}
if (logger.isDebug()) {
logger.debug("Node module install sout: " + sout);
logger.debug("Node module install serr: " + serr);
}
process.chdir(currentDir);
return cb();
});
};

_moveDirectoryContents = function(sourcePath, outPath) {
var contents, fileContents, fileStats, fullOutPath, fullSourcePath, item, _i, _len;
contents = wrench.readdirSyncRecursive(sourcePath).filter(function(p) {
Expand Down Expand Up @@ -95,12 +127,14 @@ newSkeleton = function(skeletonName, directory, opts, _logger) {
logger.setDebug();
process.env.DEBUG = true;
}
directory = directory != null ? _isSystemPath(directory) ? directory : path.join(process.cwd(), directory) : process.cwd();
directory = _isSystemPath(directory) ? directory : path.join(process.cwd(), directory);
if (_isGitHub(skeletonName)) {
return _cloneGitHub(skeletonName, directory);
} else if (_isSystemPath(skeletonName)) {
_moveDirectoryContents(skeletonName, directory);
return logger.success("Copied local skeleton to [[ " + directory + " ]]");
return _runNPMInstall(directory, function() {
return logger.success("Copied local skeleton to [[ " + directory + " ]]");
});
} else {
return retrieveRegistry(logger, function(registry) {
var skels;
Expand Down
13 changes: 7 additions & 6 deletions lib/command/list_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ _list = function() {
};

module.exports = function(program, _logger) {
var _this = this;
logger = _logger;
return program.command('skel:list').description("List all skeletons").action(_list).on('--help', function() {
logger.green(' The skel:list will list all of the skeletons in the skeleton registry.');
logger.green(' It will list all of the details for each skeleton.');
return logger.blue('\n $ mimosa skel:list\n');
});
return program.command('skel:list').description("List all skeletons").action(_list).on('--help', (function(_this) {
return function() {
logger.green(' The skel:list will list all of the skeletons in the skeleton registry.');
logger.green(' It will list all of the details for each skeleton.');
return logger.blue('\n $ mimosa skel:list\n');
};
})(this));
};
29 changes: 15 additions & 14 deletions lib/command/new_command.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
module.exports = function(program, _logger) {
var logger,
_this = this;
var logger;
logger = _logger;
return program.command('skel:new <skeletonName> [directory]').description("Create a Mimosa project using a skeleton").option("-D, --mdebug", "run in debug mode").action(function(skeletonName, directory, opts) {
return program.command('skel:new <skeletonName> <directory>').description("Create a Mimosa project using a skeleton").option("-D, --mdebug", "run in debug mode").action(function(skeletonName, directory, opts) {
var newSkel;
newSkel = require("./impl/new");
return newSkel(skeletonName, directory, opts, logger);
}).on('--help', function() {
logger.green(' The skel:new command will create a new project using a Mimosa skeleton of your choice.');
logger.green(' The first argument passed to skel:new is the name of the skeleton to use. The name can');
logger.green(' take several forms. It can be either the 1) name of the skeleton from the skeleton');
logger.green(' registry, 2) the github URL of the skeleton or 3) the path to the skeleton if the');
logger.green(' skeleton is on the file system for when skeleton development is taking place. The second');
logger.green(' argument is the name of the directory to place the skeleton in. If the directory does');
logger.green(' not exist, Mimosa will create it. If the directory is not provided, the skeleton will be');
logger.green(' placed in the current directory.');
return logger.blue('\n $ mimosa skel:new <skeletonName> <directory>\n');
});
}).on('--help', (function(_this) {
return function() {
logger.green(' The skel:new command will create a new project using a Mimosa skeleton of your choice.');
logger.green(' The first argument passed to skel:new is the name of the skeleton to use. The name can');
logger.green(' take several forms. It can be either the 1) name of the skeleton from the skeleton');
logger.green(' registry, 2) the github URL of the skeleton or 3) the path to the skeleton if the');
logger.green(' skeleton is on the file system for when skeleton development is taking place. The second');
logger.green(' argument is the name of the directory to place the skeleton in. If the directory does');
logger.green(' not exist, Mimosa will create it. If the directory is not provided, the skeleton will be');
logger.green(' placed in the current directory.');
return logger.blue('\n $ mimosa skel:new <skeletonName> <directory>\n');
};
})(this));
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "skelmimosa",
"version": "1.2.0",
"version": "1.3.0",
"homepage": "https://github.com/dbashford/mimosa-skeleton",
"author": "David Bashford",
"description": "A skeleton module for Mimosa",
Expand Down
58 changes: 43 additions & 15 deletions src/command/impl/new.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ logger = null

windowsDrive = /^[A-Za-z]:\\/

_success = ->
logger.success "New skeleton project successfully created!"
logger.info "Inside the new project folder, run 'mimosa watch' to build and watch assets."
logger.info "If the new project has server integration, add a '--server' or '-s' flag to serve assets."

_isSystemPath = (str) ->
windowsDrive.test(str) or str.indexOf("/") is 0

Expand All @@ -29,15 +34,40 @@ _cloneGitHub = (skeletonName, directory) ->
_moveDirectoryContents inPath, directory
logger.info "Cleaning up..."
_cleanup directory
rimraf inPath, (err) ->
if err
if process.platform is 'win32'
logger.warn "A known Windows/Mimosa has made the directory at [[ #{inPath} ]] unremoveable. You will want to clean that up. Apologies!"
logger.success "Skeleton successfully cloned from GitHub."
_runNPMInstall directory, ->
rimraf inPath, (err) ->
if err
if process.platform is 'win32'
logger.warn "A known Windows/Mimosa has made the directory at [[ #{inPath} ]] unremoveable. You will want to clean that up. Apologies!"
_success()
else
logger.error "An error occurred cleaning up the temporary holding directory", err
else
logger.error "An error occurred cleaning up the temporary holding directory", err
else
logger.success "Skeleton successfully cloned from GitHub."
_success()

_runNPMInstall = (directory, cb) ->
currentDir = process.cwd()
process.chdir directory

# if no packagejson, no need for npm install
packageJSON = path.join directory, "package.json"
if !fs.existsSync(packageJSON)
return cb()

logger.info "Running npm install inside project directory..."
exec "npm install", (err, sout, serr) ->
if err
logger.error err
else
console.log sout

if logger.isDebug()
logger.debug "Node module install sout: #{sout}"
logger.debug "Node module install serr: #{serr}"

process.chdir currentDir

cb()

_moveDirectoryContents = (sourcePath, outPath) ->
contents = wrench.readdirSyncRecursive(sourcePath).filter (p) ->
Expand Down Expand Up @@ -75,19 +105,17 @@ newSkeleton = (skeletonName, directory, opts, _logger) ->
logger.setDebug()
process.env.DEBUG = true

directory = if directory?
if _isSystemPath(directory)
directory
else
path.join process.cwd(), directory
directory = if _isSystemPath(directory)
directory
else
process.cwd()
path.join process.cwd(), directory

if _isGitHub(skeletonName)
_cloneGitHub(skeletonName, directory)
else if _isSystemPath(skeletonName)
_moveDirectoryContents skeletonName, directory
logger.success "Copied local skeleton to [[ #{directory} ]]"
_runNPMInstall directory, ->
logger.success "Copied local skeleton to [[ #{directory} ]]"
else
retrieveRegistry logger, (registry) ->
skels = registry.skels.filter (s) -> s.name is skeletonName
Expand Down
2 changes: 1 addition & 1 deletion src/command/new_command.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = (program, _logger) ->
logger = _logger
program
.command('skel:new <skeletonName> [directory]')
.command('skel:new <skeletonName> <directory>')
.description("Create a Mimosa project using a skeleton")
.option("-D, --mdebug", "run in debug mode")
.action( (skeletonName, directory, opts) ->
Expand Down

0 comments on commit 16ef8d6

Please sign in to comment.