Skip to content

Commit 57a55ff

Browse files
committed
Imply pnpm, yarn, npm from the executed context
1 parent 7cde12b commit 57a55ff

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@apphosting/create/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "@apphosting/create",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"main": "dist/index.js",
55
"description": "Experimental addon to the Firebase CLI to add web framework support",
66
"repository": {
77
"type": "git",
88
"url": "git+https://github.com/FirebaseExtended/firebase-framework-tools.git"
99
},
1010
"bin": {
11-
"@apphosting/create": "dist/bin/create.js"
11+
"apphosting-create": "dist/bin/create.js"
1212
},
1313
"author": {
1414
"name": "Firebase",

packages/@apphosting/create/src/bin/create.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ import { select } from "@inquirer/prompts";
55
import spawn from "@npmcli/promise-spawn";
66
import ora from "ora";
77

8-
console.log(process);
8+
const contextIsNpmCreate = process.env.npm_command === "init";
9+
10+
// npm/10.1.0 node/v20.9.0 darwin x64 workspaces/false
11+
// pnpm/9.1.0 npm/? node/v20.9.0 darwin x64
12+
// yarn/1.7.0 npm/? node/v8.9.4 darwin x64
13+
const npmUserAgent = process.env.npm_config_user_agent
14+
? Object.fromEntries(process.env.npm_config_user_agent.split(" ").map((s) => s.split("/")))
15+
: {};
916

1017
program
1118
.option("--framework <string>")
1219
.option("--package-manager <string>")
13-
.option("--skip-install")
1420
.argument("<directory>", "path to the project's root directory")
1521
.action(
16-
async (
17-
dir,
18-
{
19-
framework,
20-
packageManager,
21-
skipInstall,
22-
}: { framework?: string; packageManager?: string; skipInstall?: true },
23-
) => {
22+
async (dir, { framework, packageManager }: { framework?: string; packageManager?: string }) => {
2423
// TODO DRY up the validation and error handling, move to commander parse
2524
if (framework) {
2625
if (!["angular", "nextjs"].includes(framework)) {
@@ -36,21 +35,24 @@ program
3635
],
3736
});
3837
}
39-
const cloneSpinner = ora("Cloning template...").start();
40-
// TODO allow different templates
41-
await downloadTemplate(
42-
`gh:FirebaseExtended/firebase-framework-tools/starters/${framework}/basic`,
43-
{ dir, force: true },
44-
);
45-
cloneSpinner.succeed();
4638
// TODO DRY up validation and error message, move to commander parse
39+
let packageManagerVersion = "*";
4740
if (packageManager) {
41+
[packageManager, packageManagerVersion = "*"] = packageManager.split("@");
4842
if (!["npm", "yarn", "pnpm"].includes(packageManager)) {
4943
console.error(
5044
`Invalid package manager: ${packageManager}, valid choices are npm, yarn, and pnpm`,
5145
);
5246
process.exit(1);
5347
}
48+
} else if (contextIsNpmCreate) {
49+
packageManager = "npm";
50+
} else if (npmUserAgent.yarn) {
51+
packageManager = "yarn";
52+
packageManagerVersion = npmUserAgent.yarn;
53+
} else if (npmUserAgent.pnpm) {
54+
packageManager = "pnpm";
55+
packageManagerVersion = npmUserAgent.pnpm;
5456
} else {
5557
packageManager = await select({
5658
message: "Select a package manager",
@@ -62,26 +64,31 @@ program
6264
],
6365
});
6466
}
65-
if (packageManager !== "npm") {
66-
await spawn("corepack", ["enable"], {
67+
const cloneSpinner = ora("Cloning template...").start();
68+
// TODO allow different templates
69+
await downloadTemplate(
70+
`gh:FirebaseExtended/firebase-framework-tools/starters/${framework}/basic`,
71+
{ dir, force: true },
72+
);
73+
cloneSpinner.succeed();
74+
if (packageManager === "npm") {
75+
console.log(`> ${packageManager} install`);
76+
await spawn(packageManager, ["install"], {
6777
shell: true,
6878
stdio: "inherit",
6979
cwd: dir,
7080
});
71-
await spawn("corepack", ["use", `${packageManager}@*`], {
81+
} else {
82+
await spawn("corepack", ["enable"], {
7283
shell: true,
7384
stdio: "inherit",
7485
cwd: dir,
7586
});
76-
}
77-
if (!skipInstall) {
78-
const installSpinner = ora("Installing dependencies...").start();
79-
await spawn(packageManager, ["install"], {
87+
await spawn("corepack", ["use", `${packageManager}@${packageManagerVersion}`], {
8088
shell: true,
8189
stdio: "inherit",
8290
cwd: dir,
8391
});
84-
installSpinner.succeed();
8592
}
8693
},
8794
);

0 commit comments

Comments
 (0)