Skip to content

Commit

Permalink
util/cli support for multi line comands
Browse files Browse the repository at this point in the history
  • Loading branch information
cancerberoSgx committed Nov 16, 2018
1 parent 17b2165 commit 775021f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion samples/interactive-execute-context/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class App extends React.Component<AppProps, AppState> {

state: AppState = {
commandString: 'identify rose:',
commandArray: '["identify", "rose:"]',
commandArray: JSON.stringify(cliToArray('identify rose:')),
jsonError: '',
files: [],
imgSrcs: [],
Expand Down
4 changes: 2 additions & 2 deletions spec/callSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { blobToString, buildInputFile, Call, extractInfo, call } from '../src'
export default describe('call', () => {

describe('call', () => {

it('should resolve with stderr on errors and exitCode !=0', async done => {
const result = await call([], ['convert', 'nonExistent.png', 'foo.gif'])
expect(result.stderr.join('\n')).toContain(`'nonExistent.png': No such file or directory`)
expect(result.exitCode).not.toBe(0)
done()
})
})

it('should resolve with stdout when there\'s one', async done => {
const result = await call([], ['identify', 'rose:'])
Expand Down
38 changes: 35 additions & 3 deletions spec/util/cliSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,49 @@ export default describe('util/cli', () => {

it('should support simple commands', () => {
expect(cliToArray(`convert foo.png -rotate 90 bar.gif`))
.toEqual(['convert', 'foo.png', '-rotate', '90', 'bar.gif'])
.toEqual([['convert', 'foo.png', '-rotate', '90', 'bar.gif']])
})

it('should support commands with quoted arguments', () => {
expect(cliToArray(`convert 'my picture.png' -rotate 90 'output image.png'`))
.toEqual(['convert', 'my picture.png', '-rotate', '90', 'output image.png'])
.toEqual([['convert', 'my picture.png', '-rotate', '90', 'output image.png']])
})

it('should support escaped parenthesis', () => {
expect(cliToArray(`convert foo.png \\( +clone -channel R -fx B \\) +swap -channel B -fx v.R bar.gif`))
.toEqual(['convert', 'foo.png', '(', '+clone', '-channel', 'R', '-fx', 'B', ')', '+swap', '-channel', 'B', '-fx', 'v.R', 'bar.gif'])
.toEqual([['convert', 'foo.png', '(', '+clone', '-channel', 'R', '-fx', 'B', ')', '+swap', '-channel', 'B', '-fx', 'v.R', 'bar.gif']])
})

it('should support multiple commands separated by new line', () => {
expect(cliToArray(`
convert rose: -sharpen 0x1 reconstruct.jpg
compare rose: reconstruct.jpg difference.png
compare -compose src rose: reconstruct.jpg difference.png
`))
.toEqual([
['convert', 'rose:', '-sharpen', '0x1', 'reconstruct.jpg'],
['compare', 'rose:', 'reconstruct.jpg', 'difference.png'],
['compare', '-compose', 'src', 'rose:', 'reconstruct.jpg', 'difference.png'],
])

})

it('should support multiple commands separated by new line and respect the \\ character to continue the same command in another line', () => {
console.log(cliToArray(`
convert foo.png \\( +clone -channel R -fx B \\) \\
+swap -channel B -fx v.R bar.gif
convert bar.gif -resize 50% out.tiff
`))

expect(cliToArray(`
convert foo.png \\( +clone -channel R -fx B \\) \\
+swap -channel B -fx v.R bar.gif
convert bar.gif -resize 50% out.tiff
`))
.toEqual([
['convert', 'foo.png', '(', '+clone', '-channel', 'R', '-fx', 'B', ')', '+swap', '-channel', 'B', '-fx', 'v.R', 'bar.gif'],
['convert', 'bar.gif', '-resize', '50%', 'out.tiff']])

})

})
Expand Down
34 changes: 30 additions & 4 deletions src/util/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ export function arrayToCli(command: Command): string {
.join(' ')
}

/** generates a valie Call/execute string[] command from given command line command */
export function cliToArray(cliCommand: string): Command {
/** generates a valid Call/execute string[] command from given command line command.
* This works only for a single command
*/
export function cliToArrayOne(cliCommand: string): Command {
let inString = false
const spaceIndexes = [0]
for (let index = 0; index < cliCommand.length; index++) {
Expand All @@ -43,11 +45,35 @@ export function cliToArray(cliCommand: string): Command {
return command
}

/** generates a valid Call/execute string[] command from given command line command.
* This works for strings containing multiple commands in different lines.
* TODO: respect '\' character for continue the same command in a new line
*/
export function cliToArray(cliCommand: string): Command[] {
const lines = cliCommand.split('\n')
.map(s => s.trim()).map(cliToArrayOne)
.filter(a => a && a.length)
const result = []
let currentCommand: Command = []
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
if (line[line.length - 1] !== '\\') {
currentCommand = currentCommand.concat(line)
result.push(currentCommand)
currentCommand = []
}
else {
currentCommand = currentCommand.concat(line.slice(0, line.length - 1))
}
}
return result
}

export function asCommand(c: ExecuteCommand): Command[] {
if (typeof c === 'string') { return asCommand([c]) }
if (typeof c === 'string') { return asCommand([c]) }
if (!c[0]) { return [] }
if (typeof c[0] === 'string') {
return (c as string[]) .map((subCommand: string) => cliToArray(subCommand))
return (c as string[]).map((subCommand: string) => cliToArrayOne(subCommand))
}
return c as Command[]
}

0 comments on commit 775021f

Please sign in to comment.