Skip to content

Commit 88a3de9

Browse files
committed
Set browsers automatically if not an interactive terminal
1 parent 62b0942 commit 88a3de9

File tree

3 files changed

+51
-40
lines changed

3 files changed

+51
-40
lines changed

packages/react-dev-utils/browsersHelper.js

+47-38
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,26 @@ const fs = require('fs');
1515

1616
const defaultBrowsers = ['>0.25%', 'not op_mini all', 'ie 11'];
1717

18-
function checkBrowsers(dir, retry = true) {
18+
function shouldSetBrowsers(isInteractive) {
19+
if (!isInteractive) {
20+
return Promise.resolve(true);
21+
}
22+
23+
const question = {
24+
type: 'confirm',
25+
name: 'shouldSetBrowsers',
26+
message:
27+
chalk.yellow("We're unable to detect target browsers.") +
28+
`\n\nWould you like to add the defaults to your ${chalk.bold(
29+
'package.json'
30+
)}?`,
31+
default: true,
32+
};
33+
34+
return inquirer.prompt(question).then(answer => answer.shouldSetBrowsers);
35+
}
36+
37+
function checkBrowsers(dir, isInteractive, retry = true) {
1938
const current = browserslist.findConfig(dir);
2039
if (current != null) {
2140
return Promise.resolve(current);
@@ -35,44 +54,34 @@ function checkBrowsers(dir, retry = true) {
3554
);
3655
}
3756

38-
const question = {
39-
type: 'confirm',
40-
name: 'shouldSetBrowsers',
41-
message:
42-
chalk.yellow("We're unable to detect target browsers.") +
43-
`\n\nWould you like to add the defaults to your ${chalk.bold(
44-
'package.json'
45-
)}?`,
46-
default: true,
47-
};
48-
return inquirer.prompt(question).then(answer => {
49-
if (answer.shouldSetBrowsers) {
50-
return (
51-
pkgUp(dir)
52-
.then(filePath => {
53-
if (filePath == null) {
54-
return Promise.reject();
55-
}
56-
const pkg = JSON.parse(fs.readFileSync(filePath));
57-
pkg['browserslist'] = defaultBrowsers;
58-
fs.writeFileSync(filePath, JSON.stringify(pkg, null, 2) + os.EOL);
59-
60-
browserslist.clearCaches();
61-
console.log();
62-
console.log(
63-
`${chalk.green('Set target browsers:')} ${chalk.cyan(
64-
defaultBrowsers.join(', ')
65-
)}`
66-
);
67-
console.log();
68-
})
69-
// Swallow any error
70-
.catch(() => {})
71-
.then(() => checkBrowsers(dir, false))
72-
);
73-
} else {
74-
return checkBrowsers(dir, false);
57+
return shouldSetBrowsers(isInteractive).then(shouldSetBrowsers => {
58+
if (!shouldSetBrowsers) {
59+
return checkBrowsers(dir, isInteractive, false);
7560
}
61+
62+
return (
63+
pkgUp(dir)
64+
.then(filePath => {
65+
if (filePath == null) {
66+
return Promise.reject();
67+
}
68+
const pkg = JSON.parse(fs.readFileSync(filePath));
69+
pkg['browserslist'] = defaultBrowsers;
70+
fs.writeFileSync(filePath, JSON.stringify(pkg, null, 2) + os.EOL);
71+
72+
browserslist.clearCaches();
73+
console.log();
74+
console.log(
75+
`${chalk.green('Set target browsers:')} ${chalk.cyan(
76+
defaultBrowsers.join(', ')
77+
)}`
78+
);
79+
console.log();
80+
})
81+
// Swallow any error
82+
.catch(() => {})
83+
.then(() => checkBrowsers(dir, isInteractive, false))
84+
);
7685
});
7786
}
7887

packages/react-scripts/scripts/build.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const useYarn = fs.existsSync(paths.yarnLockFile);
5151
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
5252
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
5353

54+
const isInteractive = process.stdout.isTTY;
55+
5456
// Warn and crash if required files are missing
5557
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
5658
process.exit(1);
@@ -63,7 +65,7 @@ const writeStatsJson = argv.indexOf('--stats') !== -1;
6365
// We require that you explictly set browsers and do not fall back to
6466
// browserslist defaults.
6567
const { checkBrowsers } = require('react-dev-utils/browsersHelper');
66-
checkBrowsers(paths.appPath)
68+
checkBrowsers(paths.appPath, isInteractive)
6769
.then(() => {
6870
// First, read the current file sizes in build directory.
6971
// This lets us display how much they changed later.

packages/react-scripts/scripts/start.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ if (process.env.HOST) {
7878
// We require that you explictly set browsers and do not fall back to
7979
// browserslist defaults.
8080
const { checkBrowsers } = require('react-dev-utils/browsersHelper');
81-
checkBrowsers(paths.appPath)
81+
checkBrowsers(paths.appPath, isInteractive)
8282
.then(() => {
8383
// We attempt to use the default port but if it is busy, we offer the user to
8484
// run on a different port. `choosePort()` Promise resolves to the next free port.

0 commit comments

Comments
 (0)