Skip to content

Commit 6063f40

Browse files
authored
fix(cli): fix execution on windows and when prettier is not installed (#2587)
1 parent 6bd1b82 commit 6063f40

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

.changeset/smart-eggs-hammer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
[CLI] - fix execution on windows and when prettier is not installed

packages/thirdweb/src/cli/bin.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,39 @@ async function main() {
2323
}
2424

2525
default: {
26+
const isWindows = /^win/.test(process.platform);
2627
let bunAvailable = false;
27-
try {
28-
const res = execSync("bun --version", {
29-
stdio: "ignore",
30-
encoding: "utf-8",
31-
});
32-
if (typeof res === "string" && res.indexOf(".") > -1) {
33-
bunAvailable = true;
28+
// bun has no windows support yet anyways
29+
if (!isWindows) {
30+
try {
31+
const res = execSync("bun --version", {
32+
stdio: "ignore",
33+
encoding: "utf-8",
34+
});
35+
if (typeof res === "string" && res.indexOf(".") > -1) {
36+
bunAvailable = true;
37+
}
38+
} catch {
39+
bunAvailable = false;
3440
}
35-
} catch {
36-
bunAvailable = false;
3741
}
38-
const runner = bunAvailable ? "bunx" : "npx";
42+
let runner = "npx";
43+
44+
switch (true) {
45+
case bunAvailable:
46+
runner = "bun";
47+
break;
48+
case isWindows:
49+
runner = "npx.cmd";
50+
break;
51+
}
52+
53+
const args = command
54+
? ["--yes", "@thirdweb-dev/cli@latest", command, ...rest]
55+
: ["--yes", "@thirdweb-dev/cli@latest", ...rest];
56+
3957
// eslint-disable-next-line better-tree-shaking/no-top-level-side-effects
40-
spawn(runner, ["--yes", "@thirdweb-dev/cli@latest", command, ...rest], {
58+
spawn(runner, args, {
4159
stdio: "inherit",
4260
});
4361
}

packages/thirdweb/src/cli/commands/generate/generate.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import {
1212
type AbiFunction,
1313
formatAbiItem,
1414
} from "abitype";
15-
import { format } from "prettier";
15+
1616
import { prepareMethod } from "../../../utils/abi/prepare-method.js";
1717
import { mkdir, writeFile } from "node:fs/promises";
1818
import { packageDirectory } from "./utils.js";
1919
import { join } from "node:path";
2020
import { existsSync } from "node:fs";
21+
import type { Options } from "prettier";
2122

2223
const client = createThirdwebClient({ clientId: "test" });
2324

@@ -150,7 +151,7 @@ async function generateFromAbi(abi: Abi | string[]) {
150151
);
151152
}
152153

153-
const prettified = await format(body, {
154+
const prettified = await prettifyCode(body, {
154155
parser: "babel-ts",
155156
});
156157
return prettified;
@@ -330,3 +331,18 @@ function lowercaseFirstLetter(str: string) {
330331
function eventNameToPreparedEventName(name: string) {
331332
return `${lowercaseFirstLetter(name)}Event`;
332333
}
334+
335+
const printedPrettierWarning = false;
336+
337+
async function prettifyCode(code: string, options: Options) {
338+
try {
339+
const { format } = await import("prettier/standalone.js");
340+
return await format(code, options);
341+
} catch (e) {
342+
if (!printedPrettierWarning) {
343+
console.info("Prettier not found, skipping code formatting.");
344+
}
345+
}
346+
347+
return code;
348+
}

0 commit comments

Comments
 (0)