message: Changed commands to be lazy-loaded
diff: @@ -17,16 +17,10 @@ const readdirR = (dir: string): string | string[] => {
  return fs.statSync(dir).isDirectory()
  ? Array.prototype.concat(...fs.readdirSync(dir).map(f => readdirR(path.join(dir, f))))
  : dir;
  -}
  -
  -appInsights.trackEvent({
  - name: 'started'
  -});
  -
  -updateNotifier({ pkg: packageJSON }).notify({ defer: false });
  +};
  -fs.realpath(__dirname, (err: NodeJS.ErrnoException, resolvedPath: string): void => {
  - const commandsDir: string = path.join(resolvedPath, './o365');
  +const loadAllCommands = (rootFolder: string): void => {
  + const commandsDir: string = path.join(rootFolder, './o365');
  const files: string[] = readdirR(commandsDir) as string[];
  files.forEach(file => {
  @@ -42,16 +36,67 @@ fs.realpath(__dirname, (err: NodeJS.ErrnoException, resolvedPath: string): void
  catch { }
  }
  });
  +};
  +const loadCommandFromArgs = (args: string[], rootFolder: string): void => {
  + if (args.length <= 3) {
  + loadAllCommands(rootFolder);
  + return;
  + }
  +
  + // get the name of the command to be executed from args
  + // first two arguments are node and the name of the script
  + let cliArgs: string[] = args.slice(2);
  + // find the first command argument if any
  + // arguments start typically with - or -- but for the 'spo login' command
  + // it's the URL of the site to connect to
  + const pos: number = cliArgs.findIndex(p => p.startsWith('-') || p.startsWith('https://'));
  + if (pos > -1) {
  + // remove command args so that what's left is only the command name
  + cliArgs = cliArgs.slice(0, pos);
  + }
  +
  + const commandFilePath: string = cliArgs.length === 2 ?
  + path.join(rootFolder, 'o365', cliArgs[0], 'commands', `${cliArgs[1]}.js`) :
  + path.join(rootFolder, 'o365', cliArgs[0], 'commands', cliArgs[1], cliArgs.slice(1).join('-') + '.js');
  + if (!fs.existsSync(commandFilePath)) {
  + loadAllCommands(rootFolder);
  + return;
  + }
  +
  + try {
  + const cmd: any = require(commandFilePath);
  + if (cmd instanceof Command) {
  + cmd.init(vorpal);
  + }
  + else {
  + loadAllCommands(rootFolder);
  + }
  + }
  + catch {
  + loadAllCommands(rootFolder);
  + }
  +}
  +
  +appInsights.trackEvent({
  + name: 'started'
  +});
  +
  +updateNotifier({ pkg: packageJSON }).notify({ defer: false });
  +
  +fs.realpath(__dirname, (err: NodeJS.ErrnoException, resolvedPath: string): void => {
  if (process.argv.indexOf('--completion:clink:generate') > -1) {
  + loadAllCommands(resolvedPath);
  console.log(autocomplete.getClinkCompletion(vorpal));
  process.exit();
  }
  if (process.argv.indexOf('--completion:sh:generate') > -1) {
  + loadAllCommands(resolvedPath);
  autocomplete.generateShCompletion(vorpal);
  process.exit();
  }
  if (process.argv.indexOf('--completion:sh:setup') > -1) {
  + loadAllCommands(resolvedPath);
  autocomplete.generateShCompletion(vorpal);
  autocomplete.setupShCompletion();
  process.exit();
  @@ -90,6 +135,8 @@ fs.realpath(__dirname, (err: NodeJS.ErrnoException, resolvedPath: string): void
  }
  });
  }
  +
  + loadCommandFromArgs(process.argv, resolvedPath);
  v = vorpal.parse(process.argv);
  // if no command has been passed/match, run immersive mode
  