Skip to content

Commit

Permalink
refactor: remove barrel dependency from modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerhahnekamp committed Sep 15, 2024
1 parent 2a876d4 commit 2c11b0b
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 296 deletions.
5 changes: 2 additions & 3 deletions packages/core/src/lib/main/parse-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { FsPath } from '../file-info/fs-path';
import { FileInfo } from '../modules/file.info';
import { generateUnassignedFileInfo } from '../file-info/generate-unassigned-file-info';
import { getProjectDirsFromFileInfo } from '../modules/get-project-dirs-from-file-info';
import { findModulePathsWithBarrel } from '../modules/find-module-paths-with-barrel';
import { createModules } from '../modules/create-modules';
import { fillFileInfoMap } from '../modules/fill-file-info-map';
import throwIfNull from '../util/throw-if-null';
import { TsData } from '../file-info/ts-data';
import { Module } from '../modules/module';
import { SheriffConfig } from '../config/sheriff-config';
import { findModulePaths } from "../modules/find-module-paths";

export type ParsedResult = {
fileInfo: FileInfo;
Expand Down Expand Up @@ -38,8 +38,7 @@ export const parseProject = (
const getFileInfo = (path: FsPath) =>
throwIfNull(fileInfoMap.get(path), `cannot find FileInfo for ${path}`);

const modulePathsWithBarrel = findModulePathsWithBarrel(projectDirs, config.barrelFileName);
const modulePaths = modulePathsWithBarrel;
const modulePaths = findModulePaths(projectDirs, config.tagging, config.barrelFileName);
const modules = createModules(
unassignedFileInfo,
modulePaths,
Expand Down
78 changes: 35 additions & 43 deletions packages/core/src/lib/modules/create-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,49 @@ import { Module } from './module';
import { UnassignedFileInfo } from '../file-info/unassigned-file-info';
import traverseUnassignedFileInfo from '../file-info/traverse-unassigned-file-info';
import throwIfNull from '../util/throw-if-null';
import { FsPath } from '../file-info/fs-path';
import { formatModules } from './format-modules';
import get from '../util/get';
import { logger } from '../log';
import { FsPath, toFsPath } from '../file-info/fs-path';
import { FileInfo } from './file.info';
import { ModulePathMap } from './find-module-paths';
import { entries, fromEntries, keys, values } from "../util/typed-object-functions";

const log = logger('core.modules.create');

const findClosestModule = (path: string, moduleInfos: Module[]) => {
return throwIfNull(
moduleInfos
.filter((moduleInfo) => path.startsWith(moduleInfo.directory))
.sort((p1, p2) => (p1.directory.length > p2.directory.length ? -1 : 1))
.at(0),
`findClosestModule for ${path}`,
);
};

export const createModules = (
fileInfo: UnassignedFileInfo,
existingModules: Set<FsPath>,
export function createModules(
entryFileInfo: UnassignedFileInfo,
modulePathMap: ModulePathMap,
rootDir: FsPath,
fileInfoMap: Map<FsPath, FileInfo>,
getFileInfo: (path: FsPath) => FileInfo,
): Module[] => {
const moduleInfos = Array.from(existingModules).map(
(module) => new Module(module, fileInfoMap, getFileInfo, false),
): Module[] {
const moduleMap = fromEntries(
entries(modulePathMap).map(([path, hasBarrel]) => [
path,
new Module(toFsPath(path), fileInfoMap, getFileInfo, false, hasBarrel),
]),
);
moduleInfos.push(new Module(rootDir, fileInfoMap, getFileInfo, true));
const modulleInfoPerBarrelFile = new Map<string, Module>(
moduleInfos.map((moduleInfo) => [moduleInfo.path, moduleInfo]),
);
const moduleInfoMap = new Map<string, Module>(
moduleInfos.map((moduleInfo) => [moduleInfo.directory, moduleInfo]),
// add root module
moduleMap[rootDir] = new Module(
rootDir,
fileInfoMap,
getFileInfo,
true,
false,
);

for (const element of traverseUnassignedFileInfo(fileInfo)) {
const fi = element.fileInfo;
if (isFileInfoAModule(fi, existingModules)) {
get(modulleInfoPerBarrelFile, fi.path).addFileInfo(fi);
} else {
findClosestModule(fi.path, moduleInfos).addFileInfo(fi);
}
const modulePaths = keys(moduleMap);

for (const { fileInfo } of traverseUnassignedFileInfo(entryFileInfo)) {
const modulePath = findClosestModulePath(fileInfo.path, modulePaths);
moduleMap[modulePath].addFileInfo(fileInfo);
}

const foundModules = Array.from(moduleInfoMap.values());
log.info(formatModules(foundModules));
return foundModules;
};
return values(moduleMap);
}

const isFileInfoAModule = (
{ path }: UnassignedFileInfo,
existingModules: Set<string>,
) => existingModules.has(path);
function findClosestModulePath(path: string, modulePaths: FsPath[]) {
return throwIfNull(
modulePaths
.filter((modulePath) => path.startsWith(modulePath))
.sort((p1, p2) => (p1.length > p2.length ? -1 : 1))
.at(0),
`findClosestModule for ${path}`,
);
}
6 changes: 3 additions & 3 deletions packages/core/src/lib/modules/find-module-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { findModulePathsWithoutBarrel } from "./internal/find-module-paths-witho
import { TagConfig } from "../config/tag-config";
import { findModulePathsWithBarrel } from "./internal/find-module-paths-with-barrel";

export type ModulePaths = Record<FsPath, boolean>
export type ModulePathMap = Record<FsPath, boolean>

/**
* Find module paths which can be defined via having a barrel file or the
* configuration's property `modules`.
*
* If a module has a barrel file and an internal, it is of type barrel file.
*/
export function findModulePaths(projectDirs: FsPath[], moduleConfig: TagConfig, barrelFileName: string): ModulePaths {
export function findModulePaths(projectDirs: FsPath[], moduleConfig: TagConfig, barrelFileName: string): ModulePathMap {
const modulesWithoutBarrel = findModulePathsWithoutBarrel(projectDirs, moduleConfig);
const modulesWithBarrel = findModulePathsWithBarrel(projectDirs, barrelFileName);
const modulePaths: ModulePaths = {};
const modulePaths: ModulePathMap = {};

for (const path of modulesWithoutBarrel) {
modulePaths[path] = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { logger } from "../../log";
import { FsPath } from "../../file-info/fs-path";
import getFs from "../../fs/getFs";

const log = logger('core.modules.find-path');

export function findModulePathsWithBarrel(projectDirs: FsPath[], barrelFileName: string): Set<FsPath> {
const fs = getFs();
let modules: FsPath[] = [];

for (const projectDir of projectDirs) {
modules = modules.concat(fs.findFiles(projectDir, barrelFileName));
}

log.info(modules.join(', '));
return new Set(modules);
};
import { FsPath, toFsPath } from '../../file-info/fs-path';
import getFs from '../../fs/getFs';

export function findModulePathsWithBarrel(
projectDirs: FsPath[],
barrelFileName: string,
): FsPath[] {
return projectDirs.flatMap((projectDir) =>
getFs()
.findFiles(projectDir, barrelFileName)
.map((path) => path.slice(0, -(barrelFileName.length + 1)))
.map(toFsPath),
);
}
19 changes: 6 additions & 13 deletions packages/core/src/lib/modules/module.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import {UnassignedFileInfo} from '../file-info/unassigned-file-info';
import { UnassignedFileInfo } from '../file-info/unassigned-file-info';
import { FileInfo } from './file.info';
import { FsPath, toFsPath } from '../file-info/fs-path';
import { FsPath } from '../file-info/fs-path';

export class Module {
readonly directory: FsPath;
fileInfos: FileInfo[] = [];

constructor(
public readonly path: FsPath,
private readonly fileInfoMap: Map<FsPath, FileInfo>,
private readonly getFileInfo: (fsPath: FsPath) => FileInfo,
public readonly isRoot: boolean
) {
if (path.endsWith('index.ts')) {
this.directory = toFsPath(
this.path.substring(0, this.path.length - '/index.ts'.length),
);
} else {
this.directory = path;
}
}
public readonly isRoot: boolean,
public readonly hasBarrel: boolean,
) {}

addFileInfo(unassignedFileInfo: UnassignedFileInfo) {
const fileInfo = new FileInfo(unassignedFileInfo, this, this.getFileInfo);
Expand Down
Loading

0 comments on commit 2c11b0b

Please sign in to comment.