New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: CLI event hook flags #4457
Changes from 11 commits
708d883
020511c
132b478
f04b972
b4b0f74
aa32e3b
893868b
6c52d17
dc2ac66
e01257e
5a47a1c
4ed7a22
7c42390
56b52c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { execSync } from 'child_process'; | ||
import type { RollupWatchHooks } from '../../src/rollup/types'; | ||
import { bold, cyan } from '../../src/utils/colors'; | ||
import { stderr } from '../logging'; | ||
|
||
function extractWatchHooks( | ||
command: Record<string, any> | ||
): Partial<Record<RollupWatchHooks, string>> { | ||
if (!Array.isArray(command.watch)) return {}; | ||
|
||
return command.watch | ||
.filter(value => typeof value === 'object') | ||
.reduce((acc, keyValueOption) => ({ ...acc, ...keyValueOption }), {}); | ||
} | ||
|
||
export function createWatchHooks(command: Record<string, any>): (hook: RollupWatchHooks) => void { | ||
const watchHooks = extractWatchHooks(command); | ||
|
||
return function (hook: RollupWatchHooks): void { | ||
if (watchHooks[hook]) { | ||
const cmd = watchHooks[hook]!; | ||
|
||
if (!command.silent) { | ||
stderr(cyan(`watch.${hook} ${bold(`$ ${cmd}`)}`)); | ||
} | ||
|
||
try { | ||
execSync(cmd, { stdio: command.silent ? 'ignore' : 'inherit' }); | ||
} catch (e) { | ||
stderr((e as Error).message); | ||
} | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const { assertIncludes } = require('../../../../utils.js'); | ||
|
||
module.exports = { | ||
description: 'onStart event hoot shell command executes correctly', | ||
command: | ||
'rollup -cw --watch --watch.onStart "echo start" --watch.onBundleStart "echo bundleStart" --watch.onBundleEnd "echo bundleEnd" --watch.onEnd "echo onEnd" --watch.onError "echo onError"', | ||
stderr(stderr) { | ||
assertIncludes( | ||
stderr, | ||
`watch.onStart $ echo start | ||
start | ||
bundles main.js → _actual... | ||
watch.onBundleStart $ echo bundleStart | ||
bundleStart | ||
created _actual in 16ms | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A CLI test must not depend on things like runtime. The usual solution is to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another idea: Split the output by line, filtering empty ones and mapping the remaining ones to their first word. Then you would just need to compare
|
||
watch.onBundleEnd $ echo bundleEnd | ||
bundleEnd | ||
watch.onEnd $ echo onEnd | ||
onEnd | ||
` | ||
); | ||
// assert.strictEqual(stderr.slice(0, 12), `${CLEAR_SCREEN}${UNDERLINE}rollup`); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var main = 42; | ||
|
||
export { main as default }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default { | ||
input: 'main.js', | ||
output: { | ||
dir: '_actual', | ||
format: 'es' | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-cw
includes--watch
😉There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol oops.
I could use some guidance on how to do this test exactly. The
test/cli/samples/
suite of test all seem to do tests on either console output or bundle file output. the one I threw up there was just a POC to test all of the watch event hooks at once by just looking for the echos being writtenI can get that test working or I can take another direction. Would you like me to break up each hook into its own test? should I test that
--silent
doesn't print to stderr?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I think the test is fine in principle, and testing all hooks together seems adequate to me considering me that there is not too much logic involved. You should just make sure to make it a little more robust, e.g. see my other suggestion of preprocessing the output by splitting it into lines, doing some mapping and filtering and possibly doing an
deepStrictEqual
on the result. Looks to me like "echo" should also work on Windows, which is definitely a bonus.