export async function resynthesize()

in packages/utils/blueprint-cli/src/resynth-drivers/resynth.ts [43:144]


export async function resynthesize(log: pino.BaseLogger, options: ResynthesizeOptions) {
  // todo validate input

  // build the resythesis driver
  const resynthDriver: DriverFile =
    options.resynthDriver ||
    makeResynthDriverFile(log, {
      blueprintLocation: options.blueprint,
      resynthDriver: options.resynthDriver,
    });

  const synthDriver: DriverFile =
    options.synthDriver ||
    makeSynthDriverFile(log, {
      blueprintLocation: options.blueprint,
      synthDriver: options.synthDriver,
    });

  const { ancestorLocation, proposedLocation, existingLocation, resolvedLocation } = setupResynthesisOutputDirectory(log, options.outdir, {
    existingBundle: options.existingBundleLocation,
  });

  // synthesize ancestor files if nothing exists at the location
  if (fs.existsSync(ancestorLocation)) {
    log.warn({ ancestorLocation }, 'Mocked ancestor already exists. Preserving mock, delete folder to regenerate');
  } else {
    createFolders(log, [ancestorLocation]);
    void (await synthesize(log, {
      blueprintPath: options.priorBlueprint,
      blueprintOptions: options.priorOptions,
      jobname: `${options.jobname}-${ancestorLocation}`,
      outputDirectory: ancestorLocation,
      synthDriver: synthDriver,
      existingBundle: existingLocation,
      cleanUp: false,
      envVariables: {
        RESYNTH_PHASE: 'ANCESTOR',
      },
    }));
  }

  // synthesize proposed files
  void (await synthesize(log, {
    blueprintPath: options.blueprint,
    blueprintOptions: options.options,
    jobname: `${options.jobname}-${proposedLocation}`,
    outputDirectory: proposedLocation,
    synthDriver: synthDriver,
    existingBundle: existingLocation,
    cleanUp: false,
    envVariables: {
      RESYNTH_PHASE: 'PROPOSED',
    },
  }));

  // if theres nothing at the existing files, copy-the ancestor files into there too.
  if (fs.readdirSync(existingLocation).length == 0) {
    log.warn('===============================');
    log.warn(`Nothing found in ${existingLocation}, seeding it with ancestor files as a default`);
    log.warn('===============================');
    copyFolderSync(log, ancestorLocation, existingLocation);
  }

  log.debug(`Using resynth driver: ${resynthDriver.path}`);
  try {
    // if something already exists at the output location, we remove it.
    log.debug('cleaning up existing code at resynth resolved output location: %s', resolvedLocation);
    const cleanCommand = `rm -rf ${resolvedLocation}`;
    cp.execSync(cleanCommand, {
      stdio: 'inherit',
      cwd: options.blueprint,
    });

    const timeStart = Date.now();
    executeResynthesisCommand(log, options.blueprint, options.jobname, {
      driver: resynthDriver,
      options: options.options,
      entropy: String(Math.floor(Date.now() / 100)),
      ancestorBundleDirectory: ancestorLocation,
      existingBundleDirectory: existingLocation,
      proposedBundleDirectory: proposedLocation,
      outputDirectory: resolvedLocation,
      envVariables: {
        RESYNTH_PHASE: 'RESYNTH',
      },
    });
    const timeEnd = Date.now();

    log.debug(`Resynth Time: [${timeEnd - timeStart} ms]`);
  } catch (e) {
    throw e;
  } finally {
    // if I wrote the resynth driver, then also clean it up.
    if (!options.resynthDriver && options.cleanUp === true) {
      cleanUpDriver(log, resynthDriver);
    }

    if (!options.synthDriver && options.cleanUp === true) {
      cleanUpDriver(log, synthDriver);
    }
  }
}