Skip to content

Commit 61141c6

Browse files
authored
Merge pull request #656 from 2chanhaeng/main
Fixed errors occurring when using certain web framework integration packages and MySQL options simultaneously in `@fedify/init`
2 parents 53d8840 + bef0b7f commit 61141c6

File tree

20 files changed

+318
-242
lines changed

20 files changed

+318
-242
lines changed

CHANGES.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ To be released.
4747
[#601]: https://github.com/fedify-dev/fedify/pull/601
4848
[#652]: https://github.com/fedify-dev/fedify/pull/652
4949

50+
### @fedify/init
51+
52+
- Fixed errors when using `fedify init` with certain web framework
53+
integration packages (Astro, ElysiaJS, Nitro) alongside `@fedify/mysql`.
54+
Environment variables are now properly loaded at runtime, resolving the
55+
`TypeError: Cannot read properties of undefined` from `mysql2`.
56+
[[#649], [#656] by ChanHaeng Lee]
57+
58+
[#649]: https://github.com/fedify-dev/fedify/issues/649
59+
[#656]: https://github.com/fedify-dev/fedify/pull/656
60+
5061

5162
Version 2.1.4
5263
-------------

cspell.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
"dereferenceable",
2929
"discoverability",
3030
"docloader",
31+
"dotenvx",
3132
"draft-cavage",
3233
"eddsa",
3334
"elysia",
35+
"elysiajs",
3436
"fanout",
3537
"federatable",
3638
"Federatable",

mise.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ run = "deno run -A examples/test-examples/mod.ts"
176176
[tasks."test:init"]
177177
description = "Run tests for the init package"
178178
run = "deno task -f @fedify/init test-init"
179+
depends = ["prepare"]
179180

180181
# Snapshot updates
181182
# Note: vocab uses @std/testing/snapshot which only works on Deno

packages/init/src/action/deps.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { PACKAGE_VERSION } from "../lib.ts";
1313
import type { InitCommandData, PackageManager } from "../types.ts";
1414
import { merge, replace } from "../utils.ts";
1515
import { getPackagesPath } from "./const.ts";
16-
import { isDeno } from "./utils.ts";
16+
import { isDeno, needsDenoDotenv } from "./utils.ts";
1717

1818
type Deps = Record<string, string>;
1919

@@ -26,16 +26,18 @@ type Deps = Record<string, string>;
2626
* @returns A record of dependencies with their versions
2727
*/
2828
export const getDependencies = (
29-
{ initializer, kv, mq, testMode, packageManager }: Pick<
29+
{ initializer, kv, mq, env, testMode, packageManager }: Pick<
3030
InitCommandData,
31-
"initializer" | "kv" | "mq" | "packageManager" | "testMode"
31+
"initializer" | "kv" | "mq" | "env" | "packageManager" | "testMode"
3232
>,
3333
): Deps =>
3434
pipe(
3535
{
3636
"@fedify/fedify": PACKAGE_VERSION,
3737
"@fedify/vocab": PACKAGE_VERSION,
3838
"@logtape/logtape": deps["@logtape/logtape"],
39+
...(needsDenoDotenv({ packageManager, env }) &&
40+
{ "@std/dotenv": deps["@std/dotenv"] }),
3941
},
4042
merge(initializer.dependencies),
4143
merge(kv.dependencies),

packages/init/src/action/patch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { always, apply, entries, map, pipe, pipeLazy, tap } from "@fxts/core";
22
import { toMerged } from "es-toolkit";
33
import { readFile } from "node:fs/promises";
4-
import { formatJson, merge, replaceAll, set } from "../utils.ts";
54
import { createFile, throwUnlessNotExists } from "../lib.ts";
65
import type { InitCommandData } from "../types.ts";
6+
import { formatJson, merge, replaceAll, set } from "../utils.ts";
77
import {
88
devToolConfigs,
99
loadDenoConfig,

packages/init/src/action/set.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
MessageQueue,
1313
MessageQueueDescription,
1414
WebFrameworkDescription,
15+
WebFrameworkInitializer,
1516
} from "../types.ts";
1617
import webFrameworks from "../webframeworks/mod.ts";
1718

@@ -58,6 +59,11 @@ const setMq = set(
5859
const setEnv = set(
5960
"env",
6061
<
61-
T extends { kv: KvStoreDescription; mq: MessageQueueDescription },
62-
>({ kv, mq }: T) => merge(kv.env)(mq.env),
62+
T extends {
63+
initializer: WebFrameworkInitializer;
64+
kv: KvStoreDescription;
65+
mq: MessageQueueDescription;
66+
},
67+
>({ initializer, kv, mq }: T) =>
68+
merge(initializer.env)(merge(kv.env)(mq.env)),
6369
);

packages/init/src/action/templates.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import { entries, join, map, pipe } from "@fxts/core";
1+
import { concat, entries, join, map, pipe, when } from "@fxts/core";
22
import { toMerged } from "es-toolkit";
3-
import { replace } from "../utils.ts";
43
import { readTemplate } from "../lib.ts";
54
import type { InitCommandData, PackageManager } from "../types.ts";
5+
import { replace } from "../utils.ts";
6+
import { needsDenoDotenv } from "./utils.ts";
67

78
/**
89
* Loads the federation configuration file content from template.
9-
* Reads the default federation template and replaces placeholders with actual configuration values.
10+
* Reads the default federation template and replaces placeholders with actual
11+
* configuration values.
1012
*
11-
* @param param0 - Configuration object containing imports, project name, KV store, message queue, and package manager
13+
* @param param0 - Configuration object containing imports, project name,
14+
* KV store, message queue, and package manager
1215
* @returns The complete federation configuration file content as a string
1316
*/
1417
export const loadFederation = async (
@@ -43,12 +46,19 @@ export const loadLogging = async ({ projectName }: InitCommandData) =>
4346

4447
/**
4548
* Generates import statements for KV store and message queue dependencies.
46-
* Merges imports from both KV and MQ configurations and creates proper ES module import syntax.
49+
* Merges imports from both KV and MQ configurations and creates proper
50+
* ES module import syntax.
4751
*
48-
* @param param0 - Destructured object containing kv and mq configurations
52+
* Destructured parameters:
53+
* - kv: KV store configuration, including module import mappings
54+
* - mq: Message queue configuration, including module import mappings
55+
* - packageManager: Package manager used for environment-specific handling
56+
* - env: Environment variable setup used to determine loading requirements
57+
*
58+
* @param param0 - InitCommandData containing kv, mq, packageManager, and env
4959
* @returns A multi-line string containing all necessary import statements
5060
*/
51-
export const getImports = ({ kv, mq }: InitCommandData) =>
61+
export const getImports = ({ kv, mq, packageManager, env }: InitCommandData) =>
5262
pipe(
5363
toMerged(kv.imports, mq.imports),
5464
entries,
@@ -61,12 +71,17 @@ export const getImports = ({ kv, mq }: InitCommandData) =>
6171
.join(", ")
6272
} from ${JSON.stringify(module)};`
6373
),
74+
when(
75+
() => needsDenoDotenv({ packageManager, env }),
76+
concat(['import "@std/dotenv/load";']),
77+
),
6478
join("\n"),
6579
);
6680

6781
/**
6882
* Converts import mappings to named import string with aliases.
69-
* Creates proper ES module named import syntax, using aliases when the import name differs from the local name.
83+
* Creates proper ES module named import syntax, using aliases when the import
84+
* name differs from the local name.
7085
*
7186
* @param imports - A record mapping import names to their local aliases
7287
* @returns A comma-separated string of named imports with aliases where needed
@@ -81,12 +96,16 @@ export const getAlias = (imports: Record<string, string>) =>
8196

8297
const ENV_REG_EXP = /process\.env\.(\w+)/g;
8398
/**
84-
* Converts Node.js environment variable access to Deno-compatible syntax when needed.
85-
* Transforms `process.env.VAR_NAME` to `Deno.env.get("VAR_NAME")` for Deno projects.
99+
* Converts Node.js environment variable access to Deno-compatible syntax when
100+
* needed.
101+
* Transforms `process.env.VAR_NAME` to `Deno.env.get("VAR_NAME")` for Deno
102+
* projects.
86103
*
87-
* @param obj - The object string containing potential environment variable references
104+
* @param obj - The object string containing potential environment variable
105+
* references
88106
* @param pm - The package manager (runtime) being used
89-
* @returns The converted object string with appropriate environment variable access syntax
107+
* @returns The converted object string with appropriate environment variable
108+
* access syntax
90109
*/
91110
export const convertEnv = (obj: string, pm: PackageManager) =>
92111
pm === "deno" && ENV_REG_EXP.test(obj)

packages/init/src/action/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ export const isDeno = (
1313
{ packageManager }: Pick<InitCommandData, "packageManager">,
1414
) => packageManager === "deno";
1515

16+
/**
17+
* Returns `true` when the `@std/dotenv` dependency should be included:
18+
* the project uses Deno and has at least one environment variable to load.
19+
*/
20+
export const needsDenoDotenv = (
21+
{ packageManager, env }: Pick<InitCommandData, "packageManager" | "env">,
22+
) => packageManager === "deno" && Object.keys(env).length > 0;
23+
1624
/**
1725
* Returns a function that prepends the project directory to a
1826
* `[filename, content]` tuple, resolving the filename into an absolute path.

packages/init/src/templates/bare-bones/main/deno.ts.tpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import "@std/dotenv/load";
21
import { behindProxy } from "@hongminhee/x-forwarded-fetch";
32
import federation from "./federation.ts";
43
import "./logging.ts";

packages/init/src/templates/hono/index/deno.ts.tpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { behindProxy } from "@hongminhee/x-forwarded-fetch";
2-
import "@std/dotenv/load";
32
import app from "./app.tsx";
43
import "./logging.ts";
54

0 commit comments

Comments
 (0)