Skip to content

Commit

Permalink
Add scripts support to templates (facebook#7989)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmckeb authored Nov 19, 2019
1 parent 2f71d12 commit 51eee3f
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 51eee3f

Please sign in to comment.