appflare 0.2.51 → 0.2.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,13 @@
1
1
  import chokidar from "chokidar";
2
- import { existsSync } from "node:fs";
3
2
  import { dirname, resolve } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
4
  import { generateArtifacts } from "../generate";
5
5
  import { loadConfig } from "../load-config";
6
+ import {
7
+ findNearestPackageDir,
8
+ packageSearchDirs,
9
+ resolvePackageBin,
10
+ } from "../utils/resolve-package";
6
11
 
7
12
  type MigrateOptions = {
8
13
  local?: boolean;
@@ -10,23 +15,6 @@ type MigrateOptions = {
10
15
  preview?: boolean;
11
16
  };
12
17
 
13
- function findNearestPackageDir(startDir: string): string {
14
- let currentDir = startDir;
15
-
16
- while (true) {
17
- if (existsSync(resolve(currentDir, "package.json"))) {
18
- return currentDir;
19
- }
20
-
21
- const parentDir = dirname(currentDir);
22
- if (parentDir === currentDir) {
23
- return startDir;
24
- }
25
-
26
- currentDir = parentDir;
27
- }
28
- }
29
-
30
18
  export async function runBuild(
31
19
  configPath?: string,
32
20
  options: { build?: boolean } = {},
@@ -123,9 +111,18 @@ export async function runMigrate(
123
111
  loadedConfig.outDirAbs,
124
112
  "drizzle.config.js",
125
113
  );
126
- const npxCmd = process.platform === "win32" ? "npx.cmd" : "npx";
114
+ const moduleDir = dirname(fileURLToPath(import.meta.url));
115
+ const searchDirs = packageSearchDirs(moduleDir, [
116
+ loadedConfig.configDir,
117
+ packageDir,
118
+ ]);
119
+ const drizzleKitBin = resolvePackageBin(
120
+ "drizzle-kit",
121
+ "bin.cjs",
122
+ searchDirs,
123
+ );
127
124
  const drizzleGenerate = Bun.spawn(
128
- [npxCmd, "drizzle-kit", "generate", "--config", drizzleConfigPath],
125
+ [process.execPath, drizzleKitBin, "generate", "--config", drizzleConfigPath],
129
126
  {
130
127
  cwd: packageDir,
131
128
  stdin: "inherit",
@@ -142,9 +139,14 @@ export async function runMigrate(
142
139
  }
143
140
 
144
141
  const databaseName = loadedConfig.config.database[0].databaseName;
145
- const wranglerArgs = [
146
- npxCmd,
142
+ const wranglerBin = resolvePackageBin(
147
143
  "wrangler",
144
+ "bin/wrangler.js",
145
+ searchDirs,
146
+ );
147
+ const wranglerArgs = [
148
+ process.execPath,
149
+ wranglerBin,
148
150
  "d1",
149
151
  "migrations",
150
152
  "apply",
@@ -213,10 +215,16 @@ export async function runAddAdmin(
213
215
  ].join(" ");
214
216
 
215
217
  const databaseName = loadedConfig.config.database[0].databaseName;
216
- const npxCmd = process.platform === "win32" ? "npx.cmd" : "npx";
217
- const wranglerArgs = [
218
- npxCmd,
218
+ const moduleDir = dirname(fileURLToPath(import.meta.url));
219
+ const searchDirs = packageSearchDirs(moduleDir, [loadedConfig.configDir]);
220
+ const wranglerBin = resolvePackageBin(
219
221
  "wrangler",
222
+ "bin/wrangler.js",
223
+ searchDirs,
224
+ );
225
+ const wranglerArgs = [
226
+ process.execPath,
227
+ wranglerBin,
220
228
  "d1",
221
229
  "execute",
222
230
  databaseName,
package/cli/generate.ts CHANGED
@@ -14,6 +14,10 @@ import type { LoadedAppflareConfig } from "./types";
14
14
  import { discoverHandlerOperations } from "./utils/handler-discovery";
15
15
  import { discoverSchema } from "./utils/schema-discovery";
16
16
  import { ensureRelativeImportPath } from "./utils/path-utils";
17
+ import {
18
+ packageSearchDirs,
19
+ resolvePackageBin,
20
+ } from "./utils/resolve-package";
17
21
 
18
22
  function extractRolesFromConfig(config: LoadedAppflareConfig["config"]): string[] {
19
23
  const plugins = (config.auth.options as Record<string, unknown>).plugins;
@@ -103,15 +107,16 @@ export async function generateArtifacts(
103
107
  process.stdout.write(`📁 Directories (${(performance.now() - t0).toFixed(0)}ms)\n`);
104
108
 
105
109
  const moduleDir = dirname(fileURLToPath(import.meta.url));
106
- const tscBin = join(
107
- dirname(Bun.resolveSync("typescript/package.json", moduleDir)),
108
- "bin",
109
- "tsc",
110
+ const searchDirs = packageSearchDirs(moduleDir, [configDir]);
111
+ const tscBin = resolvePackageBin(
112
+ "typescript",
113
+ "bin/tsc",
114
+ searchDirs,
110
115
  );
111
- const betterAuthBin = join(
112
- dirname(Bun.resolveSync("@better-auth/cli/package.json", moduleDir)),
113
- "dist",
114
- "index.mjs",
116
+ const betterAuthBin = resolvePackageBin(
117
+ "@better-auth/cli",
118
+ "dist/index.mjs",
119
+ searchDirs,
115
120
  );
116
121
 
117
122
  const serverPath = resolve(outDirAbs, "server.ts");
@@ -0,0 +1,59 @@
1
+ import { existsSync } from "node:fs";
2
+ import { dirname, resolve } from "node:path";
3
+
4
+ export function findNearestPackageDir(startDir: string): string {
5
+ let currentDir = startDir;
6
+
7
+ while (true) {
8
+ if (existsSync(resolve(currentDir, "package.json"))) {
9
+ return currentDir;
10
+ }
11
+
12
+ const parentDir = dirname(currentDir);
13
+ if (parentDir === currentDir) {
14
+ return startDir;
15
+ }
16
+
17
+ currentDir = parentDir;
18
+ }
19
+ }
20
+
21
+ export function resolvePackageRoot(
22
+ packageName: string,
23
+ searchDirs: string[],
24
+ ): string {
25
+ for (const dir of searchDirs) {
26
+ try {
27
+ return dirname(Bun.resolveSync(`${packageName}/package.json`, dir));
28
+ } catch {
29
+ continue;
30
+ }
31
+ }
32
+
33
+ throw new Error(
34
+ `Could not resolve "${packageName}". Install it in your project (see appflare peerDependencies).`,
35
+ );
36
+ }
37
+
38
+ export function resolvePackageBin(
39
+ packageName: string,
40
+ binRelativePath: string,
41
+ searchDirs: string[],
42
+ ): string {
43
+ return resolve(
44
+ resolvePackageRoot(packageName, searchDirs),
45
+ binRelativePath,
46
+ );
47
+ }
48
+
49
+ export function packageSearchDirs(
50
+ moduleDir: string,
51
+ extraDirs: string[] = [],
52
+ ): string[] {
53
+ const dirs = [
54
+ moduleDir,
55
+ findNearestPackageDir(process.cwd()),
56
+ ...extraDirs,
57
+ ];
58
+ return [...new Set(dirs)];
59
+ }