Skip to content

Commit a6e5da6

Browse files
authored
feat: Infer package manager from environment upon asinit (#1442)
1 parent 61df707 commit a6e5da6

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

NOTICE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ under the licensing terms detailed in LICENSE:
2525
* Guido Zuidhof <[email protected]>
2626
2727
* Andrew Davis <[email protected]>
28+
* Maël Nison <[email protected]>
2829

2930
Portions of this software are derived from third-party works licensed under
3031
the following terms:

bin/asinit

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@ const colors = require("../cli/util/colors");
88
const version = require("../package.json").version;
99
const options = require("../cli/util/options");
1010

11+
const commands = {
12+
"npm": {
13+
install: "npm install",
14+
run: "npm run",
15+
test: "npm test"
16+
},
17+
"yarn": {
18+
install: "yarn install",
19+
run: "yarn",
20+
test: "yarn test"
21+
},
22+
"pnpm": {
23+
install: "pnpm install",
24+
run: "pnpm run",
25+
test: "pnpm test"
26+
}
27+
};
28+
29+
let pm = "npm";
30+
if (typeof process.env.npm_config_user_agent === "string") {
31+
if (/\byarn\//.test(process.env.npm_config_user_agent)) {
32+
pm = "yarn";
33+
} else if (/\bpnpm\//.test(process.env.npm_config_user_agent)) {
34+
pm = "pnpm";
35+
}
36+
}
37+
1138
const asinitOptions = {
1239
"help": {
1340
"category": "General",
@@ -118,14 +145,14 @@ function createProject(answer) {
118145
"",
119146
"Don't forget to install dependencies before you start:",
120147
"",
121-
colors.white(" npm install"),
148+
colors.white(" " + commands[pm].install),
122149
"",
123150
"To edit the entry file, open '" + colors.cyan("assembly/index.ts") + "' in your editor of choice.",
124151
"Create as many additional files as necessary and use them as imports.",
125152
"",
126153
"To build the entry file to WebAssembly when you are ready, run:",
127154
"",
128-
colors.white(" npm run asbuild"),
155+
colors.white(" " + commands[pm].run + " asbuild"),
129156
"",
130157
"Running the command above creates the following binaries incl. their respective",
131158
"text format representations and source maps:",
@@ -146,7 +173,7 @@ function createProject(answer) {
146173
"",
147174
"To run the tests, do:",
148175
"",
149-
colors.white(" npm test"),
176+
colors.white(" " + commands[pm].test),
150177
"",
151178
"The AssemblyScript documentation covers all the details:",
152179
"",
@@ -289,7 +316,7 @@ function ensurePackageJson() {
289316
const entryPath = path.relative(projectDir, entryFile).replace(/\\/g, "/");
290317
const buildUntouched = "asc " + entryPath + " --target debug";
291318
const buildOptimized = "asc " + entryPath + " --target release";
292-
const buildAll = "npm run asbuild:untouched && npm run asbuild:optimized";
319+
const buildAll = commands[pm].run + " asbuild:untouched && " + commands[pm].run + " asbuild:optimized";
293320
if (!fs.existsSync(packageFile)) {
294321
fs.writeFileSync(packageFile, JSON.stringify({
295322
"scripts": {

0 commit comments

Comments
 (0)