mirror of
https://github.com/zhigang1992/create-react-app.git
synced 2026-01-12 22:46:30 +08:00
Add scripts support to templates (#7989)
This commit is contained in:
52
packages/react-scripts/scripts/init.js
vendored
52
packages/react-scripts/scripts/init.js
vendored
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user