(dir, isInteractive, retry = true)
| 42 | } |
| 43 | |
| 44 | function checkBrowsers(dir, isInteractive, retry = true) { |
| 45 | const current = browserslist.loadConfig({ path: dir }); |
| 46 | if (current != null) { |
| 47 | return Promise.resolve(current); |
| 48 | } |
| 49 | |
| 50 | if (!retry) { |
| 51 | return Promise.reject( |
| 52 | new Error( |
| 53 | chalk.red( |
| 54 | 'As of react-scripts >=2 you must specify targeted browsers.' |
| 55 | ) + |
| 56 | os.EOL + |
| 57 | `Please add a ${chalk.underline( |
| 58 | 'browserslist' |
| 59 | )} key to your ${chalk.bold('package.json')}.` |
| 60 | ) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | return shouldSetBrowsers(isInteractive).then(shouldSetBrowsers => { |
| 65 | if (!shouldSetBrowsers) { |
| 66 | return checkBrowsers(dir, isInteractive, false); |
| 67 | } |
| 68 | |
| 69 | return ( |
| 70 | pkgUp({ cwd: dir }) |
| 71 | .then(filePath => { |
| 72 | if (filePath == null) { |
| 73 | return Promise.reject(); |
| 74 | } |
| 75 | const pkg = JSON.parse(fs.readFileSync(filePath)); |
| 76 | pkg['browserslist'] = defaultBrowsers; |
| 77 | fs.writeFileSync(filePath, JSON.stringify(pkg, null, 2) + os.EOL); |
| 78 | |
| 79 | browserslist.clearCaches(); |
| 80 | console.log(); |
| 81 | console.log( |
| 82 | `${chalk.green('Set target browsers:')} ${chalk.cyan( |
| 83 | defaultBrowsers.join(', ') |
| 84 | )}` |
| 85 | ); |
| 86 | console.log(); |
| 87 | }) |
| 88 | // Swallow any error |
| 89 | .catch(() => {}) |
| 90 | .then(() => checkBrowsers(dir, isInteractive, false)) |
| 91 | ); |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | module.exports = { defaultBrowsers, checkBrowsers }; |
no test coverage detected