Skip to content

Commit 7baaed0

Browse files
committed
refactor: remove unused config parameter and streamline package export handling in update-exports command and use case
1 parent bc6d38e commit 7baaed0

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

packages/cli/src/application/commands/update-exports.command.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { inject, injectable } from "inversify";
33
import * as process from "node:process";
44

55
import type { UpdateExportsUseCase } from "@/application/use-cases/update-exports.use-case";
6-
import type { ScriptConfig } from "@/domain/entities/package-config";
76

87
import { TYPES } from "@/ioc/types";
98

@@ -13,25 +12,22 @@ export class UpdateExportsCommand {
1312

1413
/**
1514
* Returns a Commander.js command for updating package exports.
16-
* @param config - Script configuration for package export patterns.
15+
*
1716
* @returns Configured Commander.js command.
1817
*/
19-
getCommand(config: ScriptConfig): Command {
18+
getCommand(): Command {
2019
return new Command()
2120
.name("update-exports")
2221
.description("Updates exports for all packages")
2322
.option("-p, --package <package>", "Filter by package name")
2423
.option("-d, --dry-run", "Run without making changes")
2524
.option("-c, --config <path>", "Path to configuration file")
2625
.action(async (options: { config?: string; dryRun?: string; package?: string }) => {
27-
await this.updateExportsUseCase.execute(
28-
{
29-
packageFilter: options.package,
30-
dryRun: Boolean(options.dryRun),
31-
configPath: options.config,
32-
},
33-
config,
34-
);
26+
await this.updateExportsUseCase.execute({
27+
packageFilter: options.package,
28+
dryRun: Boolean(options.dryRun),
29+
configPath: options.config,
30+
});
3531

3632
process.exit(0);
3733
});

packages/cli/src/application/use-cases/update-exports.use-case.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { inject, injectable } from "inversify";
22

3-
import type { ScriptConfig } from "@/domain/entities/package-config";
43
import type { PackageRepository } from "@/domain/interfaces/package.repository";
54

65
import { handleError } from "@/application/utilities/error-handler";
@@ -13,23 +12,19 @@ export class UpdateExportsUseCase {
1312
/**
1413
* Updates exports for all packages based on provided options and configuration.
1514
* @param options - Options for dry run, config path, and package filter.
16-
* @param config - Script configuration for package export patterns.
1715
*/
18-
async execute(
19-
options: { dryRun: boolean; configPath?: string; packageFilter?: string },
20-
config: ScriptConfig,
21-
): Promise<void> {
16+
async execute(options: { dryRun: boolean; configPath?: string; packageFilter?: string }): Promise<void> {
2217
try {
23-
console.info("Searching for packages...");
24-
const packageJsonPaths = await this.packageRepository.findAllPackages(config);
18+
console.info("♢‒ Searching for packages...");
19+
const packageJsonPaths = await this.packageRepository.findAllPackages(options.configPath);
2520

2621
if (packageJsonPaths.length === 0) {
2722
console.warn("No packages found.");
2823

2924
return;
3025
}
3126

32-
console.log(`Found ${packageJsonPaths.length} packages.`);
27+
console.log(` Found ${packageJsonPaths.length} packages.`);
3328

3429
let successCount = 0;
3530
let skipCount = 0;
@@ -47,7 +42,7 @@ export class UpdateExportsUseCase {
4742
}
4843
}
4944

50-
console.log("Completed exports update.");
45+
console.log("♢‒ Completed exports update.\n");
5146
console.info(`Stats: ${successCount} succeeded, ${skipCount} skipped, ${errorCount} failed`);
5247
} catch (error) {
5348
handleError(error, "Error processing packages");

packages/cli/src/infrastructure/repositories/file-system-package.repository.ts

+23-15
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ import path from "node:path";
55

66
import type { AnalysisPort } from "@/application/ports/analysis.port";
77
import type { FileSystemPort } from "@/application/ports/file-system.port";
8-
import type { AnalysisResult, PackageConfig, PackageExports, ScriptConfig } from "@/domain/entities/package-config";
8+
import type {
9+
AnalysisResult,
10+
PackageConfig,
11+
PackageExports,
12+
PackageJson,
13+
ScriptConfig,
14+
} from "@/domain/entities/package-config";
915
import type { PackageRepository } from "@/domain/interfaces/package.repository";
1016
import type { FileSystemUtility } from "@/infrastructure/utilities/file-system-utility";
1117

12-
import { packageJsonSchema, scriptConfigSchema } from "@/domain/entities/package-config";
18+
import { scriptConfigSchema } from "@/domain/entities/package-config";
1319
import { TYPES } from "@/ioc/types";
1420

1521
@injectable()
@@ -42,7 +48,9 @@ export class FileSystemPackageRepository implements PackageRepository {
4248
@inject(TYPES.FileSystemUtility) private fileSystemUtility: FileSystemUtility,
4349
) {}
4450

45-
async findAllPackages(config: ScriptConfig): Promise<string[]> {
51+
async findAllPackages(configPath?: string): Promise<string[]> {
52+
const config = this.getConfig(configPath);
53+
4654
return await glob(config.packagesGlob, {
4755
ignore: this.DEFAULT_IGNORE_PATTERN,
4856
});
@@ -60,7 +68,7 @@ export class FileSystemPackageRepository implements PackageRepository {
6068
let packageJson;
6169

6270
try {
63-
packageJson = packageJsonSchema.parse(JSON.parse(this.fileSystemPort.readFile(packageJsonPath)));
71+
packageJson = JSON.parse(this.fileSystemPort.readFile(packageJsonPath)) as PackageJson;
6472
} catch (error) {
6573
console.error(`Failed to parse ${packageJsonPath}: ${error instanceof Error ? error.message : "Unknown error"}`);
6674

@@ -75,28 +83,28 @@ export class FileSystemPackageRepository implements PackageRepository {
7583
return false;
7684
}
7785

78-
console.info(`Processing package: ${packageName}`);
86+
console.info(`♢‒ Processing package: ${packageName}`);
7987

8088
const config = this.getConfig(options.configPath);
8189
const packageConfig = this.getPackageConfig(packageName, config);
8290
const srcIndexPath = path.join(packageDir, packageConfig.srcIndexPath);
8391

8492
if (!this.fileSystemPort.exists(srcIndexPath)) {
85-
console.error(`Source file not found at ${srcIndexPath}`);
93+
console.error(`⎹ ⛔︎ Source file not found at ${srcIndexPath}`);
8694

8795
return false;
8896
}
8997

90-
console.info(`Analyzing imports from ${srcIndexPath}...`);
98+
console.info(`Analyzing imports from ${srcIndexPath}...`);
9199
const analysis = this.analyzeImports(srcIndexPath, packageConfig);
92100

93101
if (analysis.imports.length === 0) {
94-
console.warn(`No imports found to analyze in ${packageName}`);
102+
console.warn(`⎹ ⛔︎ No imports found to analyze in ${packageName}`);
95103

96104
return false;
97105
}
98106

99-
console.log(`Analysis complete. Found ${analysis.imports.length} subpath exports.`);
107+
console.log(`Analysis complete. Found ${analysis.imports.length} subpath exports.`);
100108

101109
this.saveExportsAnalysis(packageDir, analysis);
102110

@@ -105,23 +113,23 @@ export class FileSystemPackageRepository implements PackageRepository {
105113
const currentExportsCount = packageJson.exports ? Object.keys(packageJson.exports).length : 0;
106114
const newExportsCount = Object.keys(newExports).length;
107115

108-
console.info(`Exports: ${currentExportsCount} -> ${newExportsCount}`);
116+
console.info(`Exports: ${currentExportsCount} -> ${newExportsCount}`);
109117

110118
if (options.dryRun) {
111119
this.saveExportsPreview(packageDir, newExports);
112-
console.warn(`Saved exports preview to .exports-analysis`);
113-
console.warn(`Dry run: no changes saved for ${packageName}`);
120+
console.warn(`Saved exports preview to .exports-analysis`);
121+
console.warn(`Dry run: no changes saved for ${packageName}`);
114122

115123
return true;
116124
}
117125

118126
this.fileSystemUtility.backupFile(packageJsonPath, ".exports-analysis");
119-
console.log(`Backed up ${packageJsonPath}`);
127+
console.log(`Backed up ${packageJsonPath}`);
120128

121129
const updatedPackageJson = { ...packageJson, exports: newExports };
122130

123-
this.fileSystemPort.writeFile(packageJsonPath, JSON.stringify(updatedPackageJson, null, 2));
124-
console.log(`Updated exports for ${packageName}`);
131+
this.fileSystemPort.writeFile(packageJsonPath, `${JSON.stringify(updatedPackageJson, null, 2)}\n`);
132+
console.log(`Updated exports for ${packageName}`);
125133

126134
return true;
127135
}

0 commit comments

Comments
 (0)