export async function infuse()

in src/commands/infuse.ts [65:148]


export async function infuse(assemblyLocations: string[], options?: InfuseOptions): Promise<InfuseResult> {
  let stream: fs.WriteStream | undefined = undefined;
  if (options?.logFile) {
    // Create stream for html file and insert some styling
    stream = fs.createWriteStream(options.logFile, { encoding: 'utf-8' });
    startFile(stream);
  }

  // Load tablet file and assemblies
  const assemblies = loadAssemblies(assemblyLocations, false);
  const defaultTablets = await loadAllDefaultTablets(assemblies);

  const availableTranslations = new LanguageTablet();
  if (options?.cacheFromFile) {
    availableTranslations.addTablet(await LanguageTablet.fromOptionalFile(options.cacheFromFile));
  }
  availableTranslations.addTablets(...Object.values(defaultTablets));

  const { translationsByFqn, originalsByKey } = await availableSnippetsPerFqn(assemblies, availableTranslations);

  const additionalOutputTablet = options?.cacheToFile
    ? await LanguageTablet.fromOptionalFile(options?.cacheToFile)
    : new LanguageTablet();

  const coverageResults = mkDict(
    await Promise.all(
      assemblies.map(async ({ assembly, directory }) => {
        stream?.write(`<h1>${assembly.name}</h1>\n`);

        const implicitTablet = defaultTablets[directory];
        const implicitTabletFile = path.join(
          directory,
          implicitTablet.compressedSource ? DEFAULT_TABLET_NAME_COMPRESSED : DEFAULT_TABLET_NAME,
        );
        if (!implicitTablet) {
          throw new Error(`No tablet found for ${directory}`);
        }

        let insertedExamples = 0;
        const filteredTypes = filterForTypesWithoutExamples(assembly.types ?? {});
        for (const [typeFqn, type] of Object.entries(filteredTypes)) {
          const available = translationsByFqn[typeFqn];
          if (!available) {
            continue;
          }

          const example = pickBestExample(typeFqn, available, stream);
          const original = originalsByKey[example.key];
          insertExample(example, original, type, [implicitTablet, additionalOutputTablet]);
          insertedExamples++;
        }

        if (insertedExamples > 0) {
          // Save the updated assembly and implicit tablets
          // eslint-disable-next-line no-await-in-loop
          await Promise.all([
            replaceAssembly(assembly, directory),
            implicitTablet.save(implicitTabletFile, implicitTablet.compressedSource),
          ]);
        }

        return [
          directory,
          {
            types: Object.keys(filteredTypes).length,
            typesWithInsertedExamples: insertedExamples,
          } as InfuseTypes,
        ] as const;
      }),
    ),
  );

  stream?.close();

  // If we copied examples onto different types, we'll also have inserted new snippets
  // with different keys into the tablet. We must now write the updated tablet somewhere.
  if (options?.cacheToFile) {
    await additionalOutputTablet.save(options.cacheToFile, options.compressCacheToFile);
  }

  return {
    coverageResults: coverageResults,
  };
}