[Pkg-javascript-commits] [node-command-join] 01/07: Import Upstream version 1.1.1

Paolo Greppi paolog-guest at moszumanska.debian.org
Sat Dec 24 08:54:38 UTC 2016


This is an automated email from the git hooks/post-receive script.

paolog-guest pushed a commit to branch master
in repository node-command-join.

commit 8c5791a9026d268bfcf9aba08c7621e9b5413f2e
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date:   Sat Dec 24 08:22:20 2016 +0000

    Import Upstream version 1.1.1
---
 .babelrc         |   8 ++++
 .gitignore       |   5 +++
 .travis.yml      |   6 +++
 appveyor.yml     |  14 +++++++
 command-join.js  | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 license.md       |   9 +++++
 package.json     |  49 +++++++++++++++++++++++++
 readme.md        |  36 ++++++++++++++++++
 test/exec.js     |  19 ++++++++++
 test/nix.test.js |  29 +++++++++++++++
 test/win.test.js |  29 +++++++++++++++
 11 files changed, 313 insertions(+)

diff --git a/.babelrc b/.babelrc
new file mode 100644
index 0000000..ef29978
--- /dev/null
+++ b/.babelrc
@@ -0,0 +1,8 @@
+{
+  "plugins": [
+    "transform-es2015-template-literals",
+    "transform-es2015-arrow-functions",
+    "transform-es2015-constants",
+    "transform-es2015-block-scoping"
+  ]
+}
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d557d73
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+node_modules/
+*.log
+*.log.*
+dist/
+test-es5/
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..91b317c
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,6 @@
+language: node_js
+node_js:
+  - "0.10"
+  - "0.12"
+  - "4"
+  - "5"
diff --git a/appveyor.yml b/appveyor.yml
new file mode 100644
index 0000000..2c2079b
--- /dev/null
+++ b/appveyor.yml
@@ -0,0 +1,14 @@
+environment:
+  matrix:
+    - nodejs_version: "0.10"
+    - nodejs_version: "0.12"
+    - nodejs_version: "4"
+    - nodejs_version: "5"
+install:
+  - ps: Install-Product node $env:nodejs_version x64
+  - npm install
+test_script:
+  - node --version
+  - npm --version
+  - npm test
+build: off
diff --git a/command-join.js b/command-join.js
new file mode 100644
index 0000000..f94f2f0
--- /dev/null
+++ b/command-join.js
@@ -0,0 +1,109 @@
+'use strict'
+
+const NEEDS_QUOTE = /[\s\\*\?\[\]`$()#<>|&;]/
+const arrayFrom = require("array-from")
+const repeat = require("repeat-string")
+
+function joinNix(arr) {
+  let out
+  out = arr.map(command => {
+    // convert to string
+    command = String(command)
+    // whether we need a quote for the current block
+    let needsQuote = false
+    // collection of quoted strings and escaped single quotes
+    let blocks = []
+    // string collector
+    let currentBlock = []
+    let flushCurrentBlock = () => {
+      // skip if we don't have anything collected as the current block
+      if (!currentBlock.length) { return }
+      if (needsQuote) {
+        currentBlock.unshift("'")
+        currentBlock.push("'")
+      }
+      blocks.push(currentBlock.join(''))
+      currentBlock = []
+    }
+    command.split('').forEach(char => {
+      if (char === "'") { // if single quote
+        // flush the current block
+        flushCurrentBlock()
+        // escape a single quote
+        blocks.push("\\'")
+        return
+      }
+      if (NEEDS_QUOTE.test(char)) {
+        needsQuote = true
+      }
+      currentBlock.push(char)
+    })
+    // flush last block
+    flushCurrentBlock()
+    let escapedCommand = blocks.join('')
+    return escapedCommand
+  })
+  return out.join(' ')
+}
+
+function joinWin(arr) {
+  let out
+
+  out = arr.map(command => {
+    if (!/[\s\\"<>|&]/.test(command)) {
+      return command
+    }
+    let backslashes = 0
+    let c
+    // start escape quote
+    let outString = ["\""]
+    let flushBackslashes = (n) => {
+      outString.push(repeat("\\", n * backslashes))
+      backslashes = 0
+    }
+    command.split('').forEach(char => {
+      // if char is a backslash
+      if (char === "\\") {
+        // enqueue backslash
+        backslashes++
+      }
+      // if char is a double quote
+      else if (char === "\"") {
+        // doubly end backslash sequence if any
+        flushBackslashes(2)
+        // push string \" to escape quote
+        outString.push("\\\"")
+      }
+      else {
+        // singly end backslash sequence if any
+        flushBackslashes(1)
+        outString.push(char)
+      }
+    })
+    // flush any remaining backslashes
+    flushBackslashes(2)
+    // end escape quote
+    outString.push("\"")
+    let escapedCommand = outString.join('')
+    // escape some special characters
+    escapedCommand = escapedCommand.replace(/[&|<>;%^]/g, match => `^${match}`)
+    return escapedCommand
+  })
+
+  return out.join(' ')
+}
+
+function commandJoin(arg) {
+  if (typeof arg === 'string') {
+    arg = [arg]
+  }
+  arg = arrayFrom(arg)
+  if (process.platform === 'win32') {
+    return joinWin(arg)
+  }
+  else {
+    return joinNix(arg)
+  }
+}
+
+module.exports = commandJoin
diff --git a/license.md b/license.md
new file mode 100644
index 0000000..cd5dca4
--- /dev/null
+++ b/license.md
@@ -0,0 +1,9 @@
+MIT License
+
+Copyright (c) 2015 Sean Marvi Oliver Genabe
+
+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.
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..48d6931
--- /dev/null
+++ b/package.json
@@ -0,0 +1,49 @@
+{
+  "name": "command-join",
+  "version": "1.1.1",
+  "description": "Escape and join command-line arguments, cross-platform.",
+  "main": "dist/command-join.js",
+  "files": [
+    "dist"
+  ],
+  "scripts": {
+    "build": "babel command-join.js --out-dir dist && babel test --out-dir test-es5",
+    "test": "npm run build && mocha test-es5/*.test.js",
+    "prepublish": "npm run build"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/seangenabe/command-join.git"
+  },
+  "keywords": [
+    "shell",
+    "command",
+    "escape",
+    "join",
+    "cli",
+    "argv"
+  ],
+  "author": "Sean Genabe <seangenabe at outlook.com>",
+  "license": "MIT",
+  "bugs": {
+    "url": "https://github.com/seangenabe/command-join/issues"
+  },
+  "homepage": "https://github.com/seangenabe/command-join#readme",
+  "devDependencies": {
+    "babel-cli": "^6.14.0",
+    "babel-core": "^6.14.0",
+    "babel-plugin-transform-es2015-arrow-functions": "^6.8.0",
+    "babel-plugin-transform-es2015-block-scoping": "^6.15.0",
+    "babel-plugin-transform-es2015-constants": "^6.1.4",
+    "babel-plugin-transform-es2015-template-literals": "^6.8.0",
+    "chai": "^3.4.1",
+    "mocha": "^3.1.0"
+  },
+  "engines": {
+    "node": ">=0.10"
+  },
+  "dependencies": {
+    "array-from": "^2.1.1",
+    "repeat-string": "^1.5.4"
+  }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..66a30d6
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,36 @@
+# command-join
+
+Escape command-line arguments, cross-platform.
+
+[![npm](https://img.shields.io/npm/v/command-join.svg?style=flat-square)](https://www.npmjs.com/package/command-join)
+[![Travis Build Status](https://img.shields.io/travis/seangenabe/command-join/master.svg?label=travis&style=flat-square)](https://travis-ci.org/seangenabe/command-join)
+[![AppVeyor Build Status](https://img.shields.io/appveyor/ci/seangenabe/command-join.svg?label=appveyor&style=flat-square)](https://ci.appveyor.com/project/seangenabe/command-join)
+[![devDependency Status](https://img.shields.io/david/dev/seangenabe/command-join.svg?style=flat-square)](https://david-dm.org/seangenabe/command-join#info=devDependencies)
+[![node](https://img.shields.io/node/v/command-join.svg?style=flat-square)](https://nodejs.org/en/download/)
+
+## Usage
+
+```javascript
+const commandJoin = require('command-join')
+```
+
+### `commandJoin(arg: Array|String): String`
+
+Escapes each command-line argument and joins them into a string that can then be executed, e.g. via `child_process.exec`.
+
+If a string is passed, an array containing the string will instead be processed.
+
+**Example**
+
+```javascript
+let command = commandJoin(['a', "b\\", "'c"])
+command
+// output on Windows: a "b\\" 'c
+// output on Linux: a 'b\' \'c
+```
+
+See the tests for more convoluted examples.
+
+## License
+
+MIT
diff --git a/test/exec.js b/test/exec.js
new file mode 100644
index 0000000..e6e20f0
--- /dev/null
+++ b/test/exec.js
@@ -0,0 +1,19 @@
+'use strict'
+
+const join = require('../')
+const ChildProcess = require('child_process')
+
+if (require.main === module) {
+  console.log(process.argv.slice(2).join('\n'))
+}
+else if (typeof ChildProcess.execSync === 'function') {
+  module.exports = function exec(args) {
+    args = args.slice()
+    args.unshift(__filename)
+    let joined = join(args)
+    let command = `node ${joined}`
+    let out = ChildProcess.execSync(command)
+    // trim to remove extra \n appended by cmd
+    return String(out).trim()
+  }
+}
diff --git a/test/nix.test.js b/test/nix.test.js
new file mode 100644
index 0000000..53358a2
--- /dev/null
+++ b/test/nix.test.js
@@ -0,0 +1,29 @@
+'use strict'
+
+const join = require('../')
+const expect = require('chai').expect
+const exec = require('./exec')
+
+describe('nix tests', function() {
+
+  if (process.platform === 'win32') { return }
+
+  const commands1 = ['a', 'b', 'c', 'd.txt']
+  const commands2 = ["'a", "b'c", "d\\e", "f\\\\''g", "h i", "' j '", "k?",
+    "l[", "m]", "`n`", "$o", "$(p)", "#q", "r & s", "t < u", "v > w", "x'"]
+
+  it('should join normally', function() {
+    expect(join(commands1)).to.equal('a b c d.txt')
+  })
+
+  if (typeof exec === 'function') {
+    it('should exec normally', function() {
+        expect(exec(commands1)).to.equal(commands1.join('\n'))
+    })
+
+    it('should exec string with single quotes', function() {
+        expect(exec(commands2)).to.equal(commands2.join('\n'))
+    })
+  }
+
+})
diff --git a/test/win.test.js b/test/win.test.js
new file mode 100644
index 0000000..47a09eb
--- /dev/null
+++ b/test/win.test.js
@@ -0,0 +1,29 @@
+'use strict'
+
+const join = require('../')
+const expect = require('chai').expect
+const exec = require('./exec')
+
+describe('win32 tests', function() {
+
+  if (process.platform !== 'win32') { return }
+
+  const commands1 = ['a', 'b.txt', 'c/d', "'e'"]
+  const commands2 = ['a\\b', 'a b', 'a\tb', 'a\\\\\\b', 'a"b"c"d"\\"\\',
+    'a&<>|;', '%c%', '%D']
+
+  it('should join normally', function() {
+    expect(join(commands1)).to.equal("a b.txt c/d 'e'")
+  })
+
+  if (typeof exec === 'function') {
+    it('should exec normally', function() {
+        expect(exec(commands1)).to.equal(commands1.join('\n'))
+    })
+
+    it('should exec to escape special characters', function() {
+        expect(exec(commands2)).to.equal(commands2.join('\n'))
+    })
+  }
+
+})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-command-join.git



More information about the Pkg-javascript-commits mailing list