Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
feat: add after build hooks (#218)
Browse files Browse the repository at this point in the history
* Adding cli option after-build-hook.

* Specifying initial vs rebuild hooks.
  • Loading branch information
jeremygooch authored and Nargonath committed Nov 1, 2020
1 parent d1e2880 commit 6a94c2c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
32 changes: 28 additions & 4 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -18,6 +19,8 @@ const {
disableChunks,
outputFilename,
chunkFilename,
afterInitialBuildHook,
afterRebuildHook,
},
} = require('../utils/cliHandler');
const { getReactScriptsVersion, isEjected } = require('../utils');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -150,6 +152,9 @@ fs.emptyDir(paths.appBuild)
}

spinner.succeed();

runHook('after rebuild hook', spinner, afterRebuildHook);

inProgress = false;

if (verbose) {
Expand All @@ -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, {
Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions utils/cliHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,6 +58,12 @@ module.exports = meow(
type: 'boolean',
alias: 'v',
},
'after-initial-build-hook': {
type: 'string',
},
'after-rebuild-hook': {
type: 'string',
}
},
}
);

0 comments on commit 6a94c2c

Please sign in to comment.