Skip to content

Commit 1e1fd67

Browse files
committed
Automatically create tsconfig.json file for user
1 parent 236d1f8 commit 1e1fd67

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

packages/react-dev-utils/globby.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
8+
'use strict';
9+
10+
var globby = require('globby');
11+
12+
module.exports = globby;

packages/react-dev-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"filesize": "3.6.1",
5454
"find-up": "3.0.0",
5555
"global-modules": "1.0.0",
56+
"globby": "8.0.1",
5657
"gzip-size": "5.0.0",
5758
"immer": "1.7.2",
5859
"inquirer": "6.2.0",

packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js

+29-10
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,36 @@ const path = require('path');
1515
const paths = require('../../config/paths');
1616
const os = require('os');
1717
const immer = require('react-dev-utils/immer').produce;
18+
const globby = require('react-dev-utils/globby').sync;
1819

1920
function writeJson(fileName, object) {
2021
fs.writeFileSync(fileName, JSON.stringify(object, null, 2) + os.EOL);
2122
}
2223

24+
function verifyNoTypeScript() {
25+
const typescriptFiles = globby('**/*.(ts|tsx)', { cwd: paths.appSrc });
26+
if (typescriptFiles.length > 0) {
27+
console.warn(
28+
chalk.yellow(
29+
`We detected TypeScript in your project (${chalk.bold(
30+
`src${path.sep}${typescriptFiles[0]}`
31+
)}) and created a ${chalk.bold('tsconfig.json')} file for you.`
32+
)
33+
);
34+
console.warn();
35+
return false;
36+
}
37+
return true;
38+
}
39+
2340
function verifyTypeScriptSetup() {
2441
let firstTimeSetup = false;
2542

2643
if (!fs.existsSync(paths.appTsConfig)) {
2744
if (!paths.appIndexJs.match(/\.tsx?$/)) {
28-
return;
45+
if (verifyNoTypeScript()) {
46+
return;
47+
}
2948
}
3049
writeJson(paths.appTsConfig, {});
3150
firstTimeSetup = true;
@@ -41,14 +60,12 @@ function verifyTypeScriptSetup() {
4160
}));
4261
} catch (_) {
4362
console.error(
44-
chalk.red(
45-
'We detected a',
46-
chalk.bold('tsconfig.json'),
47-
"in your package root but couldn't find an installation of",
48-
chalk.bold('typescript') + '.'
63+
chalk.bold.red(
64+
`It looks like you're trying to use TypeScript but do not have ${chalk.bold(
65+
'typescript'
66+
)} installed.`
4967
)
5068
);
51-
console.error();
5269
console.error(
5370
chalk.bold(
5471
'Please install',
@@ -60,9 +77,11 @@ function verifyTypeScriptSetup() {
6077
)
6178
);
6279
console.error(
63-
'If you are not trying to use TypeScript, please remove the ' +
64-
chalk.cyan('tsconfig.json') +
65-
' file from your package root.'
80+
chalk.bold(
81+
'If you are not trying to use TypeScript, please remove the ' +
82+
chalk.cyan('tsconfig.json') +
83+
' file from your package root (and any TypeScript files).'
84+
)
6685
);
6786
console.error();
6887
process.exit(1);

0 commit comments

Comments
 (0)