Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit b95f1af

Browse files
committed
fix: webpack should regenerate prescan when watching
Prior to this, developers would have to rerun `npx kui-prescan` manually if they either a) added/deleted a command registration; or b) added/deleted a guidebook in their client/notebooks directory. This PR adds a build hook to the webpack config that regenerates these models. Doing to required some small changes to the prescan logic, so that they accept a target/output file as input (rather than hard-coding the target files for these two models). This allows us to avoid an finite loop of change detected -> prescan initated which only touches but does not change the two files -> change detected -> ...
1 parent 8fc2190 commit b95f1af

File tree

6 files changed

+81
-56
lines changed

6 files changed

+81
-56
lines changed

package-lock.json

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/builder/bin/prescan.sh

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,32 @@ SCRIPTDIR=$(cd $(dirname "$0") && pwd)
2525
export CLIENT_HOME=${CLIENT_HOME-`pwd`}
2626
export PLUGIN_ROOT="$(cd "$TOPDIR" && pwd)/plugins"
2727

28+
# webpack.config.js may set this in its watcher to ask us to generate
29+
# the prescan in a specific location
30+
export KUI_PRESCAN
31+
2832
# make typescript happy, until we have the real prescan model ready
2933
# (produced by builder/lib/configure.js, but which cannot be run
3034
# until after we've compiled the source)
31-
mkdir -p ./node_modules/@kui-shell
32-
touch ./node_modules/@kui-shell/prescan.json
35+
if [ -z "$KUI_PRESCAN" ]; then
36+
mkdir -p ./node_modules/@kui-shell
37+
if [ ! -e ./node_modules/@kui-shell/prescan.json ]; then
38+
touch ./node_modules/@kui-shell/prescan.json
39+
fi
40+
fi
3341

3442
# pre-compile plugin registry
3543
if [ -f ./node_modules/@kui-shell/builder/dist/bin/compile.js ]; then
36-
echo "compiling plugin registry $CLIENT_HOME"
44+
echo "compiling plugin registry $CLIENT_HOME to ${KUI_PRESCAN-default location}"
3745
node ./node_modules/@kui-shell/builder/dist/bin/compile.js
3846
fi
3947

4048
# generate the index.json in @kui-shell/client/notebooks/index.json
4149
if [ -d node_modules/@kui-shell/client/notebooks ]; then
42-
echo "Generating client-guidebooks.json"
43-
if [ ! -d node_modules/@kui-shell/build/ ]; then
44-
mkdir node_modules/@kui-shell/build
50+
TGT=${KUI_PRESCAN_GUIDEBOOKS-node_modules/@kui-shell/build/client-guidebooks.json}
51+
echo "Generating client-guidebooks.json to $TGT"
52+
if [ ! -d "$(dirname $TGT)" ]; then
53+
mkdir "$(dirname $TGT)"
4554
fi
46-
(echo -n "["; (cd node_modules/\@kui-shell/client/notebooks && find . \( -name '*.md' -o -name '*.py' -o -name '*.txt' -o -name '*.json' \) -print) | sed 's/\.\///' | xargs -I{} -n1 echo -n '"{}",'; echo -n "]") | sed 's/\,]/]/' > node_modules/@kui-shell/build/client-guidebooks.json
55+
(echo -n "["; (cd node_modules/\@kui-shell/client/notebooks && find . \( -name '*.md' -o -name '*.py' -o -name '*.txt' -o -name '*.json' \) -print) | sed 's/\.\///' | xargs -I{} -n1 echo -n '"{}",'; echo -n "]") | sed 's/\,]/]/' > "$TGT"
4756
fi

packages/builder/dist/bin/compile.js

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,10 @@ const { default: mimicDom } = require('@kui-shell/core/dist/util/mimic-dom')
1919

2020
mimicDom()
2121

22-
/** dump codecov? */
23-
function codecov() {
24-
console.log('Writing code coverage data for prescan')
25-
const { join, relative } = require('path')
26-
27-
const codeCoverageNyc = () =>
28-
process.env.TRAVIS_BUILD_DIR
29-
? join(process.env.TRAVIS_BUILD_DIR, 'node_modules/nyc')
30-
: join(process.env.TEST_SUITE_ROOT, '../nyc')
31-
const codeCoverageRoot = () => relative(process.env.TEST_ROOT, join(process.env.TEST_SUITE_ROOT, 'core'))
32-
const codeCoverageTempDirectory = () =>
33-
process.env.TRAVIS_BUILD_DIR
34-
? join(process.env.TRAVIS_BUILD_DIR, 'packages/test/.nyc_output')
35-
: join(process.env.TEST_ROOT, '.nyc_output')
36-
37-
process.chdir(require('os').tmpdir())
38-
39-
// Create the nyc instance
40-
const tempDirectory = codeCoverageTempDirectory()
41-
const NYC = require(codeCoverageNyc())
42-
const nyc = new NYC({ tempDirectory, cwd: codeCoverageRoot() })
43-
44-
// in case we are the first to the line
45-
nyc.createTempDirectory()
46-
47-
// Notes: the nyc impl of this removes all of our coverage data due to its
48-
// this.exclude.shouldInstrument filter (on our about line 343 of its index.js)
49-
// nyc.writeCoverageFile()
50-
51-
// see https://github.com/IBM/kui/issues/3217 for some discussion
52-
Object.keys(__coverage__).forEach(function (absFile) {
53-
const map = JSON.parse(require('fs').readFileSync(`${absFile}.map`))
54-
nyc.sourceMaps._sourceMapCache.registerMap(absFile, map)
55-
}, nyc)
56-
57-
// so... instead we take care of writeCoverageFile ourselves
58-
const coverage = nyc.sourceMaps.remapCoverage(__coverage__)
59-
const coverageFilename = require('path').resolve(tempDirectory, nyc.processInfo.uuid + '.json')
60-
require('fs').writeFileSync(coverageFilename, JSON.stringify(coverage), 'utf-8')
61-
}
62-
63-
function codecovIfDesired() {
64-
if (process.env.NYC !== undefined && process.env.NYC_INSTRUMENTATION_DONE !== undefined) {
65-
return codecov()
66-
}
67-
}
68-
6922
if (process.argv[2] === 'cleanup') {
7023
// nothing to do
7124
} else {
7225
compile()
73-
.then(codecovIfDesired)
7426
.then(() => process.exit(0))
7527
.catch(err => {
7628
console.error(err)

packages/core/src/plugins/assembler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ export const compile = async (
185185

186186
const modelWithUsage = amendWithUsageModels(model)
187187

188-
const destFile = externalOnly ? join(pluginRoot, '@kui-shell/prescan.json') : prescanned()
188+
const destFile =
189+
process.env.KUI_PRESCAN || (externalOnly ? join(pluginRoot, '@kui-shell/prescan.json') : prescanned())
189190
await Promise.all([writePrescanned(modelWithUsage, destFile)])
190191

191192
if (externalOnly) {

packages/webpack/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"webpack": "^5.75.0",
6868
"webpack-cli": "^5.0.1",
6969
"webpack-dev-server": "^4.11.1",
70+
"webpack-shell-plugin-next": "^2.3.1",
7071
"zip-webpack-plugin": "^4.0.1"
7172
},
7273
"kui": {

packages/webpack/webpack.config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const CopyPlugin = require('copy-webpack-plugin')
3030
const HtmlWebpackPlugin = require('html-webpack-plugin')
3131
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
3232
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
33+
const WebpackShellPlugin = require('webpack-shell-plugin-next')
3334
const { IgnorePlugin, ProvidePlugin } = require('webpack')
3435

3536
const sassLoaderChain = [
@@ -149,6 +150,23 @@ console.log('clientHome', process.env.CLIENT_HOME)
149150
*/
150151
const plugins = []
151152

153+
if (process.env.WATCH) {
154+
plugins.push(
155+
new WebpackShellPlugin({
156+
onDoneWatch: {
157+
scripts: [
158+
// here, we are trying to be careful to avoid an infinite
159+
// loop: we re-generate prescan.json, which triggers a webpack
160+
// build, which triggers this script again...
161+
'export KUI_PRESCAN=$(mktemp); export KUI_PRESCAN_GUIDEBOOKS=$(mktemp); npx kui-prescan; if [ -n "$(diff $KUI_PRESCAN node_modules/@kui-shell/prescan.json)" ]; then mv $KUI_PRESCAN node_modules/@kui-shell/prescan.json; fi; if [ -n "$(diff $KUI_PRESCAN_GUIDEBOOKS node_modules/@kui-shell/build/client-guidebooks.json)" ]; then mv $KUI_PRESCAN_GUIDEBOOKS node_modules/@kui-shell/build/client-guidebooks.json; fi'
162+
],
163+
blocking: false,
164+
parallel: true
165+
}
166+
})
167+
)
168+
}
169+
152170
if (process.env.WEBPACK_ANALYZER) {
153171
try {
154172
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
@@ -452,6 +470,11 @@ module.exports = {
452470
}
453471
}, */
454472
watchOptions: {
473+
// here we ignore changes to any node_modules, except we want to
474+
// trigger a webpack rebuild when either of these
475+
// changes: @kui-shell/prescan.json
476+
// or @kui-shell/build/client-guidebooks.json
477+
// these files are generated by `npx kui-prescan`
455478
ignored: /node_modules\/(?!@kui-shell\/(prescan\.json|build\/client-guidebooks\.json))/
456479
},
457480
devServer: {
@@ -465,6 +488,7 @@ module.exports = {
465488
}
466489
},
467490
devMiddleware: {
491+
// @kui-shell/core/src/main/spawn-electron.ts depends on files on disk
468492
writeToDisk: !inBrowser
469493
},
470494
host: '0.0.0.0',

0 commit comments

Comments
 (0)