Run.js

/* globals Wsh: false */
/* globals process: false */

// Shorthands
var util = Wsh.Util;
var CD = Wsh.Constants;
var cli = Wsh.Commander;
var ConfigStore = Wsh.ConfigStore;
var apL = Wsh.AppLauncher; // Shorthand

var isSolidArray = util.isSolidArray;

/**
 * Below are the APIs of CLI (Command Line Interface). Some inappropriate titles are used because they are generated by JsDoc.
 *
 * @namespace CLI
 */

// launchApp {{{
/**
 * Launch the specifying directory.
 *
 * @example
 * Usage: launchApp <app> [args...] [options]
 *
 * The command to launch an application
 *
 * Options:
 *   -V, --version          Output the version number
 *   -S, --shell            Wrap with CMD.EXE. (default: false)
 *   -W, --winStyle <style> See https://tuckn.net/docs/WshUtil/Wsh.Constants.windowStyles.html. (default: "activeDef")
 *   -A, --runsAsAdmin      Run as administrator (High WIL)
 *   -U, --runsAsUser       Run as user (Medium WIL)
 *   -L, --logger <val>     <level>/<transportation> (e.g. "warn/popup").  (default: "info/console")
 *   -R, --dry-run          No execute. Outputs the string of command. (default: false)
 *   -h, --help             Output usage information
 * @function launchApp
 * @memberof CLI
 */
cli.addProgram({
  command: 'launchApp <app> [args...]',
  description: 'The command to launch an application',
  version: '1.0.0',
  options: [
    ['-S, --shell', 'Wrap with CMD.EXE.', false],
    ['-W, --winStyle <style>', 'See https://tuckn.net/docs/WshUtil/Wsh.Constants.windowStyles.html.', 'activeDef'],
    ['-A, --runsAsAdmin', 'Run as administrator (High WIL)', undefined],
    ['-U, --runsAsUser', 'Run as user (Medium WIL)', undefined],
    ['-L, --logger <val>', '<level>/<transportation> (e.g. "warn/popup"). ', 'info/console'],
    ['-R, --dry-run', 'No execute. Outputs the string of command.']
  ],
  action: function (app, args, opt) {
    var runsAdmin = null;
    if (opt.runsAsAdmin === true) runsAdmin = true;
    else if (opt.runsAsUser === true) runsAdmin = false;

    var retVal = apL.launchAppUsingLog(app, args, {
      shell: opt.shell,
      winStyle: opt.winStyle,
      runsAdmin: runsAdmin,
      logger: opt.logger,
      isDryRun: opt.dryRun
    });

    if (opt.dryRun) console.log(retVal);

    process.exit(CD.runOk);
  }
}); // }}}

// schemaLaunch {{{
/**
 * Launch applications defined with a schema JSON.
 *
 * @example
 * Usage: schemaLaunch <taskName> [overwriteKey:val...] [options]
 *
 * The command to launch applications defined with a schema JSON
 *
 * Options:
 *   -V, --version          Output the version number
 *   -D, --dir-path <path>  The path name where the schema JSON is located. <Directory Path> or "cwd", "portable", "userProfile". Default: "cmd" is "%CD%\.wsh"
 *   -F, --file-name <name> A JSON file name. (default: "settings.json")
 *   -E, --encoding <name>  The JSON file encoding. (default: "utf-8")
 *   -N, --prop-name <name> A property name of the schema object. (default: "appLauncherSchema")
 *   -L, --logger <val>     <level>/<transportation>. e.g. "warn/popup".  (default: "info/console")
 *   -R, --dry-run          No execute. Outputs the string of command. (default: false)
 *   -h, --help             Output usage information
 * @function schemaLaunch
 * @memberof CLI
 */
cli.addProgram({
  command: 'schemaLaunch <taskName> [overwriteKey:val...]',
  description: 'The command to launch applications defined with a schema JSON',
  version: '1.0.0',
  options: [
    ['-D, --dir-path <path>', 'The path name where the schema JSON is located. <Directory Path> or "cwd", "portable", "userProfile". Default: "cmd" is "%CD%\\.wsh"'],
    ['-F, --file-name <name>', 'A JSON file name.', 'settings.json'],
    ['-E, --encoding <name>', 'The JSON file encoding.', CD.ado.charset.utf8],
    ['-N, --prop-name <name>', 'A property name of the schema object.', 'appLauncherSchema'],
    ['-L, --logger <val>', '<level>/<transportation>. e.g. "warn/popup". ', 'info/console'],
    ['-R, --dry-run', 'No execute. Outputs the string of command.']
  ],
  action: function (taskName, overwrites, opt) {
    var overwritesObj = {};
    if (isSolidArray(overwrites)) {
      overwrites.forEach(function (setStr) {
        var strs = setStr.split(':');
        if (strs.length > 1) overwritesObj[strs[0]] = strs.slice(1).join(':');
      });
    }

    var conf = new ConfigStore(opt.fileName, {
      dirPath: opt.dirPath,
      fileOptions: { encoding: opt.encoding }
    });
    var schema = conf.get(opt.propName);

    var retVal = apL.launchAppsUsingSchema(schema, taskName, {
      overwrites: overwritesObj,
      logger: opt.logger,
      showsResult: opt.hasResult,
      isDryRun: opt.dryRun
    });

    if (opt.dryRun) console.log(retVal);
    process.exit(CD.runOk);
  }
}); // }}}

cli.parse(process.argv);