pax_global_header00006660000000000000000000000064116244133350014514gustar00rootroot0000000000000052 comment=461ed811c8df02cc59935e626fe821c3af4942a0 node-bash-0.0.1/000077500000000000000000000000001162441333500133525ustar00rootroot00000000000000node-bash-0.0.1/.gitignore000066400000000000000000000000241162441333500153360ustar00rootroot00000000000000*.un~ /node_modules node-bash-0.0.1/License000066400000000000000000000021151162441333500146560ustar00rootroot00000000000000Copyright (c) 2011 Felix Geisendörfer (felix@debuggable.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-bash-0.0.1/Readme.md000066400000000000000000000021601162441333500150700ustar00rootroot00000000000000# bash Utilities for using bash from node.js. ## API ### bash.escape(parameter) Escapes the given `parameter` for bash. This is done by escaping all non alpha-numeric / dash characters with a backslash. Example: ```javascript > bash.escape('hello world'); 'Hello\\ World' ``` ### bash.args(options, prefix, suffix) Takes a list of `options` and turns them into an arguments string common to most *nix programs. Objects are turned into arguments: ```javascript > bash.args({a: 1, b: 2}, '--', '='); '--a=1 --b=2' ``` Values are escaped: ```javascript > bash.args({foo: 'hi you'}, '--', '='); '--foo=hi\\ you' ``` Array values turn into multiple arguments: ```javascript > bash.args({a: [1, 2]}, '--', '='); '--a=1 --a=2' ``` `null` / `true` values turn into flags: ```javascript > bash.args({a: true, b: null}, '--', '='); '--a --b' ``` Alternate suffix / prefix settings: ```javascript > bash.args({a: 1, b: 2}, '-', ' '); '-a 1 -b 2' ``` `options` can be an array as well: ```javascript > bash.args([{a: 1}, {a: 2, b: 3}] '-', ' '); '-a 1 -a 2 -b 3' ``` ## License This library is released under the MIT license. node-bash-0.0.1/index.js000066400000000000000000000000501162441333500150120ustar00rootroot00000000000000module.exports = require('./lib/bash'); node-bash-0.0.1/lib/000077500000000000000000000000001162441333500141205ustar00rootroot00000000000000node-bash-0.0.1/lib/bash.js000066400000000000000000000021471162441333500153770ustar00rootroot00000000000000var bash = exports; // Simple yet elegant shell escaping. This was proposed by shaver in IRC who // says he used it in an app that processed millions of commands this way. // At transloadit we have processed millions of commands this way now as well. bash.escape = function() { return Array.prototype .slice.call(arguments) .map(function(argument) { if (argument === undefined || argument === null) { argument = ''; } if (argument === '') { return "''"; } // Escape everything that is potentially unsafe with a backslash return (argument + '').replace(/([^0-9a-z-])/gi, '\\$1'); }) .join(' '); }; bash.args = function(options, prefix, suffix) { var args = []; options = [].concat(options); options.forEach(function(block) { for (var key in block) { [].concat(block[key]) .forEach(function(val) { if (val === null || val === true) { args.push(prefix + key); } else { args.push(prefix + key + suffix + bash.escape(val)); } }); } }); return args.join(' '); }; node-bash-0.0.1/package.json000066400000000000000000000006641162441333500156460ustar00rootroot00000000000000{ "author": "Felix Geisendörfer (http://debuggable.com/)", "name": "bash", "description": "Utilities for using bash from node.js.", "version": "0.0.1", "homepage": "https://github.com/felixge/node-bash", "repository": { "type": "git", "url": "git://github.com/felixge/node-bash.git" }, "main": "./index", "engines": { "node": "*" }, "dependencies": {}, "devDependencies": {} }node-bash-0.0.1/test/000077500000000000000000000000001162441333500143315ustar00rootroot00000000000000node-bash-0.0.1/test/common.js000066400000000000000000000002031162441333500161520ustar00rootroot00000000000000var common = exports; exports.assert = require('assert'); common.require = function(lib) { return require('../lib/' + lib); }; node-bash-0.0.1/test/test-bash.js000066400000000000000000000031531162441333500165630ustar00rootroot00000000000000var common = require('./common'); var assert = common.assert; var bash = common.require('bash'); (function testEscape() { assert.equal(bash.escape('hello _-world2'), 'hello\\ \\_-world2'); assert.equal(bash.escape('a'), 'a'); assert.equal(bash.escape('a', 'b'), 'a b'); assert.equal(bash.escape('a b'), 'a\\ b'); assert.equal(bash.escape(5), '5'); assert.equal(bash.escape(''), "''"); assert.equal(bash.escape(undefined), "''"); assert.equal(bash.escape(null), "''"); assert.equal(bash.escape(0), '0'); assert.equal(bash.escape('Geisendörfer'), 'Geisend\\örfer'); })(); (function testArgs() { (function testOneOption() { var r = bash.args({foo: 'bar'}, '--', '='); assert.equal(r, '--foo=bar'); })(); (function testTwoOptions() { var r = bash.args({a: 1, b: 2}, '--', '='); assert.equal(r, '--a=1 --b=2'); })(); (function testValueEscaping() { var r = bash.args({a: 'hey you'}, '--', '='); assert.equal(r, '--a=hey\\ you'); })(); (function testNullValueActsAsFlag() { var r = bash.args({a: null}, '--', '='); assert.equal(r, '--a'); })(); (function testTrueValueActsAsFlag() { var r = bash.args({a: true}, '--', '='); assert.equal(r, '--a'); })(); (function testArrayValueCreatesMultipleArgs() { var r = bash.args({a: [1, 2]}, '--', '='); assert.equal(r, '--a=1 --a=2'); })(); (function testArrayMap() { var r = bash.args([{a: 1}, {a: 2, b: 3}], '--', '='); assert.equal(r, '--a=1 --a=2 --b=3'); })(); (function testAlternatePrefixSuffix() { var r = bash.args({a: 1}, '-', ' '); assert.equal(r, '-a 1'); })(); })();