Skip to content

Commit 6b8d87f

Browse files
authored
feat!: prompt user before downloading software (#360)
1 parent c2a6e4f commit 6b8d87f

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ same major line. Should you need to upgrade to a new major, use an explicit
223223
not to lookup on the remote registry for the latest version of the selected
224224
package manager.
225225

226+
- `COREPACK_ENABLE_DOWNLOAD_PROMPT` can be set to `0` to
227+
prevent Corepack showing the URL when it needs to download software, or can be
228+
set to `1` to have the URL shown. By default, when Corepack is called
229+
explicitly (e.g. `corepack pnpm …`), it is set to `0`; when Corepack is called
230+
implicitely (e.g. `pnpm …`), it is set to `1`.
231+
When standard input is a TTY and no CI environment is detected, Corepack will
232+
ask for user input before starting the download.
233+
226234
- `COREPACK_ENABLE_NETWORK` can be set to `0` to prevent Corepack from accessing
227235
the network (in which case you'll be responsible for hydrating the package
228236
manager versions that will be required for the projects you'll run, using

mkshims.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ async function main() {
2121
const corepackPath = path.join(distDir, `corepack.js`);
2222
fs.writeFileSync(corepackPath, [
2323
`#!/usr/bin/env node`,
24+
`process.env.COREPACK_ENABLE_DOWNLOAD_PROMPT??='0';`,
2425
`require('./lib/corepack.cjs').runMain(process.argv.slice(2));`,
2526
].join(`\n`));
2627
fs.chmodSync(corepackPath, 0o755);
@@ -32,6 +33,7 @@ async function main() {
3233
const entryPath = path.join(distDir, `${binaryName}.js`);
3334
const entryScript = [
3435
`#!/usr/bin/env node`,
36+
`process.env.COREPACK_ENABLE_DOWNLOAD_PROMPT??='1'`,
3537
`require('./lib/corepack.cjs').runMain(['${binaryName}', ...process.argv.slice(2)]);`,
3638
].join(`\n`);
3739

sources/httpUtils.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import {UsageError} from 'clipanion';
2-
import {RequestOptions} from 'https';
3-
import {IncomingMessage, ClientRequest} from 'http';
1+
import {UsageError} from 'clipanion';
2+
import {once} from 'events';
3+
import type {RequestOptions} from 'https';
4+
import type {IncomingMessage, ClientRequest} from 'http';
5+
import {stderr, stdin} from 'process';
46

57
export async function fetchUrlStream(url: string, options: RequestOptions = {}) {
68
if (process.env.COREPACK_ENABLE_NETWORK === `0`)
@@ -12,6 +14,22 @@ export async function fetchUrlStream(url: string, options: RequestOptions = {})
1214

1315
const proxyAgent = new ProxyAgent();
1416

17+
if (process.env.COREPACK_ENABLE_DOWNLOAD_PROMPT === `1`) {
18+
console.error(`Corepack is about to download ${url}.`);
19+
if (stdin.isTTY && !process.env.CI) {
20+
stderr.write(`\nDo you want to continue? [Y/n] `);
21+
stdin.resume();
22+
const chars = await once(stdin, `data`);
23+
stdin.pause();
24+
if (
25+
chars[0][0] === 0x6e || // n
26+
chars[0][0] === 0x4e // N
27+
) {
28+
throw new UsageError(`Aborted by the user`);
29+
}
30+
}
31+
}
32+
1533
return new Promise<IncomingMessage>((resolve, reject) => {
1634
const createRequest = (url: string) => {
1735
const request: ClientRequest = https.get(url, {...options, agent: proxyAgent}, response => {

0 commit comments

Comments
 (0)