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, joinPathFragments, readProjectConfiguration } from '@nx/devkit';
import { join } from 'path';

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

export function readJournals(tree: Tree, libraryNames: string[]): LibraryMetaData[] {
  const workspaceRoot = './';
  const workspaceNodeModulesDirectory = joinPathFragments(workspaceRoot, 'node_modules');

  return libraryNames.map((libraryName) => ({
    name: libraryName,
    journal: resolveJournal(tree, libraryName, workspaceNodeModulesDirectory)
  }));
}

function resolveJournal(
  tree: Tree,
  libraryName: string,
  workspaceNodeModulesDirectory: string
): JournalFileSchema {
  const libraryJournalPath = findJournalPath(tree, libraryName, workspaceNodeModulesDirectory);
  if (libraryJournalPath) {
    return readJournalFile(tree, libraryJournalPath);
  }

  // If the library is a secondary entry point, it might not have its own journal
  // but the parent package might have one that includes blocks for this entry point.
  const parentPackageName = getParentPackageName(libraryName);
  if (parentPackageName) {
    const parentJournalPath = findJournalPath(
      tree,
      parentPackageName,
      workspaceNodeModulesDirectory
    );
    if (parentJournalPath) {
      const parentJournal = readJournalFile(tree, parentJournalPath);
      return {
        ...parentJournal,
        blocks: (parentJournal.blocks ?? []).filter((block) => block.package === libraryName)
      };
    }
  }

  const possibleLibraryJournalPaths = getPossiblePathsToJournalJson(
    tree,
    libraryName,
    workspaceNodeModulesDirectory
  );
  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}`);
}

function findJournalPath(
  tree: Tree,
  libraryName: string,
  workspaceNodeModulesDirectory: string
): string | undefined {
  return getPossiblePathsToJournalJson(tree, libraryName, workspaceNodeModulesDirectory).find(
    (path) => tree.exists(path)
  );
}

function readJournalFile(tree: Tree, path: string): JournalFileSchema {
  const content = tree.read(path, 'utf-8');
  if (!content) throw new Error(`Failed to read journal at "${path}"`);
  return JSON.parse(content) as JournalFileSchema;
}

/**
 * Derives the root package name from a secondary entry point name.
 * e.g. '@scope/pkg/sub/path' → '@scope/pkg', 'pkg/sub/path' → 'pkg'
 * Returns undefined if the name is already a root package.
 */
function getParentPackageName(libraryName: string): string | undefined {
  const segments = libraryName.split('/');
  const rootSegmentsCount = libraryName.startsWith('@') ? 2 : 1;
  if (segments.length <= rootSegmentsCount) {
    return undefined;
  }
  return segments.slice(0, rootSegmentsCount).join('/');
}

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 ""