magica
Version:
ImageMagick for browser and Node.js, easy setup, high level API and Command Line Interface, including WASM binary for an easy setup.
90 lines • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const misc_utils_of_mine_generic_1 = require("misc-utils-of-mine-generic");
function processCommand(command) {
if (typeof command !== 'string') {
return command;
}
return misc_utils_of_mine_generic_1.checkThrow(cliToArrayOne(command), 'Cannot create a command array from given string ' + command);
}
exports.processCommand = processCommand;
/**
* Generates a valid command line command from given `string[]` command. Works with a single command.
*/
function arrayToCliOne(command) {
return command
// if it contain spaces
.map(c => (c.trim().match(/\s/)) ? `'${c}'` : c)
// escape parenthesis
.map(c => c.trim() === '(' ? '\\(' : c.trim() === ')' ? '\\)' : c)
.join(' ');
}
exports.arrayToCliOne = arrayToCliOne;
/**
* Generates a valid command line string from given `string[]` that is compatible with {@link call}. Works with multiple
* commands by separating them with new lines and support comand splitting in new lines using `\`.
* See {@link ExecuteCommand} for more information.
*/
function arrayToCli(command) {
const cmd = typeof command[0] === 'string' ? [command] : command;
return cmd.map(arrayToCliOne).join('\n');
}
exports.arrayToCli = arrayToCli;
/**
* Generates a command in the form of array of strings, compatible with {@link call} from given command line string . The string must contain only one command (no newlines).
*/
function cliToArrayOne(cliCommand) {
if (cliCommand.trim().startsWith('#')) {
return undefined;
}
let inString = false;
const spaceIndexes = [0];
for (let index = 0; index < cliCommand.length; index++) {
const c = cliCommand[index];
if (c.match(/[ ]/im) && !inString) {
spaceIndexes.push(index);
}
if (c === `'`) {
inString = !inString;
}
}
spaceIndexes.push(cliCommand.length);
const command = spaceIndexes
.map((spaceIndex, i) => cliCommand.substring(i === 0 ? 0 : spaceIndexes[i - 1], spaceIndexes[i]).replace(/^[ ]+/, '').replace(/[ ]+$/, ''))
.filter(s => !!s)
// remove quotes
.map(s => s.startsWith(`'`) ? s.substring(1, s.length) : s)
.map(s => s.endsWith(`'`) ? s.substring(0, s.length - 1) : s)
// unescape parenthesis
.map(s => s === `\\(` ? `(` : s === `\\)` ? `)` : s)
.map(s => s.replace(/\\n/g, '\n')); // so `%w\\n` is transformed to `%w\n' - we cant have new lines because of cliToArray split('\n') - so user must escape it and here we unescape
return command;
}
/**
* Generates a command in the form of `string[][]` that is compatible with {@link call} from given command line string.
* This works for strings containing multiple commands in different lines. and also respect `\` character for continue the same
* command in a new line. See {@link ExecuteCommand} for more information.
*/
function cliToArray(cliCommand) {
const lines = cliCommand.split('\n')
.map(s => s.trim())
.map(cliToArrayOne)
.filter(misc_utils_of_mine_generic_1.notUndefined)
.filter(a => a.length);
const result = [];
let currentCommand = [];
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;
}
exports.cliToArray = cliToArray;
//# sourceMappingURL=command.js.map