-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename Lib to NpmHelper and move more methods to it
- Loading branch information
1 parent
2949ea8
commit 0b93d86
Showing
4 changed files
with
97 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '/'; | ||
}; |