Skip to content

Commit

Permalink
refactor: Switch parsing and composing
Browse files Browse the repository at this point in the history
Refactor the switch parsing and composing. The main function uses ES6,
is now called `toChildProcessArgs()` (which is more accurate). It also
relies on hardcoded values for specific switches (see top of the file).
  • Loading branch information
q2s2t committed Aug 21, 2018
1 parent aeec3c7 commit 2a3bace
Showing 1 changed file with 70 additions and 51 deletions.
121 changes: 70 additions & 51 deletions util/switches.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,87 @@
'use strict';
/**
* Switches that can be toggled on or off (boolean switches). Default values
* are based on the 7-zip documentation.
*/
const swDefaultBool = {
sdel: false, // Delete files after compression
spl: false, // Set Large Pages mode
sni: false, // Store NT security information
so: false, // Write data to stdout
spd: false, // Disable wildcard matching for file names
spe: false, // Eliminate duplication of root folder for extract command
spf: false, // Use fully qualified file paths
ssw: false, // Compress files open for writing
stl: false, // Set archive timestamp from the most recently modified file
y: true // Assume Yes on all queries
}

/**
* Switches that ca be toggles on or of. Their default values changes according
* to the context (command, platform, ...).
*/
const swContextBool = {
sns: undefined, // Store NTFS alternate Streams
ssc: undefined // Set Sensitive Case mode
}

/**
* Switches that can be applied multiple times
*/
const swRepeating = {
ai: undefined,
ax: undefined,
i: undefined,
x: undefined
}

/**
* Transform an object of options into an array that can be passed to the
* spawned child process.
* @param {Object} switches An object of options
* @return {array} Array to pass to the `run` function.
*/
module.exports = function (switches) {

// Default value for switches
switches = switches || {};

var a = [];
// Set default values of boolean switches
switches.so = (switches.so === true) ? true : false;
switches.spl = (switches.spl === true) ? true : false;
switches.ssc = (switches.ssc === false) ? false : true ;
switches.ssw = (switches.ssw === true) ? true : false;
switches.y = (switches.y === false) ? false : true ;
export default function toChildProcessArgs (switches = swDefaultBool) {
const swDefaultBoolKeys = Object.keys(swDefaultBool)
const swContextBoolKeys = Object.keys(swContextBool)
const swRepeatingKeys = Object.keys(swRepeating)
let cmdArgs = []

var s;
/*jshint forin:false*/
for (s in switches) {

// Switches that are set or not. Just add them to the array if they are
// present. Differ the `ssc` switch treatment to later in the function.
if (switches[s] === true && s !== 'ssc') {
a.push('-' + s);
Object.entries(switches).forEach(([swName, swVal]) => {
// Handle wildcards
if (swName === 'wildcards') {
cmdArgs.unshift(swVal)
return
}

// Switches with a value. Detect if the value contains a space. If it does
// wrap the value with double quotes. Else just add the switch and its value
// to the string. Doubles quotes are used for parsing with a RegExp later.
if (typeof switches[s] !== 'boolean') {

// Special treatment for wilcards
if (s === 'wildcards') {
a.unshift(switches.wildcards);
}

// Allow raw switches to be added to the command, repeating switches like
// -i is not possible otherwise.
else if (s === 'raw') {
switches.raw.forEach(function (rawValue) {
a.push(rawValue);
});
}

else if (switches[s].indexOf(' ') === -1) {
a.push('-' + s + switches[s]);
}

else {
a.push('-' + s + '"' + switches[s] + '"');
// Handle boolean switches
if (swDefaultBoolKeys.includes(swName)) {
if (swVal === true) {
cmdArgs.push(`-${swName}`)
}
return
}

// Special treatment for `-ssc`
if (s === 'ssc') {
a.push((switches.ssc === true) ? '-ssc' : '-ssc-');
// Handle context boolean switches
if (swContextBoolKeys.includes(swName)) {
let swSuffix = (swVal === true) ? '' : '-'
cmdArgs.push(`-${swName}${swSuffix}`)
return
}

}
// Handle repeating switches. They can be string or array
if (swRepeatingKeys.includes(swName)) {
if (typeof swVal === 'string') {
swVal = [swVal]
}
swVal.forEach(([swRepeatingString]) => {
cmdArgs.push(`-${swName}${swRepeatingString}`)
})
return
}

return a;
// Handle switches with arguments
cmdArgs.push(`-${swName}${swVal}`)
})

};
return cmdArgs
}

0 comments on commit 2a3bace

Please sign in to comment.