Skip to content

Commit 48bba43

Browse files
authored
Forbid cosmiconfig usage (#609)
2 parents a9f43ec + 60a72ad commit 48bba43

2 files changed

Lines changed: 47 additions & 52 deletions

File tree

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Read more:
2121

2222
## v0.18.0
2323

24+
- For consistency, `cosmiconfig` is no longer supported for configuration; use
25+
`graphile.config.ts` instead. (This release will throw errors if you're using
26+
cosmisconfig, in later releases cosmiconfigs will not be detected.)
2427
- Now published as pure ESM, but worry not as unflagged require(ESM) is now
2528
enabled by default in
2629
[Node 20.19.0+](https://nodejs.org/pt-br/blog/release/v20.19.0),

src/config.ts

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,46 @@ import { MINUTE, SECOND } from "./cronConstants.ts";
44
import { defaultLogger } from "./logger.ts";
55

66
const cosmiconfigResult = cosmiconfigSync("graphile-worker").search();
7-
const cosmiconfig = cosmiconfigResult?.config;
7+
8+
if (cosmiconfigResult && !cosmiconfigResult.isEmpty) {
9+
const { config: cosmiconfig, filepath } = cosmiconfigResult;
10+
if (
11+
cosmiconfig.schema != null ||
12+
cosmiconfig.pollInterval != null ||
13+
cosmiconfig.maxPoolSize != null
14+
) {
15+
console.error(
16+
`Cosmiconfig configuration found in ${filepath}. cosmiconfig is no longer supported, please switch to graphile.config.ts - an equivalent configuration might look like:`,
17+
);
18+
console.error();
19+
console.error("```ts");
20+
console.error("// graphile.config.ts");
21+
console.error('import { WorkerPreset } from "graphile-worker";');
22+
console.error("");
23+
console.error("const preset: GraphileConfig.Preset = {");
24+
console.error(" extends: [WorkerPreset],");
25+
console.error(" worker: {");
26+
if (cosmiconfig.schema != null) {
27+
console.error(` schema: ${JSON.stringify(cosmiconfig.schema)},`);
28+
}
29+
if (cosmiconfig.pollInterval != null) {
30+
console.error(
31+
` pollInterval: ${JSON.stringify(cosmiconfig.pollInterval)},`,
32+
);
33+
}
34+
if (cosmiconfig.maxPoolSize != null) {
35+
console.error(
36+
` maxPoolSize: ${JSON.stringify(cosmiconfig.maxPoolSize)},`,
37+
);
38+
}
39+
console.error(" },");
40+
console.error("};");
41+
console.error("");
42+
console.error("export default preset;");
43+
console.error("```");
44+
process.exit(1);
45+
}
46+
}
847

948
/**
1049
* Defaults to use for various options throughout the codebase, sourced from
@@ -13,18 +52,10 @@ const cosmiconfig = cosmiconfigResult?.config;
1352
export const makeWorkerPresetWorkerOptions = () =>
1453
({
1554
connectionString: process.env.DATABASE_URL,
16-
schema:
17-
process.env.GRAPHILE_WORKER_SCHEMA ||
18-
enforceStringOrUndefined("schema", cosmiconfig?.schema) ||
19-
"graphile_worker",
20-
pollInterval:
21-
enforceNumberOrUndefined("pollInterval", cosmiconfig?.pollInterval) ||
22-
2000,
23-
concurrentJobs:
24-
enforceNumberOrUndefined("concurrentJobs", cosmiconfig?.concurrentJobs) ||
25-
1,
26-
maxPoolSize:
27-
enforceNumberOrUndefined("maxPoolSize", cosmiconfig?.maxPoolSize) || 10,
55+
schema: process.env.GRAPHILE_WORKER_SCHEMA || "graphile_worker",
56+
pollInterval: 2000,
57+
concurrentJobs: 1,
58+
maxPoolSize: 10,
2859
preparedStatements: true as boolean,
2960
crontabFile: `${process.cwd()}/crontab`,
3061
taskDirectory: `${process.cwd()}/tasks`,
@@ -35,42 +66,3 @@ export const makeWorkerPresetWorkerOptions = () =>
3566
gracefulShutdownAbortTimeout: 5 * SECOND,
3667
useNodeTime: false,
3768
}) satisfies GraphileConfig.WorkerOptions;
38-
39-
function enforceStringOrUndefined(
40-
keyName: string,
41-
str: unknown,
42-
): string | undefined {
43-
if (typeof str === "string") {
44-
return str;
45-
} else if (!str) {
46-
return undefined;
47-
} else {
48-
throw new Error(
49-
`Expected '${keyName}' to be a string (or not set), but received ${typeof str}`,
50-
);
51-
}
52-
}
53-
54-
function enforceNumberOrUndefined(
55-
keyName: string,
56-
nr: unknown,
57-
): number | undefined {
58-
if (typeof nr === "number") {
59-
return nr;
60-
} else if (typeof nr === "string") {
61-
const val = parseFloat(nr);
62-
if (isFinite(val)) {
63-
return val;
64-
} else {
65-
throw new Error(
66-
`Expected '${keyName}' to be a number (or not set), but received ${nr}`,
67-
);
68-
}
69-
} else if (!nr) {
70-
return undefined;
71-
} else {
72-
throw new Error(
73-
`Expected '${keyName}' to be a number (or not set), but received ${typeof nr}`,
74-
);
75-
}
76-
}

0 commit comments

Comments
 (0)