Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

94 строки
2.3 KiB

  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. 'use strict';
  8. const browserslist = require('browserslist');
  9. const chalk = require('chalk');
  10. const os = require('os');
  11. const inquirer = require('inquirer');
  12. const pkgUp = require('pkg-up');
  13. const fs = require('fs');
  14. const defaultBrowsers = [
  15. '>0.2%',
  16. 'not dead',
  17. 'not ie <= 11',
  18. 'not op_mini all',
  19. ];
  20. function shouldSetBrowsers(isInteractive) {
  21. if (!isInteractive) {
  22. return Promise.resolve(true);
  23. }
  24. const question = {
  25. type: 'confirm',
  26. name: 'shouldSetBrowsers',
  27. message:
  28. chalk.yellow("We're unable to detect target browsers.") +
  29. `\n\nWould you like to add the defaults to your ${chalk.bold(
  30. 'package.json'
  31. )}?`,
  32. default: true,
  33. };
  34. return inquirer.prompt(question).then(answer => answer.shouldSetBrowsers);
  35. }
  36. function checkBrowsers(dir, isInteractive, retry = true) {
  37. const current = browserslist.findConfig(dir);
  38. if (current != null) {
  39. return Promise.resolve(current);
  40. }
  41. if (!retry) {
  42. return Promise.reject(
  43. new Error(
  44. chalk.red(
  45. 'As of react-scripts >=2 you must specify targeted browsers.'
  46. ) +
  47. os.EOL +
  48. `Please add a ${chalk.underline(
  49. 'browserslist'
  50. )} key to your ${chalk.bold('package.json')}.`
  51. )
  52. );
  53. }
  54. return shouldSetBrowsers(isInteractive).then(shouldSetBrowsers => {
  55. if (!shouldSetBrowsers) {
  56. return checkBrowsers(dir, isInteractive, false);
  57. }
  58. return (
  59. pkgUp(dir)
  60. .then(filePath => {
  61. if (filePath == null) {
  62. return Promise.reject();
  63. }
  64. const pkg = JSON.parse(fs.readFileSync(filePath));
  65. pkg['browserslist'] = defaultBrowsers;
  66. fs.writeFileSync(filePath, JSON.stringify(pkg, null, 2) + os.EOL);
  67. browserslist.clearCaches();
  68. console.log();
  69. console.log(
  70. `${chalk.green('Set target browsers:')} ${chalk.cyan(
  71. defaultBrowsers.join(', ')
  72. )}`
  73. );
  74. console.log();
  75. })
  76. // Swallow any error
  77. .catch(() => {})
  78. .then(() => checkBrowsers(dir, isInteractive, false))
  79. );
  80. });
  81. }
  82. module.exports = { defaultBrowsers, checkBrowsers };