From 6a94c2cd8f92ac9aec52544c6bb442690133910f Mon Sep 17 00:00:00 2001 From: Jeremy Gooch Date: Sun, 1 Nov 2020 07:56:31 -0600 Subject: [PATCH] feat: add after build hooks (#218) * Adding cli option after-build-hook. * Specifying initial vs rebuild hooks. --- scripts/index.js | 32 ++++++++++++++++++++++++++++---- utils/cliHandler.js | 10 ++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/scripts/index.js b/scripts/index.js index 8f753d5..2231fc7 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -8,6 +8,7 @@ const fs = require('fs-extra'); const path = require('path'); const ora = require('ora'); const assert = require('assert'); +const exec = require('child_process').exec; const { flags: { @@ -18,6 +19,8 @@ const { disableChunks, outputFilename, chunkFilename, + afterInitialBuildHook, + afterRebuildHook, }, } = require('../utils/cliHandler'); const { getReactScriptsVersion, isEjected } = require('../utils'); @@ -91,12 +94,11 @@ if (disableChunks) { // update media path destination if (major >= 4) { - const oneOfIndex = 1 + const oneOfIndex = 1; config.module.rules[oneOfIndex].oneOf[0].options.name = `media/[name].[hash:8].[ext]`; config.module.rules[oneOfIndex].oneOf[1].options.name = `media/[name].[hash:8].[ext]`; config.module.rules[oneOfIndex].oneOf[8].options.name = `media/[name].[hash:8].[ext]`; -} -else if (major >= 2) { +} else if (major >= 2) { // 2.0.0 => 2 // 2.0.1 => 3 // 2.0.2 => 3 @@ -150,6 +152,9 @@ fs.emptyDir(paths.appBuild) } spinner.succeed(); + + runHook('after rebuild hook', spinner, afterRebuildHook); + inProgress = false; if (verbose) { @@ -167,7 +172,8 @@ fs.emptyDir(paths.appBuild) }); }); }) - .then(() => copyPublicFolder()); + .then(() => copyPublicFolder()) + .then(() => runHook('after initial build hook', spinner, afterInitialBuildHook)); function copyPublicFolder() { return fs.copy(paths.appPublic, resolvedBuildPath, { @@ -176,6 +182,24 @@ function copyPublicFolder() { }); } +function runHook(label, spinner, hook) { + if (!hook || typeof hook !== 'string') { + return; + } + + spinner.start(label); + + exec(hook, (error, stdout, stderr) => { + if (error) { + spinner.fail(`${label}: exec error: ${error}`); + } else if (stderr) { + spinner.warn(`${label}: ${stderr}`); + } else { + spinner.succeed(`${label}: ${stdout}`); + } + }); +} + function handleBuildPath(userBuildPath) { if (path.isAbsolute(userBuildPath)) { return userBuildPath; diff --git a/utils/cliHandler.js b/utils/cliHandler.js index eacf192..18b2fff 100644 --- a/utils/cliHandler.js +++ b/utils/cliHandler.js @@ -18,6 +18,10 @@ module.exports = meow( -p, --public-path Public URL. + --after-rebuild-hook Run a command after each build/rebuild (e.g. 'node ./afterbuild.js') + + --after-initial-build-hook Run a command after each the initial build only (e.g. 'node ./afterbuild.js') + --react-scripts-version Version of the react-scripts package used in your project i.e 2.0.3. If not given it will be implied from your package.json and if it cannot be implied the major version 2 will be the default. -v, --verbose @@ -54,6 +58,12 @@ module.exports = meow( type: 'boolean', alias: 'v', }, + 'after-initial-build-hook': { + type: 'string', + }, + 'after-rebuild-hook': { + type: 'string', + } }, } );