command-join
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -1,106 +0,111 @@ | ||
'use strict' | ||
const NEEDS_QUOTE = /[\s\\*\?\[\]`$()#<>|&;]/ | ||
"use strict"; | ||
const NEEDS_QUOTE = /[\s\\*\?\[\]`$()#<>|&;]/; | ||
function joinNix(arr) { | ||
let out = [] | ||
for (let command of arr) { | ||
// 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 = [] | ||
const out = []; | ||
for (const command of arr) { | ||
// if it is an empty string then append an empty string indicator | ||
if (command.length === 0) { | ||
out.push("''"); | ||
continue; | ||
} | ||
// whether we need a quote for the current block | ||
let needsQuote = false; | ||
// collection of quoted strings and escaped single quotes | ||
const blocks = []; | ||
// string collector | ||
let currentBlock = []; | ||
const 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 = []; | ||
}; | ||
for (const char of command) { | ||
if (char === "'") { // if single quote | ||
// flush the current block | ||
flushCurrentBlock(); | ||
// escape a single quote | ||
blocks.push("\\'"); | ||
continue; | ||
} | ||
if (NEEDS_QUOTE.test(char)) { | ||
needsQuote = true; | ||
} | ||
currentBlock.push(char); | ||
} | ||
// flush last block | ||
flushCurrentBlock(); | ||
const escapedCommand = blocks.join(''); | ||
out.push(escapedCommand); | ||
} | ||
for (let char of command) { | ||
if (char === "'") { // if single quote | ||
// flush the current block | ||
flushCurrentBlock() | ||
// escape a single quote | ||
blocks.push("\\'") | ||
continue | ||
} | ||
if (NEEDS_QUOTE.test(char)) { | ||
needsQuote = true | ||
} | ||
currentBlock.push(char) | ||
} | ||
// flush last block | ||
flushCurrentBlock() | ||
let escapedCommand = blocks.join('') | ||
out.push(escapedCommand) | ||
} | ||
return out.join(' ') | ||
return out.join(' '); | ||
} | ||
function joinWin(arr) { | ||
let out = [] | ||
for (let command of arr) { | ||
if (!/[\s\\"<>|&]/.test(command)) { | ||
out.push(command) | ||
continue | ||
const out = []; | ||
for (const command of arr) { | ||
// if it is an empty string then append an empty string indicator | ||
if (command.length === 0) { | ||
out.push('""'); | ||
continue; | ||
} | ||
if (!/[\s\\"<>|&]/.test(command)) { | ||
out.push(command); | ||
continue; | ||
} | ||
let backslashes = 0; | ||
// start escape quote | ||
const outString = ["\""]; | ||
const flushBackslashes = (n) => { | ||
outString.push("\\".repeat(n * backslashes)); | ||
backslashes = 0; | ||
}; | ||
for (let char of command) { | ||
// 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}`); | ||
out.push(escapedCommand); | ||
} | ||
let backslashes = 0 | ||
let c | ||
// start escape quote | ||
let outString = ["\""] | ||
let flushBackslashes = (n) => { | ||
outString.push("\\".repeat(n * backslashes)) | ||
backslashes = 0 | ||
return out.join(' '); | ||
} | ||
function commandJoin(arg) { | ||
if (typeof arg === 'string') { | ||
arg = [arg]; | ||
} | ||
for (let char of command) { | ||
// 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) | ||
} | ||
arg = Array.from(arg); | ||
if (process.platform === 'win32') { | ||
return joinWin(arg); | ||
} | ||
// 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}`) | ||
out.push(escapedCommand) | ||
} | ||
return out.join(' ') | ||
else { | ||
return joinNix(arg); | ||
} | ||
} | ||
function commandJoin(arg) { | ||
if (typeof arg === 'string') { | ||
arg = [arg] | ||
} | ||
arg = Array.from(arg) | ||
if (process.platform === 'win32') { | ||
return joinWin(arg) | ||
} | ||
else { | ||
return joinNix(arg) | ||
} | ||
} | ||
module.exports = commandJoin | ||
module.exports = commandJoin; | ||
//# sourceMappingURL=command-join.js.map |
MIT License | ||
Copyright (c) 2015-2017 Sean Marvi Oliver Genabe | ||
Copyright (c) 2015-2019 Sean Marvi Oliver Genabe | ||
@@ -5,0 +5,0 @@ 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: |
{ | ||
"name": "command-join", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Escape and join command-line arguments, cross-platform.", | ||
"main": "command-join.js", | ||
"scripts": { | ||
"test": "mocha test/*.test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/seangenabe/command-join.git" | ||
}, | ||
"keywords": [ | ||
"shell", | ||
"argv", | ||
"cli", | ||
"command", | ||
"escape", | ||
"join", | ||
"cli", | ||
"argv" | ||
"shell" | ||
], | ||
"author": "Sean Genabe <seangenabe@outlook.com>", | ||
"license": "MIT", | ||
"homepage": "https://github.com/seangenabe/command-join#readme", | ||
"bugs": { | ||
"url": "https://github.com/seangenabe/command-join/issues" | ||
}, | ||
"homepage": "https://github.com/seangenabe/command-join#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/seangenabe/command-join.git" | ||
}, | ||
"license": "MIT", | ||
"author": "Sean Genabe <seangenabe@outlook.com>", | ||
"main": "command-join.js", | ||
"scripts": { | ||
"build": "tsc", | ||
"postinstall": "npx -p @seangenabe/tnx tnx || exit 0", | ||
"prepublishOnly": "npm test", | ||
"pretest": "npm run build", | ||
"test": "node test/index.test.js", | ||
"watch": "tsc -w" | ||
}, | ||
"dependencies": { | ||
"@improved/node": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.4.1", | ||
"mocha": "^3.1.0" | ||
"@types/node": "^12.0.2", | ||
"t0": "^1.0.0", | ||
"typescript": "^3.4.5" | ||
}, | ||
@@ -31,0 +40,0 @@ "engines": { |
@@ -6,7 +6,8 @@ # command-join | ||
[![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) | ||
[![Build Status](https://img.shields.io/travis/seangenabe/command-join/master.svg?style=flat-square)](https://travis-ci.org/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/) | ||
If you like this package, be sure to star its repo, and please consider [donating](https://seangenabe.netlify.com/donate). | ||
## Usage | ||
@@ -34,5 +35,1 @@ | ||
See the tests for more convoluted examples. | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
10221
1
1
3
7
112
34
1
+ Added@improved/node@^1.0.0
+ Added@improved/node@1.1.1(transitive)