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 net = Wsh.Net;
var smbcn = Wsh.SmbConnector; // 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
 */

// connect {{{
/**
 * Connects the specifying resource.
 *
 * @example
 * Usage: connect <compName> [options]
 *
 * The command to connect a Windows to the resource
 *
 * Options:
 *   -V, --version        Output the version number
 *   -S, --shared <name>  A shared name of the resource. Default: "IPC$"
 *   -D, --domain <name>  A domain of the user
 *   -U, --user <name>    A user name to log on
 *   -P, --pwd <password> A password of the user
 *   -L, --logger <val>   <level>/<transportation> (e.g. "warn/popup").  (default: "info/console")
 *   -H, --has-result     Show a result(net use) (default: false)
 *   -R, --dry-run        No execute. Outputs the string of command. (default: false)
 *   -h, --help           Output usage information
 * @function connect
 * @memberof CLI
 */
cli.addProgram({
  command: 'connect <compName>',
  description: 'The command to connect a Windows to the resource',
  version: '1.0.0',
  options: [
    ['-S, --shared <name>', 'A shared name of the resource. Default: "IPC$"'],
    ['-D, --domain <name>', 'A domain of the user'],
    ['-U, --user <name>', 'A user name to log on'],
    ['-P, --pwd <password>', 'A password of the user'],
    ['-L, --logger <val>', '<level>/<transportation> (e.g. "warn/popup"). ', 'info/console'],
    ['-H, --has-result', 'Show a result(net use)'],
    ['-R, --dry-run', 'No execute. Outputs the string of command.']
  ],
  action: function (compName, opt) {
    var retVal = smbcn.connectSyncSurelyUsingLog(
      compName,
      opt.shared,
      opt.domain,
      opt.user,
      opt.pwd,
      {
        logger: opt.logger,
        showsResult: opt.hasResult,
        isDryRun: opt.dryRun
      }
    );

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

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

// disconnect {{{
/**
 * Disconnects the specifying resource.
 *
 * @example
 * Usage: disconnect [compName] [options]
 *
 * The command to disconnect a Windows from the resource
 *
 * Options:
 *   -V, --version       Output the version number
 *   -S, --shared <name> A shared name of the resource. Default: "IPC$"
 *   -H, --has-result    Show a result(net use) (default: false)
 *   -R, --dry-run       No execute. Outputs the string of command. (default: false)
 *   -h, --help          Output usage information
 * @function disconnect
 * @memberof CLI
 */
cli.addProgram({
  command: 'disconnect [compName]',
  description: 'The command to disconnect a Windows from the resource',
  version: '5.0.0',
  options: [
    ['-S, --shared <name>', 'A shared name of the resource. Default: "IPC$"'],
    ['-H, --has-result', 'Show a result(net use)'],
    ['-R, --dry-run', 'No execute. Outputs the string of command.']
  ],
  action: function (compName, opt) {
    var retVal = net.SMB.disconnectSync(compName, opt.shared, {
      isDryRun: opt.dryRun
    });

    if (opt.dryRun) {
      console.log(retVal);
      if (opt.hasResult) net.SMB.showCurrentConnections();
    } else if (opt.hasResult) {
      console.dir(retVal);
      net.SMB.showCurrentConnections();
    }

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

// schemaConnect {{{
/**
 * Connects the resources defined on a schema.
 *
 * @example
 * Usage: schemaConnect <taskName> [overwriteKey:val...] [options]
 *
 * The command to connect a Windows to resources with the schema
 *
 * 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: "smbConnectorSchema")
 *   -L, --logger <val>     <level>/<transportation>. e.g. "warn/popup".  (default: "info/console")
 *   -H, --has-result       Show a result(net use) (default: false)
 *   -R, --dry-run          No execute. Outputs the string of command. (default: false)
 *   -h, --help             Output usage information
 * @function schemaConnect
 * @memberof CLI
 */
cli.addProgram({
  command: 'schemaConnect <taskName> [overwriteKey:val...]',
  description: 'The command to connect a Windows to resources with the schema',
  version: '5.0.1',
  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.', 'smbConnectorSchema'],
    ['-L, --logger <val>', '<level>/<transportation>. e.g. "warn/popup". ', 'info/console'],
    ['-H, --has-result', 'Show a result(net use)'],
    ['-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 = smbcn.connectSyncUsingSchema(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);