stackpatch 1.1.7 → 1.1.8

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.
Files changed (2) hide show
  1. package/bin/stackpatch.js +28 -89
  2. package/package.json +3 -3
package/bin/stackpatch.js CHANGED
@@ -4,19 +4,24 @@
4
4
  *
5
5
  * This wrapper detects the runtime (Bun or Node.js) and executes the TypeScript file accordingly.
6
6
  * - If Bun is available, uses Bun directly (faster)
7
- * - Otherwise, uses tsx for Node.js compatibility
7
+ * - Otherwise, uses tsx via npx for Node.js compatibility
8
8
  */
9
9
 
10
10
  import { spawn, spawnSync } from "child_process";
11
11
  import { fileURLToPath } from "url";
12
12
  import { dirname, join } from "path";
13
13
  import { existsSync } from "fs";
14
- import { createRequire } from "module";
15
14
 
16
15
  const __filename = fileURLToPath(import.meta.url);
17
16
  const __dirname = dirname(__filename);
18
17
  const tsFile = join(__dirname, "stackpatch.ts");
19
18
 
19
+ // Verify the TypeScript file exists
20
+ if (!existsSync(tsFile)) {
21
+ console.error(`Error: TypeScript file not found: ${tsFile}`);
22
+ process.exit(1);
23
+ }
24
+
20
25
  // Check if Bun is available
21
26
  function hasBun() {
22
27
  try {
@@ -30,114 +35,48 @@ function hasBun() {
30
35
  }
31
36
  }
32
37
 
33
- // Find tsx executable
34
- function findTsx() {
35
- // First, try to resolve tsx from the package's node_modules using require.resolve
36
- // This works when the package is installed locally or via npx
37
- try {
38
- const require = createRequire(import.meta.url);
39
-
40
- // Try to resolve tsx/cli.mjs directly
41
- try {
42
- const tsxBinPath = require.resolve("tsx/cli.mjs");
43
- if (existsSync(tsxBinPath)) {
44
- return ["node", tsxBinPath];
45
- }
46
- } catch {
47
- // tsx/cli.mjs not found
48
- }
49
-
50
- // Try to find tsx package and locate cli.mjs
51
- try {
52
- const tsxPackagePath = require.resolve("tsx/package.json");
53
- const tsxDir = dirname(tsxPackagePath);
54
- const tsxBin = join(tsxDir, "cli.mjs");
55
-
56
- if (existsSync(tsxBin)) {
57
- return ["node", tsxBin];
58
- }
59
- } catch {
60
- // tsx package not found
61
- }
62
- } catch {
63
- // require.resolve failed - package might not be installed yet
64
- }
65
-
66
- // Try node_modules/.bin/tsx in various locations
67
- const possibleTsxPaths = [
68
- join(__dirname, "../node_modules/.bin/tsx"),
69
- join(__dirname, "../../node_modules/.bin/tsx"),
70
- join(process.cwd(), "node_modules/.bin/tsx"),
71
- ];
72
-
73
- for (const tsxPath of possibleTsxPaths) {
74
- if (existsSync(tsxPath)) {
75
- return [tsxPath];
76
- }
77
- }
78
-
79
- // Final fallback: use npx to download and run tsx
80
- // This is the most reliable method when package is downloaded via npx
81
- return ["npx", "-y", "tsx"];
82
- }
83
-
84
38
  // Execute with appropriate runtime
85
39
  const args = process.argv.slice(2);
86
40
 
87
41
  if (hasBun()) {
88
- // Use Bun if available
42
+ // Use Bun if available - Bun can execute TypeScript directly
89
43
  const bun = spawn("bun", [tsFile, ...args], {
90
44
  stdio: "inherit",
91
45
  cwd: process.cwd(),
92
46
  });
93
- bun.on("exit", (code) => process.exit(code || 0));
47
+
48
+ bun.on("exit", (code) => {
49
+ process.exit(code || 0);
50
+ });
51
+
94
52
  bun.on("error", (err) => {
95
53
  console.error("Error running Bun:", err.message);
96
- process.exit(1);
54
+ // Fallback to npx tsx
55
+ console.log("Falling back to Node.js with tsx...");
56
+ runWithTsx();
97
57
  });
98
58
  } else {
99
59
  // Use tsx for Node.js
100
- // Try to find tsx locally first, then fall back to npx
101
- const tsxCommand = findTsx();
60
+ runWithTsx();
61
+ }
102
62
 
103
- // If we're using npx, it will handle downloading tsx if needed
104
- // This is the most reliable method when package is downloaded via npx
105
- const tsx = spawn(tsxCommand[0], [...tsxCommand.slice(1), tsFile, ...args], {
63
+ function runWithTsx() {
64
+ // Always use npx -y tsx - it's the most reliable method
65
+ // npx will download tsx if needed, and it works in all scenarios
66
+ const tsx = spawn("npx", ["-y", "tsx", tsFile, ...args], {
106
67
  stdio: "inherit",
107
68
  cwd: process.cwd(),
108
- shell: tsxCommand[0] === "npx", // Use shell for npx to handle PATH correctly
69
+ shell: true, // Use shell for npx to handle PATH correctly
109
70
  });
110
71
 
111
72
  tsx.on("exit", (code) => {
112
- if (code !== 0 && code !== null) {
113
- process.exit(code);
114
- }
115
- process.exit(0);
73
+ process.exit(code || 0);
116
74
  });
117
75
 
118
76
  tsx.on("error", (err) => {
119
- // If npx fails, try one more time with explicit node
120
- if (tsxCommand[0] !== "npx") {
121
- console.error("Error: Could not execute TypeScript file.");
122
- console.error("Trying with npx...");
123
-
124
- const npxTsx = spawn("npx", ["-y", "tsx", tsFile, ...args], {
125
- stdio: "inherit",
126
- cwd: process.cwd(),
127
- shell: true,
128
- });
129
-
130
- npxTsx.on("exit", (code) => process.exit(code || 0));
131
- npxTsx.on("error", () => {
132
- console.error("Error: Could not execute TypeScript file.");
133
- console.error("Please ensure tsx is available: npm install -g tsx");
134
- process.exit(1);
135
- });
136
- } else {
137
- console.error("Error: Could not execute TypeScript file.");
138
- console.error("Please ensure tsx is available: npm install -g tsx");
139
- console.error("Original error:", err.message);
140
- process.exit(1);
141
- }
77
+ console.error("Error: Could not execute TypeScript file.");
78
+ console.error("Please ensure Node.js and npm are properly installed.");
79
+ console.error("Original error:", err.message);
80
+ process.exit(1);
142
81
  });
143
82
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "stackpatch",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "Composable frontend features for modern React & Next.js apps - Add authentication, UI components, and more with zero configuration",
5
5
  "main": "bin/stackpatch.ts",
6
6
  "type": "module",
7
7
  "bin": {
8
- "stackpatch": "bin/stackpatch.js",
9
- "create-stackpatch": "bin/stackpatch.js"
8
+ "stackpatch": "bin/stackpatch.ts",
9
+ "create-stackpatch": "bin/stackpatch.ts"
10
10
  },
11
11
  "files": [
12
12
  "bin",