Skip to content

Commit 2107673

Browse files
committed
fix: improved env loader
1 parent 5803ad9 commit 2107673

File tree

10 files changed

+39
-41
lines changed

10 files changed

+39
-41
lines changed

apps/test-bot/src/app.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Client } from 'discord.js';
22

3-
process.loadEnvFile();
4-
53
const client = new Client({
64
intents: ['Guilds', 'GuildMembers', 'GuildMessages', 'MessageContent'],
75
});

packages/commandkit/bin/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
22

3-
import { bootstrapCommandkitCLI } from '../dist/index.mjs';
3+
import { bootstrapCommandkitCLI } from '../dist/index.js';
44

55
await bootstrapCommandkitCLI(process.argv);

packages/commandkit/src/cli/app-process.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@ export function createAppProcess(
99
cwd: string,
1010
isDev: boolean,
1111
) {
12-
const envFiles = isDev ? devEnvFileArgs(cwd) : prodEnvFileArgs(cwd);
13-
1412
if (!existsSync(join(cwd, fileName))) {
1513
panic(`Could not locate the entrypoint file: ${fileName}`);
1614
}
1715

1816
const ps = spawn(
1917
'node',
2018
[
21-
...envFiles,
2219
`--title="CommandKit ${isDev ? 'Development' : 'Production'}"`,
2320
'--enable-source-maps',
2421
fileName,
@@ -27,7 +24,7 @@ export function createAppProcess(
2724
cwd: cwd,
2825
windowsHide: true,
2926
env: DevEnv(),
30-
stdio: 'pipe',
27+
stdio: ['pipe', 'pipe', 'pipe'],
3128
},
3229
);
3330

packages/commandkit/src/cli/build.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { loadConfigFile } from '../config/loader';
88
import { writeFile } from 'node:fs/promises';
99
import { join } from 'node:path';
10+
import { devEnvFileArgs, prodEnvFileArgs } from './env';
1011

1112
export interface ApplicationBuildOptions {
1213
plugins?: CompilerPlugin[];
@@ -80,6 +81,16 @@ export async function buildApplication({
8081
}
8182
}
8283

84+
const envScript = (dev: boolean) => `// --- Environment Variables Loader ---
85+
const $env = [${(dev ? devEnvFileArgs : prodEnvFileArgs).map((p) => `"${p}"`).join(', ')}];
86+
for (const file of $env) {
87+
try {
88+
process.loadEnvFile(file);
89+
console.log('\\x1b[36m✔ Loaded \\x1b[0m\\x1b[33m%s\\x1b[0m', file);
90+
} catch {}
91+
}
92+
`;
93+
8394
const requireScript = [
8495
'// --- CommandKit require() polyfill ---',
8596
' if (typeof require === "undefined") {',
@@ -123,15 +134,20 @@ async function injectEntryFile(
123134
distDir?: string,
124135
) {
125136
const code = `/* Entrypoint File Generated By CommandKit */
126-
${isDev ? `\n\n// Injected for development\n${wrapInAsyncIIFE([antiCrashScript, requireScript])}\n\n` : ''}
137+
${isDev ? `\n\n// Injected for development\n${wrapInAsyncIIFE([envScript(isDev), antiCrashScript, requireScript])}\n\n` : wrapInAsyncIIFE([envScript(isDev), requireScript])}
138+
127139
import { CommandKit } from 'commandkit';
128140
import app from './app.js';
129141
130-
const commandkit = new CommandKit({
131-
client: app,
132-
});
142+
async function main() {
143+
const commandkit = new CommandKit({
144+
client: app,
145+
});
146+
147+
await commandkit.start();
148+
}
133149
134-
await commandkit.start();
150+
await main();
135151
`;
136152

137153
const dist = isDev ? '.commandkit' : distDir || 'dist';

packages/commandkit/src/cli/development.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function bootstrapDevelopmentServer(configPath?: string) {
5252
`${colors.cyanBright('Reloading command(s) at ')} ${colors.yellowBright(path)}`,
5353
);
5454
await buildAndStart(cwd, true);
55-
ps.stdin.write(`COMMANDKIT_EVENT=reload-commands|${path}`);
55+
ps.stdin.write(`COMMANDKIT_EVENT=reload-commands|${path}\n`);
5656
return true;
5757
}
5858

@@ -61,7 +61,7 @@ export async function bootstrapDevelopmentServer(configPath?: string) {
6161
`${colors.cyanBright('Reloading event(s) at ')} ${colors.yellowBright(path)}`,
6262
);
6363
await buildAndStart(cwd, true);
64-
ps.stdin.write(`COMMANDKIT_EVENT=reload-events|${path}`);
64+
ps.stdin.write(`COMMANDKIT_EVENT=reload-events|${path}\n`);
6565
return true;
6666
}
6767

@@ -70,7 +70,7 @@ export async function bootstrapDevelopmentServer(configPath?: string) {
7070
`${colors.cyanBright('Reloading locale(s) at ')} ${colors.yellowBright(path)}`,
7171
);
7272
await buildAndStart(cwd, true);
73-
ps.stdin.write(`COMMANDKIT_EVENT=reload-locales|${path}`);
73+
ps.stdin.write(`COMMANDKIT_EVENT=reload-locales|${path}\n`);
7474
return true;
7575
}
7676

@@ -97,15 +97,15 @@ export async function bootstrapDevelopmentServer(configPath?: string) {
9797
break;
9898
case 'rc':
9999
console.log(`Received reload commands command, reloading...`);
100-
ps?.stdin.write(`COMMANDKIT_EVENT=reload-commands`);
100+
ps?.stdin.write(`COMMANDKIT_EVENT=reload-commands\n`);
101101
break;
102102
case 're':
103103
console.log(`Received reload events command, reloading...`);
104-
ps?.stdin.write(`COMMANDKIT_EVENT=reload-events`);
104+
ps?.stdin.write(`COMMANDKIT_EVENT=reload-events\n`);
105105
break;
106106
case 'rl':
107107
console.log(`Received reload locales command, reloading...`);
108-
ps?.stdin.write(`COMMANDKIT_EVENT=reload-locales`);
108+
ps?.stdin.write(`COMMANDKIT_EVENT=reload-locales\n`);
109109
break;
110110
}
111111
});

packages/commandkit/src/cli/env.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ export const ProdEnvFiles = [
2626
'.env.local',
2727
];
2828

29-
export const devEnvFileArgs = (cwd: string) =>
30-
[...CommonEnvFiles, ...DevEnvFiles].map(
31-
(file) => `--env-file-if-exists="${join(cwd, file)}"`,
32-
);
29+
export const devEnvFileArgs = [...CommonEnvFiles, ...DevEnvFiles];
3330

34-
export const prodEnvFileArgs = (cwd: string) =>
35-
[...CommonEnvFiles, ...ProdEnvFiles].map(
36-
(file) => `--env-file-if-exists=${join(cwd, file)}`,
37-
);
31+
export const prodEnvFileArgs = [...CommonEnvFiles, ...ProdEnvFiles];

packages/commandkit/src/plugins/runtime/CommandKitPluginRuntime.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ export class CommandKitPluginRuntime extends EventTarget {
1818

1919
public constructor(public readonly commandkit: CommandKit) {
2020
super();
21-
22-
this.commandkit.client.on(Events.ClientReady, this.onClientLogin);
21+
// this.commandkit.client.on(Events.ClientReady, this.onClientLogin);
2322
}
2423

2524
public getPlugin(pluginName: string): RuntimePlugin | null {

packages/commandkit/src/plugins/runtime/builtin/MacroPlugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { MacroTransformer } from 'use-macro';
21
import {
32
CompilerPlugin,
43
MaybeFalsey,
@@ -9,10 +8,11 @@ import {
98
export class MacroPlugin extends CompilerPlugin {
109
public readonly name = 'MacroPlugin';
1110

12-
private macroTransformer!: MacroTransformer;
11+
private macroTransformer!: import('use-macro').MacroTransformer;
1312

1413
public async activate(): Promise<void> {
15-
this.macroTransformer = new MacroTransformer();
14+
const transform = await import('use-macro');
15+
this.macroTransformer = new transform.MacroTransformer();
1616
}
1717

1818
public async deactivate(): Promise<void> {

packages/commandkit/src/utils/dev-hooks.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,11 @@ export function registerDevHooks(commandkit: CommandKit) {
1111
const input = chunk.toString().trim();
1212

1313
const match = input.match(EVENT_PATTERN);
14-
15-
console.log({ match });
16-
1714
if (!match) return;
1815

1916
const [, event, , path] = match;
20-
21-
void path;
22-
2317
Logger.info(`Received HMR event: ${event}${path ? ` for ${path}` : ''}`);
2418

25-
// TODO: Implement reload logic
26-
2719
switch (event) {
2820
case 'reload-commands':
2921
commandkit.commandHandler.reloadCommands();
@@ -33,6 +25,7 @@ export function registerDevHooks(commandkit: CommandKit) {
3325
break;
3426
case 'reload-locales':
3527
commandkit.commandHandler.reloadCommands();
28+
break;
3629
}
3730
});
3831
}

packages/commandkit/tsup.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { defineConfig } from 'tsup';
22
import { esbuildPluginUseMacro } from 'use-macro';
33

44
export default defineConfig({
5-
format: ['cjs', 'esm'],
6-
entry: ['src'],
5+
format: ['cjs'],
6+
entry: ['src/index.ts'],
77
outDir: './dist',
88
sourcemap: true,
99
watch: false,
@@ -14,5 +14,6 @@ export default defineConfig({
1414
skipNodeModulesBundle: true,
1515
clean: true,
1616
target: 'node16',
17+
// @ts-ignore
1718
esbuildPlugins: [esbuildPluginUseMacro()],
1819
});

0 commit comments

Comments
 (0)