|
11 | 11 | const chalk = require('chalk');
|
12 | 12 | const fs = require('fs');
|
13 | 13 | const resolve = require('resolve');
|
| 14 | +const path = require('path'); |
14 | 15 | const paths = require('../../config/paths');
|
| 16 | +const os = require('os'); |
| 17 | + |
| 18 | +function writeJson(fileName, object) { |
| 19 | + fs.writeFileSync(fileName, JSON.stringify(object, null, 2) + os.EOL); |
| 20 | +} |
15 | 21 |
|
16 | 22 | function verifyTypeScriptSetup() {
|
17 | 23 | if (!fs.existsSync(paths.appTsConfig)) {
|
@@ -53,6 +59,106 @@ function verifyTypeScriptSetup() {
|
53 | 59 | console.error();
|
54 | 60 | process.exit(1);
|
55 | 61 | }
|
| 62 | + |
| 63 | + const messages = []; |
| 64 | + let tsconfig; |
| 65 | + try { |
| 66 | + tsconfig = require(paths.appTsConfig); |
| 67 | + } catch (_) { |
| 68 | + console.error( |
| 69 | + chalk.red.bold( |
| 70 | + 'Could not parse', |
| 71 | + chalk.cyan('tsconfig.json') + '.', |
| 72 | + 'Please make sure it contains syntactically correct JSON.' |
| 73 | + ) |
| 74 | + ); |
| 75 | + process.exit(1); |
| 76 | + } |
| 77 | + |
| 78 | + if (tsconfig.compilerOptions == null) { |
| 79 | + tsconfig.compilerOptions = {}; |
| 80 | + } |
| 81 | + |
| 82 | + const compilerOptions = { |
| 83 | + target: { suggested: 'es5' }, |
| 84 | + allowJs: { suggested: true }, |
| 85 | + skipLibCheck: { suggested: true }, |
| 86 | + module: { value: 'esnext', reason: 'for import() and import/export' }, |
| 87 | + moduleResolution: { value: 'node', reason: 'to match webpack resolution' }, |
| 88 | + isolatedModules: { value: true, reason: 'implementation limitation' }, |
| 89 | + noEmit: { value: true }, |
| 90 | + jsx: { value: 'preserve', reason: 'JSX is compiled by Babel' }, |
| 91 | + esModuleInterop: { value: true, reason: 'Babel compatibility' }, |
| 92 | + allowSyntheticDefaultImports: { |
| 93 | + value: true, |
| 94 | + reason: 'Babel compatibility', |
| 95 | + }, |
| 96 | + strict: { suggested: true }, |
| 97 | + }; |
| 98 | + |
| 99 | + for (const option of Object.keys(compilerOptions)) { |
| 100 | + const { value, suggested, reason } = compilerOptions[option]; |
| 101 | + if (suggested != null) { |
| 102 | + if (tsconfig.compilerOptions[option] === undefined) { |
| 103 | + tsconfig.compilerOptions[option] = suggested; |
| 104 | + messages.push( |
| 105 | + `${chalk.cyan('compilerOptions.' + option)} to be ${chalk.bold( |
| 106 | + 'suggested' |
| 107 | + )} value: ${chalk.cyan.bold(suggested)} (this can be changed)` |
| 108 | + ); |
| 109 | + } |
| 110 | + } else if (tsconfig.compilerOptions[option] !== value) { |
| 111 | + tsconfig.compilerOptions[option] = value; |
| 112 | + messages.push( |
| 113 | + `${chalk.cyan('compilerOptions.' + option)} ${chalk.bold( |
| 114 | + 'must' |
| 115 | + )} be ${chalk.cyan.bold(value)}` + |
| 116 | + (reason != null ? ` (${reason})` : '') |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (tsconfig.include == null) { |
| 122 | + tsconfig.include = ['src']; |
| 123 | + messages.push( |
| 124 | + `${chalk.cyan('include')} should be ${chalk.cyan.bold('src')}` |
| 125 | + ); |
| 126 | + } |
| 127 | + if (tsconfig.exclude == null) { |
| 128 | + tsconfig.exclude = ['**/__tests__/**', '**/?*(spec|test).*']; |
| 129 | + messages.push(`${chalk.cyan('exclude')} should exclude test files`); |
| 130 | + } |
| 131 | + |
| 132 | + if (messages.length > 0) { |
| 133 | + console.warn( |
| 134 | + chalk.bold( |
| 135 | + 'The following changes are being made to your', |
| 136 | + chalk.cyan('tsconfig.json'), |
| 137 | + 'file:' |
| 138 | + ) |
| 139 | + ); |
| 140 | + messages.forEach(message => { |
| 141 | + console.warn(' - ' + message); |
| 142 | + }); |
| 143 | + console.warn(); |
| 144 | + writeJson(paths.appTsConfig, tsconfig); |
| 145 | + } |
| 146 | + |
| 147 | + // Copy type declarations associated with this version of `react-scripts` |
| 148 | + const declaredTypes = path.resolve( |
| 149 | + __dirname, |
| 150 | + '..', |
| 151 | + '..', |
| 152 | + 'config', |
| 153 | + 'react-app.d.ts' |
| 154 | + ); |
| 155 | + const declaredTypesContent = fs |
| 156 | + .readFileSync(declaredTypes, 'utf8') |
| 157 | + .replace(/\/\/ @remove-file-on-eject\r?\n/, ''); |
| 158 | + fs.writeFileSync( |
| 159 | + path.resolve(paths.appSrc, 'react-app.d.ts'), |
| 160 | + declaredTypesContent |
| 161 | + ); |
56 | 162 | }
|
57 | 163 |
|
58 | 164 | module.exports = verifyTypeScriptSetup;
|
0 commit comments