Skip to content

Commit

Permalink
Rename Lib to NpmHelper and move more methods to it
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjoelkemp committed Mar 17, 2014
1 parent 2949ea8 commit 0b93d86
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 43 deletions.
5 changes: 3 additions & 2 deletions helpers/GruntHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ var fs = require('fs'),
q = require('q'),
exec = require('child_process').exec,
EventEmitter = require('events').EventEmitter,
util = require('util');
util = require('util'),
utils = require('./Utils');

module.exports = function (directory) {
EventEmitter.call(this);
Expand Down Expand Up @@ -80,7 +81,7 @@ module.exports.prototype.flushConfig = function (config) {
if (! config) throw new Error('Grunt config cannot be empty');

var deferred = q.defer(),
dirName = this.directory[this.directory.length - 1] === '/' ? this.directory : this.directory + '/',
dirName = utils.slashDir(this.directory),
gruntDef = generateGruntfile(config);

// Substitute the generated configuration
Expand Down
41 changes: 0 additions & 41 deletions helpers/LibHelper.js

This file was deleted.

88 changes: 88 additions & 0 deletions helpers/NpmHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var q = require('q'),
exec = require('child_process').exec,
fs = require('fs'),
utils = require('./Utils');

// A library/module is already installed if its folder exists
// within node_modules
// TODO: Why not just use require.resolve?
module.exports.isLibInstalled = function (lib) {
var installDir = './node_modules/' + lib,
deferred = q.defer();

fs.exists(installDir, function (exists) {
deferred.resolve(exists);
});

return deferred.promise;
};

// Installs the given library as a dev dependency
// and resolve when done or if the lib was already installed
module.exports.installLib = function (lib) {
var deferred = q.defer(),
// Get the grunt package to install
installCmd = 'npm install --save-dev ' + lib;

// Install it
exec(installCmd, function(err) {
if (err) {
console.log(err);
deferred.reject(err);
} else {
deferred.resolve();
}
});

return deferred.promise;
};

module.exports.hasPackageJsonFile = function (directory) {
if (! directory) throw new Error('directory not given');

var deferred = q.defer();

// TODO: Are we guaranteed for the package.json file to live in supplied directory?
directory = utils.slashDir(directory);

fs.exists(directory + 'package.json', function (exists) {
deferred.resolve(exists);
});

return deferred.promise;
};

// Returns a json object representing an empty package.json file
module.exports.getDummyPackageJson = function () {
return {
'author': '',
'name': '',
'description': '',
'version': '',
'repository': {
'url': ''
},
'dependencies': {},
'devDependencies': {
'ya.js': '*'
},
'main': '',
'license': ''
};
};

// Generates and saves an empty package.json file in
// the passed directory
module.exports.createEmptyPackageJsonFile = function (directory) {
directory = utils.slashDir(directory);

var emptyPackageFile = JSON.stringify(this.getDummyPackageJson(), null, 2),
deferred = q.defer();

fs.writeFile(directory + 'package.json', emptyPackageFile, function (err) {
if (err) deferred.reject();
else deferred.resolve();
});

return deferred.promise;
};
6 changes: 6 additions & 0 deletions helpers/Utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Collection of little utility helpers

// Helper to return a slash-trailed version of the directory name
module.exports.slashDir = function (directory) {
return directory[directory.length - 1] === '/' ? directory : directory + '/';
};

0 comments on commit 0b93d86

Please sign in to comment.