Skip to content

Commit 8ceb60a

Browse files
committed
3.4.0 Init.js
1 parent 69e2338 commit 8ceb60a

File tree

11 files changed

+121
-34
lines changed

11 files changed

+121
-34
lines changed

packages/cra-template-typescript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cra-template-typescript",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"keywords": [
55
"react",
66
"create-react-app",

packages/cra-template-typescript/template/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ See the section about [deployment](https://facebook.github.io/create-react-app/d
3333

3434
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
3535

36-
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
36+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
3737

3838
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
3939

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# https://www.robotstxt.org/robotstxt.html
22
User-agent: *
3+
Disallow:

packages/cra-template-typescript/template/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import logo from './logo.svg';
33
import './App.css';
44

5-
const App = () => {
5+
function App() {
66
return (
77
<div className="App">
88
<header className="App-header">

packages/cra-template-typescript/template/src/serviceWorker.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,12 @@ function checkValidServiceWorker(swUrl: string, config?: Config) {
138138

139139
export function unregister() {
140140
if ('serviceWorker' in navigator) {
141-
navigator.serviceWorker.ready.then(registration => {
142-
registration.unregister();
143-
});
141+
navigator.serviceWorker.ready
142+
.then(registration => {
143+
registration.unregister();
144+
})
145+
.catch(error => {
146+
console.error(error.message);
147+
});
144148
}
145149
}

packages/cra-template/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cra-template",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"keywords": [
55
"react",
66
"create-react-app",

packages/cra-template/template/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ See the section about [deployment](https://facebook.github.io/create-react-app/d
3333

3434
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
3535

36-
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
36+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
3737

3838
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
3939

packages/cra-template/template/src/serviceWorker.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,12 @@ function checkValidServiceWorker(swUrl, config) {
130130

131131
export function unregister() {
132132
if ('serviceWorker' in navigator) {
133-
navigator.serviceWorker.ready.then(registration => {
134-
registration.unregister();
135-
});
133+
navigator.serviceWorker.ready
134+
.then(registration => {
135+
registration.unregister();
136+
})
137+
.catch(error => {
138+
console.error(error.message);
139+
});
136140
}
137141
}

packages/react-scripts/scripts/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ checkBrowsers(paths.appPath, isInteractive)
110110
console.log();
111111

112112
const appPackage = require(paths.appPackageJson);
113-
const publicUrl = paths.publicUrl;
113+
const publicUrl = paths.publicUrlOrPath;
114114
const publicPath = config.output.publicPath;
115115
const buildFolder = path.relative(process.cwd(), paths.appBuild);
116116
printHostingInstructions(

packages/react-scripts/scripts/init.js

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,41 @@ function isInMercurialRepository() {
4444
}
4545
}
4646

47-
function tryGitInit(appPath) {
48-
let didInit = false;
47+
function tryGitInit() {
4948
try {
5049
execSync('git --version', { stdio: 'ignore' });
5150
if (isInGitRepository() || isInMercurialRepository()) {
5251
return false;
5352
}
5453

5554
execSync('git init', { stdio: 'ignore' });
56-
didInit = true;
55+
return true;
56+
} catch (e) {
57+
console.warn('Git repo not initialized', e);
58+
return false;
59+
}
60+
}
5761

62+
function tryGitCommit(appPath) {
63+
try {
5864
execSync('git add -A', { stdio: 'ignore' });
59-
execSync('git commit -m "Initial commit from Create React App"', {
65+
execSync('git commit -m "Initialize project using Create React App"', {
6066
stdio: 'ignore',
6167
});
6268
return true;
6369
} catch (e) {
64-
if (didInit) {
65-
// If we successfully initialized but couldn't commit,
66-
// maybe the commit author config is not set.
67-
// In the future, we might supply our own committer
68-
// like Ember CLI does, but for now, let's just
69-
// remove the Git files to avoid a half-done state.
70-
try {
71-
// unlinkSync() doesn't work on directories.
72-
fs.removeSync(path.join(appPath, '.git'));
73-
} catch (removeErr) {
74-
// Ignore.
75-
}
70+
// We couldn't commit in already initialized git repo,
71+
// maybe the commit author config is not set.
72+
// In the future, we might supply our own committer
73+
// like Ember CLI does, but for now, let's just
74+
// remove the Git files to avoid a half-done state.
75+
console.warn('Git commit not created', e);
76+
console.warn('Removing .git directory...');
77+
try {
78+
// unlinkSync() doesn't work on directories.
79+
fs.removeSync(path.join(appPath, '.git'));
80+
} catch (removeErr) {
81+
// Ignore.
7682
}
7783
return false;
7884
}
@@ -121,11 +127,55 @@ module.exports = function(
121127
templateJson = require(templateJsonPath);
122128
}
123129

130+
const templatePackage = templateJson.package || {};
131+
132+
// Keys to ignore in templatePackage
133+
const templatePackageBlacklist = [
134+
'name',
135+
'version',
136+
'description',
137+
'keywords',
138+
'bugs',
139+
'license',
140+
'author',
141+
'contributors',
142+
'files',
143+
'main',
144+
'browser',
145+
'bin',
146+
'man',
147+
'directories',
148+
'repository',
149+
'devDependencies',
150+
'peerDependencies',
151+
'bundledDependencies',
152+
'optionalDependencies',
153+
'engineStrict',
154+
'os',
155+
'cpu',
156+
'preferGlobal',
157+
'private',
158+
'publishConfig',
159+
];
160+
161+
// Keys from templatePackage that will be merged with appPackage
162+
const templatePackageToMerge = ['dependencies', 'scripts'];
163+
164+
// Keys from templatePackage that will be added to appPackage,
165+
// replacing any existing entries.
166+
const templatePackageToReplace = Object.keys(templatePackage).filter(key => {
167+
return (
168+
!templatePackageBlacklist.includes(key) &&
169+
!templatePackageToMerge.includes(key)
170+
);
171+
});
172+
124173
// Copy over some of the devDependencies
125174
appPackage.dependencies = appPackage.dependencies || {};
126175

127176
// Setup the script rules
128-
const templateScripts = templateJson.scripts || {};
177+
// TODO: deprecate 'scripts' key directly on templateJson
178+
const templateScripts = templatePackage.scripts || templateJson.scripts || {};
129179
appPackage.scripts = Object.assign(
130180
{
131181
start: 'react-scripts start',
@@ -155,6 +205,11 @@ module.exports = function(
155205
// Setup the browsers list
156206
appPackage.browserslist = defaultBrowsers;
157207

208+
// Add templatePackage keys/values to appPackage, replacing existing entries
209+
templatePackageToReplace.forEach(key => {
210+
appPackage[key] = templatePackage[key];
211+
});
212+
158213
_readmeExists = fs.existsSync(path.join(appPath, 'README.md'));
159214

160215
appPackage = initWpTheme.fnUpdateAppPackage(
@@ -173,6 +228,7 @@ module.exports = function(
173228
JSON.stringify(appPackage, null, 2) + os.EOL
174229
);
175230

231+
// const readmeExists = fs.existsSync(path.join(appPath, 'README.md'));
176232
if (_readmeExists) {
177233
fs.renameSync(
178234
path.join(appPath, 'README.md'),
@@ -221,6 +277,15 @@ module.exports = function(
221277
);
222278
}
223279

280+
// Initialize git repo
281+
let initializedGit = false;
282+
283+
if (tryGitInit()) {
284+
initializedGit = true;
285+
console.log();
286+
console.log('Initialized a git repository.');
287+
}
288+
224289
let command;
225290
let remove;
226291
let args;
@@ -236,7 +301,9 @@ module.exports = function(
236301
}
237302

238303
// Install additional template dependencies, if present
239-
const templateDependencies = templateJson.dependencies;
304+
// TODO: deprecate 'dependencies' key directly on templateJson
305+
const templateDependencies =
306+
templatePackage.dependencies || templateJson.dependencies;
240307
if (templateDependencies) {
241308
args = args.concat(
242309
Object.keys(templateDependencies).map(key => {
@@ -280,9 +347,10 @@ module.exports = function(
280347
return;
281348
}
282349

283-
if (tryGitInit(appPath)) {
350+
// Create git commit if git repo was initialized
351+
if (initializedGit && tryGitCommit(appPath)) {
284352
console.log();
285-
console.log('Initialized a git repository.');
353+
console.log('Created git commit.');
286354
}
287355

288356
// wptheme intercept -- stop the standard create-react-app init here... wptheme will take over and display the final message.

0 commit comments

Comments
 (0)