-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: write scripts to a file and run that instead of passing scripts…
… as a single string
- Loading branch information
Showing
6 changed files
with
229 additions
and
18 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict' | ||
|
||
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() | ||
}) |
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
Oops, something went wrong.