Skip to content

Commit 49ad98e

Browse files
Merge pull request #3 from paritytech/yuri/auto-pnpm
Suggesting to install pnpm
2 parents 3018b3f + 46f8894 commit 49ad98e

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/bin/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22
import { ExitPromptError } from "@inquirer/core";
33
import { inquireDappData } from "#src/util/dappData";
4+
import { ensurePnpm } from "#src/util/ensurePnpm";
45
import { spawnTemplate } from "#src/util/spawnTemplate";
56

67
/* This is somewhat a hack to automatically include
@@ -11,6 +12,9 @@ import * as pkg from "../../package.json";
1112

1213
(async () => {
1314
console.log(`${pkg.name} ${pkg.version}`);
15+
16+
await ensurePnpm();
17+
1418
const dappData = await inquireDappData();
1519

1620
await spawnTemplate(dappData);

src/util/ensurePnpm.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { confirm } from "@inquirer/prompts";
2+
import { spawnAndWait } from "#src/util/childProcess";
3+
import { colors as c, success } from "#src/util/log";
4+
import cp from "child_process";
5+
6+
export async function ensurePnpm(): Promise<void> {
7+
console.log(`${c.secondary(`Checking ${c.accent("pnpm")}`)}\n`);
8+
9+
try {
10+
cp.execSync("pnpm --version", { stdio: "ignore" });
11+
12+
console.log(success(`${c.accent("pnpm")} is installed\n`));
13+
return;
14+
} catch (e) {
15+
// Checking that it's an npx's "command not found" error
16+
if (!(e instanceof Error) || !("status" in e) || e.status !== 127) {
17+
throw e;
18+
}
19+
}
20+
21+
const res = await confirm({ message: `The template uses ${c.accent("pnpm")} as a package manager. Install it?` });
22+
23+
if (!res) {
24+
console.error("Abort");
25+
process.exit(1);
26+
}
27+
28+
await spawnAndWait("npm", ["install", "-g", "pnpm"], {});
29+
30+
console.log(success(`${c.accent("pnpm")} has been installed\n`));
31+
}

0 commit comments

Comments
 (0)