Comparing version
@@ -12,22 +12,15 @@ 'use strict'; | ||
var isLinux = ip.platform === 'linux'; | ||
if (ip.present()) { | ||
return ip.exec('docker-machine ip ' + type, { | ||
encoding: 'utf8', | ||
if (!ip.present()) { | ||
if (isLinux) { | ||
return 'localhost'; | ||
} | ||
return null; | ||
// By default parent stderr is used, which is bad | ||
stdio: [] | ||
}).trim(); | ||
} | ||
return ip.exec('docker-machine ip ' + type, { | ||
encoding: 'utf8', | ||
// By default parent stderr is used, which is bad | ||
stdio: [] | ||
}).trim(); | ||
return 'localhost'; | ||
} | ||
ip.present = function () { | ||
try { | ||
@@ -48,5 +41,4 @@ ip.exec('docker-machine version && echo $?', { | ||
ip.exec = _child_process.execSync; | ||
ip.platform = process.platform; | ||
exports.default = ip; | ||
module.exports = exports['default']; |
22
index.js
import { execSync as exec } from 'child_process'; | ||
function ip(type = 'default') { | ||
const isLinux = ip.platform === 'linux'; | ||
if (ip.present()) { | ||
return ip.exec(`docker-machine ip ${type}`, { | ||
encoding: 'utf8', | ||
if (!ip.present()) { | ||
if (isLinux) { | ||
return 'localhost'; | ||
} | ||
return null; | ||
// By default parent stderr is used, which is bad | ||
stdio: [] | ||
}).trim(); | ||
} | ||
return ip.exec(`docker-machine ip ${type}`, { | ||
encoding: 'utf8', | ||
// By default parent stderr is used, which is bad | ||
stdio: [] | ||
}).trim(); | ||
return 'localhost'; | ||
} | ||
ip.present = () => { | ||
try { | ||
@@ -39,4 +32,3 @@ ip.exec('docker-machine version && echo $?', { | ||
ip.exec = exec; | ||
ip.platform = process.platform; | ||
export default ip; |
{ | ||
"name": "docker-ip", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Get docker ip", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -75,63 +75,2 @@ import sinon from 'sinon'; | ||
it('should get `default` ip without arguments', () => { | ||
expect(ip()).to.equal(null); | ||
expect(ip.exec).to.not.be.called; | ||
}); | ||
it('should get `dev` ip', () => { | ||
expect(ip('dev')).to.equal(null); | ||
expect(ip.exec).to.not.be.called; | ||
}); | ||
}); | ||
}); | ||
describe('linux', () => { | ||
let old = ip.platform; | ||
beforeEach(() => { | ||
sinon.stub(ip, 'exec', () => 'test'); | ||
ip.platform = 'linux'; | ||
}); | ||
afterEach(() => { | ||
ip.exec.restore(); | ||
ip.platform = old; | ||
}); | ||
describe('if "docker machine" is present', () => { | ||
beforeEach(() => { | ||
sinon.stub(ip, 'present', () => true); | ||
}); | ||
afterEach(() => { | ||
ip.present.restore(); | ||
}); | ||
it('should get `default` ip without arguments', () => { | ||
ip(); | ||
expect(ip.exec.firstCall.args[0]).to.equal('docker-machine ip default'); | ||
expect(ip.exec.firstCall.args[1]).to.deep.equal({ | ||
encoding: 'utf8', | ||
stdio: [] | ||
}); | ||
}); | ||
it('should get `dev` ip', () => { | ||
ip('dev'); | ||
expect(ip.exec.firstCall.args[0]).to.equal('docker-machine ip dev'); | ||
expect(ip.exec.firstCall.args[1]).to.deep.equal({ | ||
encoding: 'utf8', | ||
stdio: [] | ||
}); | ||
}); | ||
}); | ||
describe('if "docker machine" is not present', () => { | ||
beforeEach(() => { | ||
sinon.stub(ip, 'present', () => false); | ||
}); | ||
afterEach(() => { | ||
ip.present.restore(); | ||
}); | ||
it('should get `default` ip without arguments', () => { | ||
expect(ip()).to.equal('localhost'); | ||
@@ -138,0 +77,0 @@ expect(ip.exec).to.not.be.called; |
48259
-3.48%541
-10.13%