Skip to content

Commit bf346c1

Browse files
committed
refactor(config): streamline module path resolution
- Introduced a `moduleRoot` constant to simplify path calculations for the schema and package.json files. - Updated the `createValidator` method to use `moduleRoot` for resolving the schema path and loading the client version from package.json, enhancing code clarity and maintainability. - Removed redundant code related to package.json path handling, improving overall efficiency.
1 parent 75b04a4 commit bf346c1

1 file changed

Lines changed: 22 additions & 21 deletions

File tree

packages/cli/stores/config.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ const defaultConfig: Config = {
4444
typesOutput: [{ path: DEFAULT_TYPES_OUTPUT, format: "react" }],
4545
};
4646

47+
const moduleRoot = fileURLToPath(import.meta.url).substring(
48+
0,
49+
fileURLToPath(import.meta.url).lastIndexOf("cli") + 3,
50+
);
51+
4752
// Helper to normalize typesOutput to array format
4853
export function normalizeTypesOutput(
4954
output?: string | TypesOutput[],
@@ -70,11 +75,7 @@ class ConfigStore {
7075
protected async createValidator() {
7176
try {
7277
// Using current config store file, resolve the schema.json path
73-
const filePath = fileURLToPath(import.meta.url);
74-
const schemaPath = join(
75-
filePath.substring(0, filePath.lastIndexOf("cli") + 3),
76-
"schema.json",
77-
);
78+
const schemaPath = join(moduleRoot, "schema.json");
7879
const content = await readFile(schemaPath, "utf-8");
7980
const parsed = parseJSON(content) as unknown as Config;
8081
const ajv = new Ajv();
@@ -90,32 +91,32 @@ class ConfigStore {
9091
return;
9192
}
9293

94+
// Load the client version from the module's package.json metadata
9395
try {
94-
const packageJSONPath = await findUp("package.json");
96+
const moduleMetadata = await readFile(
97+
join(moduleRoot, "package.json"),
98+
"utf-8",
99+
);
100+
const moduleMetadataParsed = parseJSON(moduleMetadata) as unknown as {
101+
version: string;
102+
};
103+
this.clientVersion = moduleMetadataParsed.version;
104+
} catch {
105+
// Should not be the case, but ignore if no package.json is found
106+
}
107+
108+
try {
109+
const projectMetadataPath = await findUp("package.json");
95110
this.configPath = await findUp(CONFIG_FILE_NAME);
96111
this.projectPath = dirname(
97-
this.configPath ?? packageJSONPath ?? process.cwd(),
112+
this.configPath ?? projectMetadataPath ?? process.cwd(),
98113
);
99114

100115
if (!this.configPath) return;
101116

102117
const content = await readFile(this.configPath, "utf-8");
103118
const parsed = parseJSON(content) as unknown as Partial<Config>;
104119

105-
if (packageJSONPath) {
106-
try {
107-
const packageJSONContent = await readFile(packageJSONPath, "utf-8");
108-
const packageJSONParsed = parseJSON(
109-
packageJSONContent,
110-
) as unknown as {
111-
version: string;
112-
};
113-
this.clientVersion = packageJSONParsed.version;
114-
} catch {
115-
// Should not be the case, but no package.json found
116-
}
117-
}
118-
119120
// Normalize values
120121
if (parsed.baseUrl)
121122
parsed.baseUrl = stripTrailingSlash(parsed.baseUrl.trim());

0 commit comments

Comments
 (0)