Comparing version 1.0.1 to 1.0.2
@@ -16,6 +16,7 @@ /* ================================================================ | ||
var fs = require('fs'); | ||
var os = require('os'); | ||
var _ = require('lodash'); | ||
var childProcess = require('child_process'); | ||
var _ = {}; | ||
// get system platform | ||
@@ -32,11 +33,13 @@ _.platform = (function() { | ||
_.isExistedFile = function(p){ | ||
p = p.replace(/(\?|#).*$/, ''); | ||
return fs.existsSync(p) && fs.statSync(p).isFile(); | ||
} | ||
_.exec = function(cmd, opts) { | ||
return new Promise((resolve, reject) => { | ||
childProcess.exec(cmd, opts, (err, stdout, stderr) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve([stdout, stderr]); | ||
}); | ||
}); | ||
}; | ||
_.isExistedDir = function(p){ | ||
return fs.existsSync(p) && fs.statSync(p).isDirectory(); | ||
} | ||
module.exports = _; |
@@ -17,5 +17,3 @@ /* ================================================================ | ||
var fs = require('fs'); | ||
var co = require('co'); | ||
var _ = require('./helper'); | ||
var exec = require('./exec'); | ||
@@ -25,70 +23,85 @@ var JAVA_HOME = {}; | ||
PATH_INVALID: '$JAVA_HOME path is invalid', | ||
PATH_NOTFOUND: '$JAVA_HOME is not found', | ||
PATH_NOTSET: '$JAVA_HOME not set' | ||
PATH_NOTFOUND: '$JAVA_HOME not found', | ||
PATH_NOTSET: '$JAVA_HOME is not set' | ||
}; | ||
function checkJavaHome(javaHome) { | ||
return new Promise((resolve, reject) => { | ||
if (!javaHome || typeof javaHome !== 'string') { | ||
return reject(new Error(ERROR.PATH_INVALID)); | ||
} | ||
fs.access(javaHome, err => { | ||
if (err) { | ||
return reject(new Error(ERROR.PATH_INVALID)); | ||
} | ||
fs.realpath(javaHome, (err, resolvedPath) => { | ||
if (err) { | ||
return reject(new Error(ERROR.PATH_INVALID)); | ||
} | ||
javaHome = resolvedPath; | ||
fs.stat(javaHome, (err, fsStat) => { | ||
if (err || !fsStat.isDirectory()) { | ||
return reject(new Error(ERROR.PATH_INVALID)); | ||
} | ||
resolve(javaHome); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
JAVA_HOME.getPath = function(callback) { | ||
co(function *(ctx) { | ||
var promise = new Promise((resolve, reject) => { | ||
// try process.env.JAVA_HOME | ||
var javaHome = process.env.JAVA_HOME; | ||
if (javaHome) { | ||
if (fs.existsSync(javaHome)) { | ||
// resolve symbolic link | ||
javaHome = fs.realpathSync(javaHome); | ||
if (_.isExistedDir(javaHome)) { | ||
return callback(null, javaHome); | ||
checkJavaHome(javaHome) | ||
.then(resolve) | ||
.catch(err => { | ||
if (err && _.platform.isWindows) { | ||
return reject(err); | ||
} else { | ||
return callback(ERROR.PATH_INVALID); | ||
// try $JAVA_HOME variables | ||
return _.exec('echo $JAVA_HOME'); | ||
} | ||
} else { | ||
return callback(ERROR.PATH_INVALID); | ||
} | ||
} | ||
if (_.platform.isWindows) { | ||
return callback(ERROR.PATH_NOTSET); | ||
} | ||
// try $JAVA_HOME variables | ||
javaHome = yield exec('echo $JAVA_HOME'); | ||
if (javaHome.trim()) { | ||
if (fs.existsSync(javaHome)) { | ||
// resolve symbolic link | ||
javaHome = fs.realpathSync(javaHome); | ||
if (_.isExistedDir(javaHome)) { | ||
return callback(null, javaHome); | ||
}) | ||
.then(std => { | ||
var stdout = std[0]; | ||
if (stdout.trim()) { | ||
javaHome = stdout; | ||
return checkJavaHome(javaHome); | ||
} else { | ||
return callback(ERROR.PATH_INVALID); | ||
throw new Error(ERROR.PATH_NOTFOUND); | ||
} | ||
} else { | ||
return callback(ERROR.PATH_INVALID); | ||
} | ||
} | ||
}) | ||
.then(resolve) | ||
.catch(err => { | ||
if (err && !_.platform.isOSX) { | ||
return reject(err); | ||
} else { | ||
// try /usr/libexec/java_home | ||
return _.exec('/usr/libexec/java_home'); | ||
} | ||
}) | ||
.then(std => { | ||
var stdout = std[0]; | ||
if (stdout.trim()) { | ||
return reject(new Error(ERROR.PATH_NOTSET)); | ||
} else { | ||
return reject(new Error(ERROR.PATH_NOTFOUND)); | ||
} | ||
}); | ||
}); | ||
// try /usr/libexec/java_home | ||
if (!_.platform.isOSX) { | ||
return callback(ERROR.PATH_NOTFOUND); | ||
} | ||
try { | ||
javaHome = yield exec('/usr/libexec/java_home'); | ||
if (javaHome.trim()) { | ||
return callback(null, javaHome); | ||
} | ||
} catch (e) { | ||
return callback(ERROR.PATH_NOTFOUND); | ||
} | ||
})(this); | ||
if (typeof callback === 'function') { | ||
return promise.then( | ||
res => callback(null, res), | ||
err => callback(err) | ||
); | ||
} | ||
return promise; | ||
}; | ||
module.exports = JAVA_HOME; |
{ | ||
"name": "java-home", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "java-home", | ||
@@ -11,3 +11,3 @@ "keywords": [ | ||
}, | ||
"main": "index.js", | ||
"main": "./lib/java-home", | ||
"repository": { | ||
@@ -18,4 +18,2 @@ "type": "git", | ||
"dependencies": { | ||
"co": "3", | ||
"lodash": "^3.10.1" | ||
}, | ||
@@ -22,0 +20,0 @@ "devDependencies": { |
@@ -16,5 +16,26 @@ /* ================================================================ | ||
var java-home = require('../'); | ||
var javaHome = require('../'); | ||
describe('test', function(){ | ||
it('getPath should be a promise', function(done) { | ||
javaHome.getPath().then(javaHomePath => { | ||
console.log(javaHomePath); | ||
done(); | ||
}, err => { | ||
console.log(err); | ||
done(); | ||
}); | ||
}); | ||
it('getPath should accept callback', function(done) { | ||
javaHome.getPath((err, javaHomePath) => { | ||
if (err) { | ||
console.log(err); | ||
done(); | ||
} else { | ||
console.log(javaHomePath); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); |
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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
0
167
2
1
8433
10
- Removedco@3
- Removedlodash@^3.10.1
- Removedco@3.1.0(transitive)
- Removedlodash@3.10.1(transitive)