Skip to content

Commit 6bd4f00

Browse files
committed
fix: find nearest deno config with compiler options
Previously it would only consult the compiler options in the workspace root deno.json, now it also considers relevant workspace members.
1 parent c11cf7b commit 6bd4f00

File tree

1 file changed

+8
-40
lines changed

1 file changed

+8
-40
lines changed

src/dev/builder.ts

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class Builder implements FreshBuilder {
157157
}
158158
}
159159

160-
const denoJson = await readDenoConfigForCompilerOptions(app.config.root);
160+
const denoJson = await findNearestDenoConfigWithCompilerOptions(app.config.root);
161161

162162
const jsxImportSource = denoJson.config.compilerOptions?.jsxImportSource;
163163
if (jsxImportSource === undefined) {
@@ -231,12 +231,11 @@ export interface DenoConfig {
231231
};
232232
}
233233

234-
export async function readDenoConfigForCompilerOptions(
234+
export async function findNearestDenoConfigWithCompilerOptions(
235235
directory: string,
236236
): Promise<{ config: DenoConfig; filePath: string }> {
237237
let dir = directory;
238-
const configs: { config: DenoConfig; filePath: string }[] = [];
239-
outer: while (true) {
238+
while (true) {
240239
for (const name of ["deno.json", "deno.jsonc"]) {
241240
const filePath = path.join(dir, name);
242241
try {
@@ -247,10 +246,8 @@ export async function readDenoConfigForCompilerOptions(
247246
} else {
248247
config = JSON.parse(file);
249248
}
250-
configs.push({ config, filePath });
251-
if (config.workspace) {
252-
break outer;
253-
}
249+
if (config.compilerOptions) return { config, filePath };
250+
if (config.workspace) break;
254251
break;
255252
} catch (err) {
256253
if (!(err instanceof Deno.errors.NotFound)) {
@@ -263,36 +260,7 @@ export async function readDenoConfigForCompilerOptions(
263260
dir = parent;
264261
}
265262

266-
if (configs.length === 0) {
267-
throw new Error(
268-
`Could not find a deno.json or deno.jsonc file in the current directory or any parent directory.`,
269-
);
270-
}
271-
272-
const firstConfig = configs[0];
273-
const lastConfig = configs.at(-1);
274-
if (lastConfig?.config.workspace) {
275-
if (lastConfig === firstConfig) return lastConfig;
276-
if (!Array.isArray(lastConfig.config.workspace)) {
277-
throw new Error(
278-
`Expected "workspace" option to be an array in: ${lastConfig.filePath}`,
279-
);
280-
}
281-
const members = lastConfig.config.workspace.map((member) => {
282-
if (typeof member !== "string") {
283-
throw new Error(
284-
`Expected "workspace" member to be a string in: ${lastConfig.filePath}`,
285-
);
286-
}
287-
return path.join(lastConfig.filePath, "..", member);
288-
});
289-
const parent = path.dirname(firstConfig.filePath);
290-
if (!members.includes(parent)) {
291-
return firstConfig;
292-
} else {
293-
return lastConfig;
294-
}
295-
}
296-
297-
return firstConfig;
263+
throw new Error(
264+
`Could not find a deno.json or deno.jsonc file in the current directory or any parent directory that contains a 'compilerOptions' field.`,
265+
);
298266
}

0 commit comments

Comments
 (0)