Skip to content

Commit 51eee3f

Browse files
authored
Add scripts support to templates (facebook#7989)
1 parent 2f71d12 commit 51eee3f

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

scripts/init.js

+36-16
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,44 @@ module.exports = function(
105105
'..'
106106
);
107107

108+
let templateJsonPath;
109+
if (templateName) {
110+
templateJsonPath = path.join(templatePath, 'template.json');
111+
} else {
112+
// TODO: Remove support for this in v4.
113+
templateJsonPath = path.join(appPath, '.template.dependencies.json');
114+
}
115+
116+
let templateJson = {};
117+
if (fs.existsSync(templateJsonPath)) {
118+
templateJson = require(templateJsonPath);
119+
}
120+
108121
// Copy over some of the devDependencies
109122
appPackage.dependencies = appPackage.dependencies || {};
110123

111124
// Setup the script rules
112-
appPackage.scripts = {
113-
start: 'react-scripts start',
114-
build: 'react-scripts build',
115-
test: 'react-scripts test',
116-
eject: 'react-scripts eject',
117-
};
125+
const templateScripts = templateJson.scripts || {};
126+
appPackage.scripts = Object.assign(
127+
{
128+
start: 'react-scripts start',
129+
build: 'react-scripts build',
130+
test: 'react-scripts test',
131+
eject: 'react-scripts eject',
132+
},
133+
templateScripts
134+
);
135+
136+
// Update scripts for Yarn users
137+
if (useYarn) {
138+
appPackage.scripts = Object.entries(appPackage.scripts).reduce(
139+
(acc, [key, value]) => ({
140+
...acc,
141+
[key]: value.replace(/(npm run |npm )/, 'yarn '),
142+
}),
143+
{}
144+
);
145+
}
118146

119147
// Setup the eslint config
120148
appPackage.eslintConfig = {
@@ -196,21 +224,13 @@ module.exports = function(
196224
}
197225

198226
// Install additional template dependencies, if present
199-
let templateJsonPath;
200-
if (templateName) {
201-
templateJsonPath = path.join(templatePath, 'template.json');
202-
} else {
203-
templateJsonPath = path.join(appPath, '.template.dependencies.json');
204-
}
205-
206-
if (fs.existsSync(templateJsonPath)) {
207-
const templateDependencies = require(templateJsonPath).dependencies;
227+
const templateDependencies = templateJson.dependencies;
228+
if (templateDependencies) {
208229
args = args.concat(
209230
Object.keys(templateDependencies).map(key => {
210231
return `${key}@${templateDependencies[key]}`;
211232
})
212233
);
213-
fs.unlinkSync(templateJsonPath);
214234
}
215235

216236
// Install react and react-dom for backward compatibility with old CRA cli

0 commit comments

Comments
 (0)