Skip to content

Commit

Permalink
Refactor some bits to add manifestStr
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Feb 10, 2025
1 parent 012fd5b commit 9ce5766
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
19 changes: 12 additions & 7 deletions packages/knip/src/ConfigurationChief.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
RawPluginConfiguration,
WorkspaceConfiguration,
} from './types/config.js';
import type { Package, PackageJson } from './types/package-json.js';
import type { PackageJson, WorkspacePackage } from './types/package-json.js';
import { arrayify, compact } from './util/array.js';
import parsedArgValues from './util/cli-arguments.js';
import { type WorkspaceGraph, createWorkspaceGraph } from './util/create-workspace-graph.js';
Expand Down Expand Up @@ -72,6 +72,7 @@ export type Workspace = {
ancestors: string[];
config: WorkspaceConfiguration;
manifestPath: string;
manifestStr: string;
ignoreMembers: IgnorePatterns;
srcDir?: string;
outDir?: string;
Expand All @@ -97,7 +98,7 @@ export class ConfigurationChief {
manifest?: PackageJson;

ignoredWorkspacePatterns: string[] = [];
workspacePackages = new Map<string, Package>();
workspacePackages = new Map<string, WorkspacePackage>();
workspacesByPkgName = new Map<string, Workspace>();
workspacesByName = new Map<string, Workspace>();
additionalWorkspaceNames = new Set<string>();
Expand Down Expand Up @@ -233,7 +234,7 @@ export class ConfigurationChief {

this.workspaceGraph = createWorkspaceGraph(this.cwd, this.availableWorkspaceNames, wsPkgNames, packages);

this.includedWorkspaces = this.setIncludedWorkspaces();
this.includedWorkspaces = this.getIncludedWorkspaces();

for (const workspace of this.includedWorkspaces) {
this.workspacesByPkgName.set(workspace.pkgName, workspace);
Expand Down Expand Up @@ -286,7 +287,7 @@ export class ConfigurationChief {
);
}

private setIncludedWorkspaces() {
private getIncludedWorkspaces() {
if (this.workspace) {
const dir = resolve(this.workspace);
if (!isDirectory(dir)) throw new ConfigurationError('Workspace is not a directory');
Expand Down Expand Up @@ -331,7 +332,10 @@ export class ConfigurationChief {
.sort(byPathDepth)
.map((name): Workspace => {
const dir = join(this.cwd, name);
const pkgName = this.workspacePackages.get(name)?.pkgName ?? `KNIP_ADDED_${name}`;
const pkg = this.workspacePackages.get(name);
const pkgName = pkg?.pkgName ?? `KNIP_ADDED_${name}`;
const manifestPath = pkg?.manifestPath ?? join(dir, 'package.json');
const manifestStr = pkg?.manifestStr ?? '';
const workspaceConfig = this.getWorkspaceConfig(name);
const ignoreMembers = arrayify(workspaceConfig.ignoreMembers).map(toRegexOrString);
return {
Expand All @@ -340,7 +344,8 @@ export class ConfigurationChief {
dir,
config: this.getConfigForWorkspace(name),
ancestors: this.availableWorkspaceNames.reduce(getAncestors(name), []),
manifestPath: join(dir, 'package.json'),
manifestPath,
manifestStr,
ignoreMembers,
};
});
Expand All @@ -350,7 +355,7 @@ export class ConfigurationChief {
return this.workspacePackages.get(name)?.manifest;
}

public getIncludedWorkspaces() {
public getWorkspaces() {
return this.includedWorkspaces;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/knip/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {

await chief.init();

const workspaces = chief.getIncludedWorkspaces();
const workspaces = chief.getWorkspaces();
const report = chief.getIncludedIssueTypes({
includedIssueTypes,
excludedIssueTypes,
Expand Down Expand Up @@ -110,10 +110,10 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
const shouldIgnoreTags = getShouldIgnoreTagHandler(tags);

for (const workspace of workspaces) {
const { name, dir, manifestPath } = workspace;
const { name, dir, manifestPath, manifestStr } = workspace;
const manifest = chief.getManifestForWorkspace(name);
if (!manifest) continue;
deputy.addWorkspace({ name, cwd, dir, manifestPath, manifest, ...chief.getIgnores(name) });
deputy.addWorkspace({ name, cwd, dir, manifestPath, manifestStr, manifest, ...chief.getIgnores(name) });
}

for (const workspace of workspaces) {
Expand Down
9 changes: 8 additions & 1 deletion packages/knip/src/types/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ export type PackageJson = {
typings?: string;
} & Plugins;

export type Package = { dir: string; name: string; pkgName: string | undefined; manifest: PackageJson };
export type WorkspacePackage = {
dir: string;
name: string;
pkgName: string | undefined;
manifest: PackageJson;
manifestPath: string;
manifestStr: string;
};
1 change: 1 addition & 0 deletions packages/knip/src/types/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type DependencyArray = Array<string>;
type WorkspaceManifest = {
workspaceDir: string;
manifestPath: string;
manifestStr: string;
dependencies: DependencyArray;
devDependencies: DependencyArray;
peerDependencies: DependencySet;
Expand Down
17 changes: 9 additions & 8 deletions packages/knip/src/util/map-workspaces.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { readFile } from 'node:fs/promises';
import fg from 'fast-glob';
import type { Package, PackageJson } from '../types/package-json.js';
import type { PackageJson, WorkspacePackage } from '../types/package-json.js';
import { partition } from './array.js';
import { debugLog } from './debug.js';
import { ConfigurationError } from './errors.js';
import { getPackageName } from './package-name.js';
import { join } from './path.js';
import { _require } from './require.js';

type Packages = Map<string, Package>;
type Packages = Map<string, WorkspacePackage>;
type WorkspacePkgNames = Set<string>;

export default async function mapWorkspaces(cwd: string, workspaces: string[]): Promise<[Packages, WorkspacePkgNames]> {
Expand All @@ -25,17 +25,18 @@ export default async function mapWorkspaces(cwd: string, workspaces: string[]):

for (const name of matches) {
const dir = join(cwd, name);
const filePath = join(dir, 'package.json');
const manifestPath = join(dir, 'package.json');
try {
const manifest: PackageJson = _require(filePath);
const manifestStr = await readFile(manifestPath, 'utf8');
const manifest: PackageJson = JSON.parse(manifestStr);
const pkgName = getPackageName(manifest, dir);
const pkg: Package = { dir, name, pkgName, manifest };
const pkg: WorkspacePackage = { dir, name, pkgName, manifestPath, manifestStr, manifest };
packages.set(name, pkg);
if (pkgName) wsPkgNames.add(pkgName);
else throw new ConfigurationError(`Missing package name in ${filePath}`);
else throw new ConfigurationError(`Missing package name in ${manifestPath}`);
} catch (error) {
// @ts-expect-error
if (error?.code === 'MODULE_NOT_FOUND') debugLog('*', `Unable to load package.json for ${name}`);
if (error?.code === 'ENOENT') debugLog('*', `Unable to load package.json for ${name}`);
else throw error;
}
}
Expand Down

0 comments on commit 9ce5766

Please sign in to comment.