-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: write script and args to temp file and run that #78
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict' | ||
|
||
// eslint-disable-next-line max-len | ||
// this code adapted from: https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ | ||
const cmd = (input) => { | ||
if (!input.length) { | ||
return '""' | ||
} | ||
|
||
let result | ||
if (!/[ \t\n\v"]/.test(input)) { | ||
result = input | ||
} else { | ||
result = '"' | ||
for (let i = 0; i <= input.length; ++i) { | ||
let slashCount = 0 | ||
while (input[i] === '\\') { | ||
++i | ||
++slashCount | ||
} | ||
|
||
if (i === input.length) { | ||
result += '\\'.repeat(slashCount * 2) | ||
break | ||
} | ||
|
||
if (input[i] === '"') { | ||
result += '\\'.repeat(slashCount * 2 + 1) | ||
result += input[i] | ||
} else { | ||
result += '\\'.repeat(slashCount) | ||
result += input[i] | ||
} | ||
} | ||
result += '"' | ||
} | ||
|
||
// and finally, prefix shell meta chars with a ^ | ||
result = result.replace(/[!^&()<>|"]/g, '^$&') | ||
// except for % which is escaped with another % | ||
result = result.replace(/%/g, '%%') | ||
|
||
return result | ||
} | ||
|
||
const sh = (input) => { | ||
if (!input.length) { | ||
return `''` | ||
} | ||
|
||
if (!/[\t\n\r "#$&'()*;<>?\\`|~]/.test(input)) { | ||
return input | ||
} | ||
|
||
// replace single quotes with '\'' and wrap the whole result in a fresh set of quotes | ||
const result = `'${input.replace(/'/g, `'\\''`)}'` | ||
// if the input string already had single quotes around it, clean those up | ||
.replace(/^(?:'')+(?!$)/, '') | ||
.replace(/\\'''/g, `\\'`) | ||
|
||
return result | ||
} | ||
|
||
module.exports = { | ||
cmd, | ||
sh, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const t = require('tap') | ||
|
||
const escape = require('../lib/escape.js') | ||
|
||
t.test('sh', (t) => { | ||
t.test('returns empty quotes when input is empty', async (t) => { | ||
const input = '' | ||
const output = escape.sh(input) | ||
t.equal(output, `''`, 'returned empty single quotes') | ||
}) | ||
|
||
t.test('returns plain string if quotes are not necessary', async (t) => { | ||
const input = 'test' | ||
const output = escape.sh(input) | ||
t.equal(output, input, 'returned plain string') | ||
}) | ||
|
||
t.test('wraps in single quotes if special character is present', async (t) => { | ||
const input = 'test words' | ||
const output = escape.sh(input) | ||
t.equal(output, `'test words'`, 'wrapped in single quotes') | ||
}) | ||
t.end() | ||
}) | ||
|
||
t.test('cmd', (t) => { | ||
t.test('returns empty quotes when input is empty', async (t) => { | ||
const input = '' | ||
const output = escape.cmd(input) | ||
t.equal(output, '""', 'returned empty double quotes') | ||
}) | ||
|
||
t.test('returns plain string if quotes are not necessary', async (t) => { | ||
const input = 'test' | ||
const output = escape.cmd(input) | ||
t.equal(output, input, 'returned plain string') | ||
}) | ||
|
||
t.test('wraps in double quotes when necessary', async (t) => { | ||
const input = 'test words' | ||
const output = escape.cmd(input) | ||
t.equal(output, '^"test words^"', 'wrapped in double quotes') | ||
}) | ||
|
||
t.test('doubles up backslashes at end of input', async (t) => { | ||
const input = 'one \\ two \\' | ||
const output = escape.cmd(input) | ||
t.equal(output, '^"one \\ two \\\\^"', 'doubles backslash at end of string') | ||
}) | ||
|
||
t.test('doubles up backslashes immediately before a double quote', async (t) => { | ||
const input = 'one \\"' | ||
const output = escape.cmd(input) | ||
t.equal(output, '^"one \\\\\\^"^"', 'doubles backslash before double quote') | ||
}) | ||
|
||
t.test('backslash escapes double quotes', async (t) => { | ||
const input = '"test"' | ||
const output = escape.cmd(input) | ||
t.equal(output, '^"\\^"test\\^"^"', 'escaped double quotes') | ||
}) | ||
t.end() | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanna be sure it's intentional that we're only looking for certain whitespace characters, and not using
\s
. Will approve assuming this is intentional.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was meant to be a list of each individual character that causes us to need single quotes to avoid expansion/keep the argument in one piece, the switch could likely be made but i felt like this was more explicit and clear