From 6447d56f32fd55e05c765a3d051104e553d3ce4f Mon Sep 17 00:00:00 2001 From: Brody McKee Date: Sat, 16 Nov 2019 21:50:08 +0200 Subject: [PATCH] Add scripts support to templates --- packages/react-scripts/scripts/init.js | 52 ++++++++++++++++++-------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/packages/react-scripts/scripts/init.js b/packages/react-scripts/scripts/init.js index fb3490617c4..aa035d15345 100644 --- a/packages/react-scripts/scripts/init.js +++ b/packages/react-scripts/scripts/init.js @@ -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