Commit 54fafe1b authored by Jérémy Lal's avatar Jérémy Lal

Imported Upstream version 0.0.3

parents
Copyright (c) Isaac Z. Schlueter
All rights reserved.
The BSD License
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
# osenv
Look up environment settings specific to different operating systems.
## Usage
```javascript
var osenv = require('osenv')
var path = osenv.path()
var user = osenv.user()
// etc.
// Some things are not reliably in the env, and have a fallback command:
var h = osenv.hostname(function (er, hostname) {
h = hostname
})
// This will still cause it to be memoized, so calling osenv.hostname()
// is now an immediate operation.
// You can always send a cb, which will get called in the nextTick
// if it's been memoized, or wait for the fallback data if it wasn't
// found in the environment.
osenv.hostname(function (er, hostname) {
if (er) console.error('error looking up hostname')
else console.log('this machine calls itself %s', hostname)
})
```
## osenv.hostname()
The machine name. Calls `hostname` if not found.
## osenv.user()
The currently logged-in user. Calls `whoami` if not found.
## osenv.prompt()
Either PS1 on unix, or PROMPT on Windows.
## osenv.tmpdir()
The place where temporary files should be created.
## osenv.home()
No place like it.
## osenv.path()
An array of the places that the operating system will search for
executables.
## osenv.editor()
Return the executable name of the editor program. This uses the EDITOR
and VISUAL environment variables, and falls back to `vi` on Unix, or
`notepad.exe` on Windows.
## osenv.shell()
The SHELL on Unix, which Windows calls the ComSpec. Defaults to 'bash'
or 'cmd'.
var isWindows = process.platform === 'win32'
var windir = isWindows ? process.env.windir || 'C:\\Windows' : null
var path = require('path')
var exec = require('child_process').exec
// looking up envs is a bit costly.
// Also, sometimes we want to have a fallback
// Pass in a callback to wait for the fallback on failures
// After the first lookup, always returns the same thing.
function memo (key, lookup, fallback) {
var fell = false
var falling = false
exports[key] = function (cb) {
var val = lookup()
if (!val && !fell && !falling && fallback) {
fell = true
falling = true
exec(fallback, function (er, output, stderr) {
falling = false
if (er) return // oh well, we tried
val = output.trim()
})
}
exports[key] = function (cb) {
if (cb) process.nextTick(cb.bind(null, null, val))
return val
}
if (cb && !falling) process.nextTick(cb.bind(null, null, val))
return val
}
}
memo('user', function () {
return ( isWindows
? process.env.USERDOMAIN + '\\' + process.env.USERNAME
: process.env.USER
)
}, 'whoami')
memo('prompt', function () {
return isWindows ? process.env.PROMPT : process.env.PS1
})
memo('hostname', function () {
return isWindows ? process.env.COMPUTERNAME : process.env.HOSTNAME
}, 'hostname')
memo('tmpdir', function () {
var t = isWindows ? 'temp' : 'tmp'
return process.env.TMPDIR ||
process.env.TMP ||
process.env.TEMP ||
( exports.home() ? path.resolve(exports.home(), t)
: isWindows ? path.resolve(windir, t)
: '/tmp'
)
})
memo('home', function () {
return ( isWindows ? process.env.USERPROFILE
: process.env.HOME
)
})
memo('path', function () {
return (process.env.PATH ||
process.env.Path ||
process.env.path).split(isWindows ? ';' : ':')
})
memo('editor', function () {
return process.env.EDITOR ||
process.env.VISUAL ||
(isWindows ? 'notepad.exe' : 'vi')
})
memo('shell', function () {
return isWindows ? process.env.ComSpec || 'cmd'
: process.env.SHELL || 'bash'
})
{
"name": "osenv",
"version": "0.0.3",
"main": "osenv.js",
"directories": {
"test": "test"
},
"dependencies": {},
"devDependencies": {
"tap": "~0.2.5"
},
"scripts": {
"test": "tap test/*.js"
},
"repository": {
"type": "git",
"url": "git://github.com/isaacs/osenv"
},
"keywords": [
"environment",
"variable",
"home",
"tmpdir",
"path",
"prompt",
"ps1"
],
"author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
"license": "BSD",
"description": "Look up environment settings specific to different operating systems"
}
// only run this test on windows
// pretending to be another platform is too hacky, since it breaks
// how the underlying system looks up module paths and runs
// child processes, and all that stuff is cached.
if (process.platform === 'win32') {
console.log('TAP Version 13\n' +
'1..0\n' +
'# Skip unix tests, this is not unix\n')
return
}
var tap = require('tap')
// like unix, but funny
process.env.USER = 'sirUser'
process.env.HOME = '/home/sirUser'
process.env.HOSTNAME = 'my-machine'
process.env.TMPDIR = '/tmpdir'
process.env.TMP = '/tmp'
process.env.TEMP = '/temp'
process.env.PATH = '/opt/local/bin:/usr/local/bin:/usr/bin/:bin'
process.env.PS1 = '(o_o) $ '
process.env.EDITOR = 'edit'
process.env.VISUAL = 'visualedit'
process.env.SHELL = 'zsh'
tap.test('basic unix sanity test', function (t) {
var osenv = require('../osenv.js')
t.equal(osenv.user(), process.env.USER)
t.equal(osenv.home(), process.env.HOME)
t.equal(osenv.hostname(), process.env.HOSTNAME)
t.same(osenv.path(), process.env.PATH.split(':'))
t.equal(osenv.prompt(), process.env.PS1)
t.equal(osenv.tmpdir(), process.env.TMPDIR)
// mildly evil, but it's for a test.
process.env.TMPDIR = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.tmpdir(), process.env.TMP)
process.env.TMP = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.tmpdir(), process.env.TEMP)
process.env.TEMP = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.tmpdir(), '/home/sirUser/tmp')
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
osenv.home = function () { return null }
t.equal(osenv.tmpdir(), '/tmp')
t.equal(osenv.editor(), 'edit')
process.env.EDITOR = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.editor(), 'visualedit')
process.env.VISUAL = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.editor(), 'vi')
t.equal(osenv.shell(), 'zsh')
process.env.SHELL = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.shell(), 'bash')
t.end()
})
// only run this test on windows
// pretending to be another platform is too hacky, since it breaks
// how the underlying system looks up module paths and runs
// child processes, and all that stuff is cached.
if (process.platform !== 'win32') {
console.log('TAP Version 13\n' +
'1..0\n' +
'# Skip windows tests, this is not windows\n')
return
}
// load this before clubbing the platform name.
var tap = require('tap')
process.env.windir = 'C:\\windows'
process.env.USERDOMAIN = 'some-domain'
process.env.USERNAME = 'sirUser'
process.env.USERPROFILE = 'C:\\Users\\sirUser'
process.env.COMPUTERNAME = 'my-machine'
process.env.TMPDIR = 'C:\\tmpdir'
process.env.TMP = 'C:\\tmp'
process.env.TEMP = 'C:\\temp'
process.env.Path = 'C:\\Program Files\\;C:\\Binary Stuff\\bin'
process.env.PROMPT = '(o_o) $ '
process.env.EDITOR = 'edit'
process.env.VISUAL = 'visualedit'
process.env.ComSpec = 'some-com'
tap.test('basic windows sanity test', function (t) {
var osenv = require('../osenv.js')
var osenv = require('../osenv.js')
t.equal(osenv.user(),
process.env.USERDOMAIN + '\\' + process.env.USERNAME)
t.equal(osenv.home(), process.env.USERPROFILE)
t.equal(osenv.hostname(), process.env.COMPUTERNAME)
t.same(osenv.path(), process.env.Path.split(';'))
t.equal(osenv.prompt(), process.env.PROMPT)
t.equal(osenv.tmpdir(), process.env.TMPDIR)
// mildly evil, but it's for a test.
process.env.TMPDIR = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.tmpdir(), process.env.TMP)
process.env.TMP = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.tmpdir(), process.env.TEMP)
process.env.TEMP = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.tmpdir(), 'C:\\Users\\sirUser\\temp')
process.env.TEMP = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
osenv.home = function () { return null }
t.equal(osenv.tmpdir(), 'C:\\windows\\temp')
t.equal(osenv.editor(), 'edit')
process.env.EDITOR = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.editor(), 'visualedit')
process.env.VISUAL = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.editor(), 'notepad.exe')
t.equal(osenv.shell(), 'some-com')
process.env.ComSpec = ''
delete require.cache[require.resolve('../osenv.js')]
var osenv = require('../osenv.js')
t.equal(osenv.shell(), 'cmd')
t.end()
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment