-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
60 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 |
---|---|---|
@@ -1,27 +1,31 @@ | ||
'use strict'; | ||
|
||
const verifyStrings = strings => Array.isArray(strings) && strings.every(s => typeof s === 'string'); | ||
import {verifyStrings} from './utils.js'; | ||
|
||
const impl = (shellEscape, shell, options) => (strings, ...args) => { | ||
const result = []; | ||
const impl = | ||
(shellEscape, shell, options) => | ||
(strings, ...args) => { | ||
const result = []; | ||
|
||
for (let i = 0; i < strings.length; i++) { | ||
// process a string | ||
result.push(strings[i]); | ||
for (let i = 0; i < strings.length; i++) { | ||
// process a string | ||
result.push(strings[i]); | ||
|
||
// process an argument | ||
if (i >= args.length) continue; | ||
const arg = String(args[i]); | ||
if (!arg) continue; | ||
result.push(shellEscape(arg)); | ||
} | ||
// process an argument | ||
if (i >= args.length) continue; | ||
const arg = String(args[i]); | ||
if (!arg) continue; | ||
result.push(shellEscape(arg)); | ||
} | ||
|
||
return shell(result.join(''), options); | ||
}; | ||
return shell(result.join(''), options); | ||
}; | ||
|
||
const bqShell = (shellEscape, shell) => (strings, ...args) => { | ||
if (verifyStrings(strings)) return impl(shellEscape, shell, {})(strings, ...args); | ||
return impl(shellEscape, shell, strings); | ||
}; | ||
const bqShell = | ||
(shellEscape, shell) => | ||
(strings, ...args) => { | ||
if (verifyStrings(strings)) return impl(shellEscape, shell, {})(strings, ...args); | ||
return impl(shellEscape, shell, strings); | ||
}; | ||
|
||
export default bqShell; |
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 |
---|---|---|
@@ -1,61 +1,65 @@ | ||
'use strict'; | ||
|
||
const verifyStrings = strings => Array.isArray(strings) && strings.every(s => typeof s === 'string'); | ||
import {verifyStrings} from './utils.js'; | ||
|
||
const impl = (spawn, options) => (strings, ...args) => { | ||
const result = []; | ||
const impl = | ||
(spawn, options) => | ||
(strings, ...args) => { | ||
const result = []; | ||
|
||
let previousSpace = true; | ||
for (let i = 0; i < strings.length; i++) { | ||
// process a string | ||
let previousSpace = true; | ||
for (let i = 0; i < strings.length; i++) { | ||
// process a string | ||
|
||
let string = strings[i]; | ||
previousSpace ||= /^\s/.test(string); | ||
if (previousSpace) string = string.trimStart(); | ||
const lastSpace = /\s$/.test(string); | ||
if (lastSpace) string = string.trimEnd(); | ||
let string = strings[i]; | ||
previousSpace ||= /^\s/.test(string); | ||
if (previousSpace) string = string.trimStart(); | ||
const lastSpace = /\s$/.test(string); | ||
if (lastSpace) string = string.trimEnd(); | ||
|
||
let parts = string.split(/\s+/g).filter(part => part); | ||
if (parts.length) { | ||
if (!previousSpace) { | ||
if (result.length) { | ||
result[result.length - 1] += parts[0]; | ||
} else { | ||
result.push(parts[0]); | ||
let parts = string.split(/\s+/g).filter(part => part); | ||
if (parts.length) { | ||
if (!previousSpace) { | ||
if (result.length) { | ||
result[result.length - 1] += parts[0]; | ||
} else { | ||
result.push(parts[0]); | ||
} | ||
parts = parts.slice(1); | ||
} | ||
parts = parts.slice(1); | ||
result.push(...parts); | ||
previousSpace = lastSpace; | ||
} else { | ||
previousSpace ||= lastSpace; | ||
} | ||
result.push(...parts); | ||
previousSpace = lastSpace; | ||
} else { | ||
previousSpace ||= lastSpace; | ||
} | ||
|
||
// process an argument | ||
// process an argument | ||
|
||
if (i >= args.length) continue; | ||
if (i >= args.length) continue; | ||
|
||
const arg = String(args[i]); | ||
if (!arg) continue; | ||
const arg = String(args[i]); | ||
if (!arg) continue; | ||
|
||
if (previousSpace) { | ||
result.push(arg); | ||
} else { | ||
if (result.length) { | ||
result[result.length - 1] += arg; | ||
} else { | ||
if (previousSpace) { | ||
result.push(arg); | ||
} else { | ||
if (result.length) { | ||
result[result.length - 1] += arg; | ||
} else { | ||
result.push(arg); | ||
} | ||
} | ||
previousSpace = false; | ||
} | ||
previousSpace = false; | ||
} | ||
|
||
return spawn(result, options); | ||
}; | ||
return spawn(result, options); | ||
}; | ||
|
||
const bqSpawn = spawn => (strings, ...args) => { | ||
if (verifyStrings(strings)) return impl(spawn, {})(strings, ...args); | ||
return impl(spawn, strings); | ||
}; | ||
const bqSpawn = | ||
spawn => | ||
(strings, ...args) => { | ||
if (verifyStrings(strings)) return impl(spawn, {})(strings, ...args); | ||
return impl(spawn, strings); | ||
}; | ||
|
||
export default bqSpawn; |
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