fix: find nearest deno config with compiler options (#3096)
Previously it would only consult the compiler options in the workspace root deno.json, now it also considers relevant workspace members.
This commit is contained in:
+10
-40
@@ -157,7 +157,9 @@ export class Builder implements FreshBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const denoJson = await readDenoConfigForCompilerOptions(app.config.root);
|
const denoJson = await findNearestDenoConfigWithCompilerOptions(
|
||||||
|
app.config.root,
|
||||||
|
);
|
||||||
|
|
||||||
const jsxImportSource = denoJson.config.compilerOptions?.jsxImportSource;
|
const jsxImportSource = denoJson.config.compilerOptions?.jsxImportSource;
|
||||||
if (jsxImportSource === undefined) {
|
if (jsxImportSource === undefined) {
|
||||||
@@ -231,12 +233,11 @@ export interface DenoConfig {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function readDenoConfigForCompilerOptions(
|
export async function findNearestDenoConfigWithCompilerOptions(
|
||||||
directory: string,
|
directory: string,
|
||||||
): Promise<{ config: DenoConfig; filePath: string }> {
|
): Promise<{ config: DenoConfig; filePath: string }> {
|
||||||
let dir = directory;
|
let dir = directory;
|
||||||
const configs: { config: DenoConfig; filePath: string }[] = [];
|
while (true) {
|
||||||
outer: while (true) {
|
|
||||||
for (const name of ["deno.json", "deno.jsonc"]) {
|
for (const name of ["deno.json", "deno.jsonc"]) {
|
||||||
const filePath = path.join(dir, name);
|
const filePath = path.join(dir, name);
|
||||||
try {
|
try {
|
||||||
@@ -247,10 +248,8 @@ export async function readDenoConfigForCompilerOptions(
|
|||||||
} else {
|
} else {
|
||||||
config = JSON.parse(file);
|
config = JSON.parse(file);
|
||||||
}
|
}
|
||||||
configs.push({ config, filePath });
|
if (config.compilerOptions) return { config, filePath };
|
||||||
if (config.workspace) {
|
if (config.workspace) break;
|
||||||
break outer;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!(err instanceof Deno.errors.NotFound)) {
|
if (!(err instanceof Deno.errors.NotFound)) {
|
||||||
@@ -263,36 +262,7 @@ export async function readDenoConfigForCompilerOptions(
|
|||||||
dir = parent;
|
dir = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (configs.length === 0) {
|
throw new Error(
|
||||||
throw new Error(
|
`Could not find a deno.json or deno.jsonc file in the current directory or any parent directory that contains a 'compilerOptions' field.`,
|
||||||
`Could not find a deno.json or deno.jsonc file in the current directory or any parent directory.`,
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const firstConfig = configs[0];
|
|
||||||
const lastConfig = configs.at(-1);
|
|
||||||
if (lastConfig?.config.workspace) {
|
|
||||||
if (lastConfig === firstConfig) return lastConfig;
|
|
||||||
if (!Array.isArray(lastConfig.config.workspace)) {
|
|
||||||
throw new Error(
|
|
||||||
`Expected "workspace" option to be an array in: ${lastConfig.filePath}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const members = lastConfig.config.workspace.map((member) => {
|
|
||||||
if (typeof member !== "string") {
|
|
||||||
throw new Error(
|
|
||||||
`Expected "workspace" member to be a string in: ${lastConfig.filePath}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return path.join(lastConfig.filePath, "..", member);
|
|
||||||
});
|
|
||||||
const parent = path.dirname(firstConfig.filePath);
|
|
||||||
if (!members.includes(parent)) {
|
|
||||||
return firstConfig;
|
|
||||||
} else {
|
|
||||||
return lastConfig;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return firstConfig;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user