Add scripts support to templates (#7989)

This commit is contained in:
Brody McKee
2019-11-19 12:14:31 +02:00
committed by GitHub
parent df5088da4d
commit 23d5776724

View File

@@ -105,16 +105,44 @@ module.exports = function(
'..'
);
let templateJsonPath;
if (templateName) {
templateJsonPath = path.join(templatePath, 'template.json');
} else {
// TODO: Remove support for this in v4.
templateJsonPath = path.join(appPath, '.template.dependencies.json');
}
let templateJson = {};
if (fs.existsSync(templateJsonPath)) {
templateJson = require(templateJsonPath);
}
// Copy over some of the devDependencies
appPackage.dependencies = appPackage.dependencies || {};
// Setup the script rules
appPackage.scripts = {
start: 'react-scripts start',
build: 'react-scripts build',
test: 'react-scripts test',
eject: 'react-scripts eject',
};
const templateScripts = templateJson.scripts || {};
appPackage.scripts = Object.assign(
{
start: 'react-scripts start',
build: 'react-scripts build',
test: 'react-scripts test',
eject: 'react-scripts eject',
},
templateScripts
);
// Update scripts for Yarn users
if (useYarn) {
appPackage.scripts = Object.entries(appPackage.scripts).reduce(
(acc, [key, value]) => ({
...acc,
[key]: value.replace(/(npm run |npm )/, 'yarn '),
}),
{}
);
}
// Setup the eslint config
appPackage.eslintConfig = {
@@ -196,21 +224,13 @@ module.exports = function(
}
// Install additional template dependencies, if present
let templateJsonPath;
if (templateName) {
templateJsonPath = path.join(templatePath, 'template.json');
} else {
templateJsonPath = path.join(appPath, '.template.dependencies.json');
}
if (fs.existsSync(templateJsonPath)) {
const templateDependencies = require(templateJsonPath).dependencies;
const templateDependencies = templateJson.dependencies;
if (templateDependencies) {
args = args.concat(
Object.keys(templateDependencies).map(key => {
return `${key}@${templateDependencies[key]}`;
})
);
fs.unlinkSync(templateJsonPath);
}
// Install react and react-dom for backward compatibility with old CRA cli