Comparing version 0.0.3 to 0.0.4
56
npmi.js
@@ -1,6 +0,7 @@ | ||
var npm = require('npm'); | ||
var npm = require('npm'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var LOAD_ERR = 'NPM_LOAD_ERR', | ||
INSTALL_ERR = 'NPM_INSTALL_ERR', | ||
LIST_ERR = 'NPM_LIST_ERR', | ||
VIEW_ERR = 'NPM_VIEW_ERR'; | ||
@@ -17,3 +18,3 @@ | ||
version = options.version || 'latest', | ||
path = options.path || '.', | ||
installPath = options.path || '.', | ||
forceInstall = options.forceInstall || false, | ||
@@ -37,25 +38,31 @@ localInstall = options.localInstall || false, | ||
if (installedVersion === latestVersion) return callback(null); | ||
else npm.commands.install(path, [name+'@'+latestVersion], installCallback); | ||
else npm.commands.install(installPath, [name+'@'+latestVersion], installCallback); | ||
} | ||
} | ||
function listCallback(err, list) { | ||
if (err) { | ||
err.code = LIST_ERR; | ||
return callback(err); | ||
} | ||
function checkInstalled() { | ||
// check that version matches | ||
fs.readFile(path.resolve(installPath, 'node_modules', name, 'package.json'), function (err, pkgRawData) { | ||
if (err) { | ||
// hmm, something went wrong while reading module's package.json file | ||
// lets try to reinstall it just in case | ||
return npm.commands.install(installPath, [name+'@'+version], installCallback); | ||
} | ||
// npm list success | ||
if (list.dependencies[name]) { | ||
var pkg = JSON.parse(pkgRawData); | ||
if (version === 'latest') { | ||
return npm.commands.view([name], true, viewCallback(list.dependencies[name].version)); | ||
// specified version is "latest" which means nothing for a check | ||
// so we need to ask npm to give us a view of the module from remote registry | ||
// in order to check if it really is the latest one that is currently installed | ||
return npm.commands.view([name], true, viewCallback(pkg.version)); | ||
} else if (pkg.version === version) { | ||
// package is installed and version matches | ||
return callback(null); | ||
} else { | ||
if (list.dependencies[name].version === version) { | ||
return callback(null); | ||
} | ||
// version does not match: reinstall | ||
return npm.commands.install(installPath, [name+'@'+version], installCallback); | ||
} | ||
} | ||
// if we end-up here, it means that whether the package is not installed | ||
// or it is installed but with the wrong version: reinstall | ||
npm.commands.install(path, [name+'@'+version], installCallback); | ||
}); | ||
} | ||
@@ -82,11 +89,11 @@ | ||
// local install won't work with version specified | ||
npm.commands.install(path, [name], installCallback); | ||
npm.commands.install(installPath, [name], installCallback); | ||
} else { | ||
if (forceInstall) { | ||
// reinstall package module | ||
npm.commands.install(path, [name+'@'+version], installCallback); | ||
// reinstall package module | ||
npm.commands.install(installPath, [name+'@'+version], installCallback); | ||
} else { | ||
// check if package is installed and | ||
npm.commands.list([name], true, listCallback); | ||
// check if package is installed | ||
checkInstalled(); | ||
} | ||
@@ -101,5 +108,4 @@ } | ||
npmi.INSTALL_ERR = INSTALL_ERR; | ||
npmi.LIST_ERR = LIST_ERR; | ||
npmi.VIEW_ERR = VIEW_ERR; | ||
module.exports = npmi; |
{ | ||
"name": "npmi", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Gives a simplier API to npm install (programatically installs stuffs)", | ||
@@ -5,0 +5,0 @@ "main": "npmi.js", |
@@ -9,3 +9,4 @@ var npmi = require('./../npmi'); | ||
path: '.', | ||
forceInstall: false | ||
forceInstall: false, | ||
localInstall: false | ||
}; | ||
@@ -12,0 +13,0 @@ npmi(options, function (err, result) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7320
8
127
1