pax_global_header00006660000000000000000000000064123407002770014514gustar00rootroot0000000000000052 comment=ff06d7d164fd985d579fe7298c984256dd9055ec osenv-0.1.0/000077500000000000000000000000001234070027700126445ustar00rootroot00000000000000osenv-0.1.0/.gitignore000066400000000000000000000001461234070027700146350ustar00rootroot00000000000000*.swp .*.swp .DS_Store *~ .project .settings npm-debug.log coverage.html .idea lib-cov node_modules osenv-0.1.0/LICENSE000066400000000000000000000024361234070027700136560ustar00rootroot00000000000000Copyright (c) Isaac Z. Schlueter ("Author") 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 AUTHOR 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 AUTHOR 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-0.1.0/README.md000066400000000000000000000026741234070027700141340ustar00rootroot00000000000000# 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'. osenv-0.1.0/osenv.js000066400000000000000000000035271234070027700143430ustar00rootroot00000000000000var isWindows = process.platform === 'win32' var path = require('path') var exec = require('child_process').exec var os = require('os') // 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 () { return os.tmpDir() }) 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' }) osenv-0.1.0/package.json000066400000000000000000000011361234070027700151330ustar00rootroot00000000000000{ "name": "osenv", "version": "0.1.0", "main": "osenv.js", "directories": { "test": "test" }, "dependencies": {}, "devDependencies": { "tap": "~0.4.9" }, "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 (http://blog.izs.me/)", "license": "BSD", "description": "Look up environment settings specific to different operating systems" } osenv-0.1.0/test/000077500000000000000000000000001234070027700136235ustar00rootroot00000000000000osenv-0.1.0/test/unix.js000066400000000000000000000043071234070027700151500ustar00rootroot00000000000000// 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') 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() }) osenv-0.1.0/test/windows.js000066400000000000000000000046601234070027700156610ustar00rootroot00000000000000// 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') 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') 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() })