19657025fSTomasz Sapetaimport chalk from 'chalk';
29657025fSTomasz Sapetaimport fs from 'fs-extra';
3a272999eSBartosz Kaszubowskiimport path from 'path';
4a272999eSBartosz Kaszubowski
59657025fSTomasz Sapetaimport { findFiles } from '../utils';
6a272999eSBartosz Kaszubowskiimport { Task } from './Task';
79657025fSTomasz Sapeta
89657025fSTomasz Sapetaexport type FileContentTransformStepSettings = {
99657025fSTomasz Sapeta  source?: string;
109657025fSTomasz Sapeta  filePattern: string;
119657025fSTomasz Sapeta  find: string;
129657025fSTomasz Sapeta  replace: string;
13*53977629SŁukasz Kosmaty  debug?: boolean;
149657025fSTomasz Sapeta};
159657025fSTomasz Sapeta
169657025fSTomasz Sapeta/**
179657025fSTomasz Sapeta * A task which will transformed files content.
189657025fSTomasz Sapeta * Firstly, it's searching for all files which matched the `filePattern` in the working directory.
199657025fSTomasz Sapeta * Then it'll find the provided pattern and replace it with a new value.
209657025fSTomasz Sapeta */
219657025fSTomasz Sapetaexport class TransformFilesContent extends Task {
229657025fSTomasz Sapeta  protected readonly source?: string;
239657025fSTomasz Sapeta  protected readonly filePattern: string;
249657025fSTomasz Sapeta  protected readonly find: RegExp;
259657025fSTomasz Sapeta  protected readonly replace: string;
26*53977629SŁukasz Kosmaty  protected readonly debug: boolean;
279657025fSTomasz Sapeta
28*53977629SŁukasz Kosmaty  constructor({ source, filePattern, find, replace, debug }: FileContentTransformStepSettings) {
299657025fSTomasz Sapeta    super();
309657025fSTomasz Sapeta    this.source = source;
319657025fSTomasz Sapeta    this.filePattern = filePattern;
329657025fSTomasz Sapeta    this.find = new RegExp(find, 'gm');
339657025fSTomasz Sapeta    this.replace = replace;
34*53977629SŁukasz Kosmaty    this.debug = debug || false;
359657025fSTomasz Sapeta  }
369657025fSTomasz Sapeta
37a272999eSBartosz Kaszubowski  protected overrideWorkingDirectory(): string {
38a272999eSBartosz Kaszubowski    return this.source || '<workingDirectory>';
399657025fSTomasz Sapeta  }
409657025fSTomasz Sapeta
419657025fSTomasz Sapeta  async execute() {
429657025fSTomasz Sapeta    const workDirectory = this.getWorkingDirectory();
439657025fSTomasz Sapeta
449657025fSTomasz Sapeta    this.logSubStep(
459657025fSTomasz Sapeta      `�� find ${chalk.yellow(this.find.toString())} in ${chalk.green(
46a272999eSBartosz Kaszubowski        this.overrideWorkingDirectory()
479657025fSTomasz Sapeta      )}/${chalk.yellow(this.filePattern)} and replace with ${chalk.magenta(this.replace)}`
489657025fSTomasz Sapeta    );
499657025fSTomasz Sapeta
509657025fSTomasz Sapeta    const files = await findFiles(workDirectory, this.filePattern);
51*53977629SŁukasz Kosmaty    if (this.debug) {
52*53977629SŁukasz Kosmaty      this.logDebugInfo('Files: ' + files.join('\n'));
53*53977629SŁukasz Kosmaty    }
549657025fSTomasz Sapeta    await Promise.all(
559657025fSTomasz Sapeta      files.map(async (file) => {
569657025fSTomasz Sapeta        const content = await fs.readFile(file, 'utf8');
579657025fSTomasz Sapeta        const transformedContent = content.replace(this.find, this.replace);
589657025fSTomasz Sapeta        return await fs.writeFile(file, transformedContent, 'utf8');
599657025fSTomasz Sapeta      })
609657025fSTomasz Sapeta    );
619657025fSTomasz Sapeta  }
629657025fSTomasz Sapeta}
639657025fSTomasz Sapeta
649657025fSTomasz Sapetaexport const prefixPackage = ({
659657025fSTomasz Sapeta  packageName,
669657025fSTomasz Sapeta  prefix,
679657025fSTomasz Sapeta}: {
689657025fSTomasz Sapeta  source?: string;
699657025fSTomasz Sapeta  packageName: string;
709657025fSTomasz Sapeta  prefix: string;
719657025fSTomasz Sapeta}): TransformFilesContent => {
729657025fSTomasz Sapeta  return new TransformFilesContent({
739657025fSTomasz Sapeta    filePattern: path.join('android', '**', '*.@(java|kt)'),
749657025fSTomasz Sapeta    find: packageName,
759657025fSTomasz Sapeta    replace: `${prefix}.${packageName}`,
769657025fSTomasz Sapeta  });
779657025fSTomasz Sapeta};
789657025fSTomasz Sapeta
799657025fSTomasz Sapetaexport const renameIOSSymbols = (settings: {
809657025fSTomasz Sapeta  find: string;
819657025fSTomasz Sapeta  replace: string;
829657025fSTomasz Sapeta}): TransformFilesContent => {
839657025fSTomasz Sapeta  return new TransformFilesContent({
849657025fSTomasz Sapeta    ...settings,
85*53977629SŁukasz Kosmaty    filePattern: path.join('ios', '**', '*.@(h|m|mm)'),
869657025fSTomasz Sapeta  });
879657025fSTomasz Sapeta};
88