一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
78 行
1.3 KiB
TypeScript
78 行
1.3 KiB
TypeScript
interface SplitCommandStringOptions {
|
|
handleEscape?: boolean;
|
|
}
|
|
|
|
export default (command: string, options: SplitCommandStringOptions | null = null) => {
|
|
options = options || {};
|
|
if (!('handleEscape' in options)) {
|
|
options.handleEscape = true;
|
|
}
|
|
|
|
const args = [];
|
|
let state = 'start';
|
|
let current = '';
|
|
let quote = '"';
|
|
let escapeNext = false;
|
|
for (let i = 0; i < command.length; i++) {
|
|
const c = command[i];
|
|
|
|
if (state === 'quotes') {
|
|
if (c !== quote) {
|
|
current += c;
|
|
} else {
|
|
args.push(current);
|
|
current = '';
|
|
state = 'start';
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (escapeNext) {
|
|
current += c;
|
|
escapeNext = false;
|
|
continue;
|
|
}
|
|
|
|
if (c === '\\' && options.handleEscape) {
|
|
escapeNext = true;
|
|
continue;
|
|
}
|
|
|
|
if (c === '"' || c === '\'') {
|
|
state = 'quotes';
|
|
quote = c;
|
|
continue;
|
|
}
|
|
|
|
if (state === 'arg') {
|
|
if (c === ' ' || c === '\t') {
|
|
args.push(current);
|
|
current = '';
|
|
state = 'start';
|
|
} else {
|
|
current += c;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (c !== ' ' && c !== '\t') {
|
|
state = 'arg';
|
|
current += c;
|
|
}
|
|
}
|
|
|
|
if (state === 'quotes') {
|
|
throw new Error(`Unclosed quote in command line: ${command}`);
|
|
}
|
|
|
|
if (current !== '') {
|
|
args.push(current);
|
|
}
|
|
|
|
if (args.length <= 0) {
|
|
throw new Error('Empty command line');
|
|
}
|
|
|
|
return args;
|
|
};
|