xref: /expo/tools/src/vendoring/devmenu/steps/Task.ts (revision 53977629)
19657025fSTomasz Sapetaimport logger from '../../../Logger';
29657025fSTomasz Sapetaimport { toRepoPath } from '../utils';
39657025fSTomasz Sapeta
49657025fSTomasz Sapeta/**
5a272999eSBartosz Kaszubowski * A base class for all task.
6a272999eSBartosz Kaszubowski * It provides a simple task launcher, log utils and path to working directory.
79657025fSTomasz Sapeta */
89657025fSTomasz Sapetaexport abstract class Task {
99657025fSTomasz Sapeta  private workingDirectory?: string;
109657025fSTomasz Sapeta
119657025fSTomasz Sapeta  /**
129657025fSTomasz Sapeta   * Tasks can contain multiple steps. This function provides a consistent way to log information about each step.
139657025fSTomasz Sapeta   * @param message
149657025fSTomasz Sapeta   */
159657025fSTomasz Sapeta  protected logSubStep(message: string) {
169657025fSTomasz Sapeta    logger.info(`> ${message}`);
179657025fSTomasz Sapeta  }
189657025fSTomasz Sapeta
199657025fSTomasz Sapeta  /**
209657025fSTomasz Sapeta   * A function which provides a consistent way of printing debug information inside a task.
219657025fSTomasz Sapeta   * @param message which will be printed using debug log level.
229657025fSTomasz Sapeta   */
239657025fSTomasz Sapeta  protected logDebugInfo(message: string | string[]) {
249657025fSTomasz Sapeta    if (typeof message === 'string') {
259657025fSTomasz Sapeta      logger.debug(`  ${message}`);
269657025fSTomasz Sapeta    } else {
279657025fSTomasz Sapeta      logger.debug(`  ${message.join('\n    ')}`);
289657025fSTomasz Sapeta    }
299657025fSTomasz Sapeta  }
309657025fSTomasz Sapeta
319657025fSTomasz Sapeta  /**
329657025fSTomasz Sapeta   * We want to have a way to change working directory using task's settings.
339657025fSTomasz Sapeta   * For example, we could run pipe in the temp directory but one task from it in the repo.
34a272999eSBartosz Kaszubowski   * It's ignored if `null` was returned.
359657025fSTomasz Sapeta   * @returns the override working directory for task.
369657025fSTomasz Sapeta   */
37a272999eSBartosz Kaszubowski  protected overrideWorkingDirectory(): string | null {
38a272999eSBartosz Kaszubowski    return null;
399657025fSTomasz Sapeta  }
409657025fSTomasz Sapeta
419657025fSTomasz Sapeta  /**
429657025fSTomasz Sapeta   * @returns the absolute path to working directory for task based on overrideWorkDirectory().
439657025fSTomasz Sapeta   */
449657025fSTomasz Sapeta  protected getWorkingDirectory(): string {
459657025fSTomasz Sapeta    const overrideValue = this.overrideWorkingDirectory();
46*53977629SŁukasz Kosmaty    if (overrideValue && overrideValue !== '<workingDirectory>') {
479657025fSTomasz Sapeta      return toRepoPath(overrideValue);
489657025fSTomasz Sapeta    }
499657025fSTomasz Sapeta
50*53977629SŁukasz Kosmaty    return this.workingDirectory!;
519657025fSTomasz Sapeta  }
529657025fSTomasz Sapeta
539657025fSTomasz Sapeta  /**
549657025fSTomasz Sapeta   * Sets the working directory for the task.
559657025fSTomasz Sapeta   * @param workingDirectory
569657025fSTomasz Sapeta   */
579657025fSTomasz Sapeta  public setWorkingDirectory(workingDirectory: string) {
589657025fSTomasz Sapeta    this.workingDirectory = workingDirectory;
599657025fSTomasz Sapeta  }
609657025fSTomasz Sapeta
619657025fSTomasz Sapeta  /**
629657025fSTomasz Sapeta   * A function which will be call in start method. The body of the task.
639657025fSTomasz Sapeta   */
64086befdaSandy  protected abstract execute(): Promise<void>;
659657025fSTomasz Sapeta
669657025fSTomasz Sapeta  /**
679657025fSTomasz Sapeta   * A method that starts the task. It provides error handling.
689657025fSTomasz Sapeta   */
699657025fSTomasz Sapeta  public async start() {
709657025fSTomasz Sapeta    try {
719657025fSTomasz Sapeta      await this.execute();
729657025fSTomasz Sapeta    } catch (e) {
739657025fSTomasz Sapeta      logger.error(e);
749657025fSTomasz Sapeta    }
759657025fSTomasz Sapeta  }
769657025fSTomasz Sapeta}
77