lib/helpers.js
function buildStartCmd (startAppOptions, apiLevel) {
  let cmd = ['am', 'start'];
  if (util.hasValue(startAppOptions.user)) {
    cmd.push('--user', startAppOptions.user);
  }
  cmd.push('-W', '-n', `${startAppOptions.pkg}/${startAppOptions.activity}`);
  if (startAppOptions.stopApp && apiLevel >= 15) {
    cmd.push('-S');
  }
  if (startAppOptions.action) {
    cmd.push('-a', startAppOptions.action);
  }
  if (startAppOptions.category) {
    cmd.push('-c', startAppOptions.category);
  }
  if (startAppOptions.flags) {
    cmd.push('-f', startAppOptions.flags);
  }
  if (startAppOptions.optionalIntentArguments) {
    // expect optionalIntentArguments to be a single string of the form:
    //     "-flag key"
    //     "-flag key value"
    // or a combination of these (e.g., "-flag1 key1 -flag2 key2 value2")

    // take a string and parse out the part before any spaces, and anything after
    // the first space
    let parseKeyValue = function (str) {
      str = str.trim();
      let space = str.indexOf(' ');
      if (space === -1) {
        return str.length ? [str] : [];
      } else {
        return [str.substring(0, space).trim(), str.substring(space + 1).trim()];
      }
    };

    // cycle through the optionalIntentArguments and pull out the arguments
    // add a space initially so flags can be distinguished from arguments that
    // have internal hyphens
    let optionalIntentArguments = ` ${startAppOptions.optionalIntentArguments}`;
    let re = / (-[^\s]+) (.+)/;
    while (true) { // eslint-disable-line no-constant-condition
      let args = re.exec(optionalIntentArguments);
      if (!args) {
        if (optionalIntentArguments.length) {
          // no more flags, so the remainder can be treated as 'key' or 'key value'
          cmd.push.apply(cmd, parseKeyValue(optionalIntentArguments));
        }
        // we are done
        break;
      }

      // take the flag and see if it is at the beginning of the string
      // if it is not, then it means we have been through already, and
      // what is before the flag is the argument for the previous flag
      let flag = args[1];
      let flagPos = optionalIntentArguments.indexOf(flag);
      if (flagPos !== 0) {
        let prevArgs = optionalIntentArguments.substring(0, flagPos);
        cmd.push.apply(cmd, parseKeyValue(prevArgs));
      }

      // add the flag, as there are no more earlier arguments
      cmd.push(flag);

      // make optionalIntentArguments hold the remainder
      optionalIntentArguments = args[2];
    }
  }
  return cmd;
}
function buildStartCmd (startAppOptions, apiLevel) {
  let cmd = ['am', 'start'];
  if (util.hasValue(startAppOptions.user)) {
    cmd.push('--user', startAppOptions.user);
  }
  cmd.push('-W', '-n', `${startAppOptions.pkg}/${startAppOptions.activity}`);
  if (startAppOptions.stopApp && apiLevel >= 15) {
    cmd.push('-S');
  }
  if (startAppOptions.action) {
    cmd.push('-a', startAppOptions.action);
  }
  if (startAppOptions.category) {
    cmd.push('-c', startAppOptions.category);
  }
  if (startAppOptions.flags) {
    cmd.push('-f', startAppOptions.flags);
  }
  if (startAppOptions.optionalIntentArguments) {
    // expect optionalIntentArguments to be a single string of the form:
    //     "-flag key"
    //     "-flag key value"
    // or a combination of these (e.g., "-flag1 key1 -flag2 key2 value2")

    // take a string and parse out the part before any spaces, and anything after
    // the first space
    let parseKeyValue = function (str) {
      str = str.trim();
      let space = str.indexOf(' ');
      if (space === -1) {
        return str.length ? [str] : [];
      } else {
        return [str.substring(0, space).trim(), str.substring(space + 1).trim()];
      }
    };

    // cycle through the optionalIntentArguments and pull out the arguments
    // add a space initially so flags can be distinguished from arguments that
    // have internal hyphens
    let optionalIntentArguments = ` ${startAppOptions.optionalIntentArguments}`;
    let re = / (-[^\s]+) (.+)/;
    while (true) { // eslint-disable-line no-constant-condition
      let args = re.exec(optionalIntentArguments);
      if (!args) {
        if (optionalIntentArguments.length) {
          // no more flags, so the remainder can be treated as 'key' or 'key value'
          cmd.push.apply(cmd, parseKeyValue(optionalIntentArguments));
        }
        // we are done
        break;
      }

      // take the flag and see if it is at the beginning of the string
      // if it is not, then it means we have been through already, and
      // what is before the flag is the argument for the previous flag
      let flag = args[1];
      let flagPos = optionalIntentArguments.indexOf(flag);
      if (flagPos !== 0) {
        let prevArgs = optionalIntentArguments.substring(0, flagPos);
        cmd.push.apply(cmd, parseKeyValue(prevArgs));
      }

      // add the flag, as there are no more earlier arguments
      cmd.push(flag);

      // make optionalIntentArguments hold the remainder
      optionalIntentArguments = args[2];
    }
  }
  return cmd;
}
function buildStartCmd ( startAppOptions , apiLevel ) { let cmd = [ 'am' , 'start' ] ; if ( util . hasValue ( startAppOptions . user ) ) { cmd . push ( '--user' , startAppOptions . user ) ; } cmd . push ( '-W' , '-n' , ` ${ startAppOptions . pkg } ${ startAppOptions . activity } ` ) ; if ( startAppOptions . stopApp && apiLevel >= 15 ) { cmd . push ( '-S' ) ; } if ( startAppOptions . action ) { cmd . push ( '-a' , startAppOptions . action ) ; } if ( startAppOptions . category ) { cmd . push ( '-c' , startAppOptions . category ) ; } if ( startAppOptions . flags ) { cmd . push ( '-f' , startAppOptions . flags ) ; } if ( startAppOptions . optionalIntentArguments ) { // expect optionalIntentArguments to be a single string of the form: //     "-flag key" //     "-flag key value" // or a combination of these (e.g., "-flag1 key1 -flag2 key2 value2") // take a string and parse out the part before any spaces, and anything after // the first space let parseKeyValue = function ( str ) { str = str . trim ( ) ; let space = str . indexOf ( ' ' ) ; if ( space === - 1 ) { return str . length ? [ str ] : [ ] ; } else { return [ str . substring ( 0 , space ) . trim ( ) , str . substring ( space + 1 ) . trim ( ) ] ; } } ; // cycle through the optionalIntentArguments and pull out the arguments // add a space initially so flags can be distinguished from arguments that // have internal hyphens let optionalIntentArguments = ` ${ startAppOptions . optionalIntentArguments } ` ; let re = /  (-[^\s]+) (.+) / ; while ( true ) { // eslint-disable-line no-constant-condition let args = re . exec ( optionalIntentArguments ) ; if ( ! args ) { if ( optionalIntentArguments . length ) { // no more flags, so the remainder can be treated as 'key' or 'key value' cmd . push . apply ( cmd , parseKeyValue ( optionalIntentArguments ) ) ; } // we are done break ; } // take the flag and see if it is at the beginning of the string // if it is not, then it means we have been through already, and // what is before the flag is the argument for the previous flag let flag = args [ 1 ] ; let flagPos = optionalIntentArguments . indexOf ( flag ) ; if ( flagPos !== 0 ) { let prevArgs = optionalIntentArguments . substring ( 0 , flagPos ) ; cmd . push . apply ( cmd , parseKeyValue ( prevArgs ) ) ; } // add the flag, as there are no more earlier arguments cmd . push ( flag ) ; // make optionalIntentArguments hold the remainder optionalIntentArguments = args [ 2 ] ; } } return cmd ; }
Builds command line representation for the given
application startup options

@param {StartAppOptions} startAppOptions - Application options mapping
@param {number} apiLevel - The actual OS API level
@returns {Array<String>} The actual command line array
Builds command line representation for the given application startup options