child process | test child process | | Search

The bashToOne function maps Bash shell script code to a single executable command, handling platform-specific line endings and command separators. It does this by splitting, modifying, and joining the input code according to the detected platform (Windows or Unix-like).

Run example

npm run import -- "convert bash to one liner"

convert bash to one liner

// read cmd template and execute each line?
var multiline = process.platform === 'win32' ? '^\n ' : '\\\n ';
var multicmd = process.platform === 'win32' ? ' ^\n; ' : ' \\\n&& ';

function bashToOne(code) {
    return code
        .split(/\n/ig)
        .map(l => '{EXEC} ' + l)
        .join('\n')
        .replace(/\\((\s*\n)+\{EXEC\})+\s*&&/ig, multicmd)
        .replace(/\\((\s*\n)+\{EXEC\})+/ig, multiline)
        .split(/\s*\{EXEC\}\s*/ig)
        .filter(r => r.trim() !== '')
        .join(multicmd);
}
module.exports = bashToOne;

What the code could have been:

// Import required modules
const { EOL } = require('os');

/**
 * Formats bash code to run each line sequentially.
 *
 * @param {string} code - The bash code to format.
 * @returns {string} The formatted bash code.
 */
function bashToOne(code) {
    // Split the code into individual lines
    const lines = code.split(EOL);
    
    // Map over the lines to wrap each with the EXEC placeholder
    const wrappedLines = lines.map(line => ` {EXEC} ${line.trim()}`).join(EOL);
    
    // Replace newline characters with multiline placeholders
    const multilinePlaceholder = EOL === '\r\n'? '^' + EOL +'' : '\\\n ';
    const wrappedLinesWithMultiline = wrappedLines.replace(/\n/g, multilinePlaceholder);
    
    // Replace `&&` with the multicmd placeholder
    const multicmd = EOL === '\r\n'?'^' + EOL + ';':'\\\n&& ';
    const formattedCode = wrappedLinesWithMultiline.replace(/(\s*\n)+&&/g, multicmd);
    
    // Split the code on the EXEC placeholder and filter out empty lines
    const commands = formattedCode.split(' {EXEC} ').filter(command => command.trim()!== '');
    
    // Join the commands with the multicmd placeholder and return the result
    return commands.join(multicmd);
}

module.exports = bashToOne;

Function: bashToOne

Description

Maps Bash shell script code to a single executable command.

Parameters

Returns

The converted code as a single executable command.

Logic

  1. Splits the input code into individual lines using the newline character (\n).
  2. Prepends each line with {EXEC} to indicate where the command should be executed.
  3. Replaces sequences of newline characters with either a Windows-style newline (\n) or a Windows-style command separator (;) depending on the platform.
  4. Replaces sequences of newline characters followed by {EXEC} with either a Windows-style newline or a Unix-style command separator, depending on the platform.
  5. Filters out empty lines and joins the remaining lines with the Unix-style command separator (&&).
  6. Returns the converted code as a single executable command.

Platform Detection

Uses the process.platform property to determine whether to use Windows-style or Unix-style line endings and command separators.