-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototype: Spawning PHP sub-processes in Web Workers
- Loading branch information
Showing
12 changed files
with
270 additions
and
97 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
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
27 changes: 27 additions & 0 deletions
27
packages/php-wasm/util/src/lib/split-shell-command.spec.ts
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,27 @@ | ||
import { splitShellCommand } from './split-shell-command'; | ||
|
||
describe('splitShellCommand', () => { | ||
it('Should split a shell command into an array', () => { | ||
const command = | ||
'wp post create --post_title="Test post" --post_excerpt="Some content"'; | ||
const result = splitShellCommand(command); | ||
expect(result).toEqual([ | ||
'wp', | ||
'post', | ||
'create', | ||
'--post_title=Test post', | ||
'--post_excerpt=Some content', | ||
]); | ||
}); | ||
|
||
it('Should treat multiple spaces as a single space', () => { | ||
const command = 'ls --wordpress --playground --is-great'; | ||
const result = splitShellCommand(command); | ||
expect(result).toEqual([ | ||
'ls', | ||
'--wordpress', | ||
'--playground', | ||
'--is-great', | ||
]); | ||
}); | ||
}); |
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,49 @@ | ||
/** | ||
* Naive shell command parser. | ||
* Ensures that commands like `wp option set blogname "My blog name"` are split into | ||
* `['wp', 'option', 'set', 'blogname', 'My blog name']` instead of | ||
* `['wp', 'option', 'set', 'blogname', 'My', 'blog', 'name']`. | ||
* | ||
* @param command | ||
* @returns | ||
*/ | ||
export function splitShellCommand(command: string) { | ||
const MODE_NORMAL = 0; | ||
const MODE_IN_QUOTE = 1; | ||
|
||
let mode = MODE_NORMAL; | ||
let quote = ''; | ||
|
||
const parts: string[] = []; | ||
let currentPart = ''; | ||
for (let i = 0; i < command.length; i++) { | ||
const char = command[i]; | ||
if (mode === MODE_NORMAL) { | ||
if (char === '"' || char === "'") { | ||
mode = MODE_IN_QUOTE; | ||
quote = char; | ||
} else if (char.match(/\s/)) { | ||
if (currentPart) { | ||
parts.push(currentPart); | ||
} | ||
currentPart = ''; | ||
} else { | ||
currentPart += char; | ||
} | ||
} else if (mode === MODE_IN_QUOTE) { | ||
if (char === '\\') { | ||
i++; | ||
currentPart += command[i]; | ||
} else if (char === quote) { | ||
mode = MODE_NORMAL; | ||
quote = ''; | ||
} else { | ||
currentPart += char; | ||
} | ||
} | ||
} | ||
if (currentPart) { | ||
parts.push(currentPart); | ||
} | ||
return parts; | ||
} |
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
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.