async function main()

in packages/apps/autorest/src/app.ts [67:165]


async function main() {
  logBanner();

  try {
    // did they ask for what is available?
    if (listAvailable) {
      process.exit(await showAvailableCoreVersions(args));
    }

    // show what we have.
    if (args.info) {
      process.exit(await showInstalledExtensions(args));
    }

    try {
      /* make sure we have a .autorest folder */
      await ensureAutorestHome();

      if (args.reset || args["clear-temp"]) {
        // clear out all the temp-data too
        await clearTempData();
      }

      // if we have an autorest home folder, --reset may mean something.
      // if it's not there, --reset won't do anything.
      if (args.reset) {
        process.exit(await resetAutorest(args));
      }
    } catch {
      // We have a chance to fail again later if this proves problematic.
    }
    const sink = new ConsoleLoggerSink({ format: args["message-format"] });
    const logger = new AutorestSyncLogger({
      sinks: [sink],
    });
    const config = await loadConfig(sink, args);
    if (config?.version) {
      logger.info(`AutoRest core version selected from configuration: ${chalk.yellow.bold(config.version)}.`);
    }

    const coreVersionPath = await resolveCoreVersion(
      logger.with(new FilterLogger({ level: getLogLevel({ ...args, ...config }) })),
      config,
    );

    if (args.verbose || args.debug) {
      configureLibrariesLogger("verbose", (...x) => logger.debug(x.join(" ")));
    }

    // let's strip the extra stuff from the command line before we require the core module.
    const newArgs: string[] = [];

    for (const each of process.argv) {
      let keep = true;
      for (const discard of [
        "--version",
        "--list-installed",
        "--list-available",
        "--reset",
        "--latest",
        "--latest-release",
        "--runtime-id",
      ]) {
        if (each === discard || each.startsWith(`${discard}=`) || each.startsWith(`${discard}:`)) {
          keep = false;
        }
      }
      if (keep) {
        newArgs.push(each);
      }
    }

    // use this to make the core aware that this run may be legal even without any inputs
    // this is a valid scenario for "preparation calls" to autorest like `autorest --reset` or `autorest --latest`
    if (args.reset || args.latest || args.version == "latest") {
      // if there is *any* other argument left, that's an indicator that the core is supposed to do something
      newArgs.push("--allow-no-input");
    }

    process.argv = newArgs;

    if (args.debug) {
      logger.debug(`Starting ${newCorePackage} from ${coreVersionPath}`);
    }

    // reset the working folder to the correct place.
    process.chdir(cwd);

    const result = await launchCore(coreVersionPath, "app.js", config);
    if (!result) {
      throw new Error(`Unable to start AutoRest Core from ${coreVersionPath}`);
    }
  } catch (exception) {
    console.log(chalk.redBright("Failure:"));
    console.error(chalk.bold(exception));
    console.error(chalk.bold((<Error>exception).stack));
    process.exit(1);
  }
}