File

libs/nx/src/generators/journey-src/lib/add-acl-defaults-to-policy/read-journals.ts

Index

Properties

Properties

journal
Type JournalFileSchema
name
Type string
import { JournalFileSchema } from '@allianz/taly-core/schemas';
import { Tree, readProjectConfiguration } from '@nx/devkit';
import { join } from 'path';

export interface LibraryMetaData {
  name: string;
  journal: JournalFileSchema;
}

export function readJournals(
  tree: Tree,
  libraryNames: string[],
  workspaceNodeModulesDirectory: string
): LibraryMetaData[] {
  return libraryNames.map((libraryName) => {
    const possibleLibraryJournalPaths = getPossiblePathsToJournalJson(
      tree,
      libraryName,
      workspaceNodeModulesDirectory
    );
    const libraryJournalPath = possibleLibraryJournalPaths.find((path) => tree.exists(path));
    if (!libraryJournalPath) {
      throw new Error(`Could not read library journal for "${libraryName}".
We tried these paths: ${possibleLibraryJournalPaths.join(', ')}.
If one of these paths seems to be correct it might be that this particular library did not include the journal.json file. In that case please contact the library authors.
If you are consuming a library from inside your workspace, make sure the journal.json file for that library exists. If it doesn't, run the journal executor for that library. Try this:\nnpx nx journal ${libraryName}`);
    }
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    const libraryJournalContent = tree.read(libraryJournalPath, 'utf-8')!;
    const libraryJournal = JSON.parse(libraryJournalContent) as JournalFileSchema;
    return {
      name: libraryName,
      journal: libraryJournal
    };
  });
}

function getPossiblePathsToJournalJson(
  tree: Tree,
  libraryName: string,
  workspaceNodeModulesDirectory: string
): string[] {
  const workspaceNodeModulesLibraryRoot = join(workspaceNodeModulesDirectory, libraryName);
  const rootNodeModulesLibraryRoot = join('node_modules', libraryName);

  let localLibraryRoot: string | undefined;
  try {
    localLibraryRoot = readProjectConfiguration(tree, libraryName).root;
  } catch {
    // this might mean that this particular library is an external library so we
    // swallow any errors here and show a meaningful error message above.
  }

  // order in which journals will be looked for:
  // - workspace node_modules
  // - local libraries
  // - root node_modules
  //
  // the "root node_modules" will only yield a difference if the current workspace is
  // not the root, i.e. we are working in a generated workspace
  const prioritizedPossibleLibraryRoots = [
    workspaceNodeModulesLibraryRoot,
    localLibraryRoot,
    rootNodeModulesLibraryRoot
  ];
  return prioritizedPossibleLibraryRoots
    .filter((path): path is string => Boolean(path))
    .map((possibleRoot) => join(possibleRoot, 'journal.json'));
}

results matching ""

    No results matching ""