Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added error handling for execSync * now get hooks from config, abstracted into own file, with typings added * CLI only * removing an unintended import in types.d.ts * add new flags to cli/help.md * added some docs * update docs * added a cli test for watch-event-hooks * scaffold test, need to figure out how to actually write it now * need guidance * tests passing now with change for child.stdout -> process.stderr * fixed test abortOnStderr Co-authored-by: Harris Miller <hmiller@alteryx.com>
- Loading branch information
1 parent
f44a3a3
commit 0c75914
Showing
13 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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 { | ||
// !! important - use stderr for all writes from execSync | ||
const stdio = [process.stdin, process.stderr, process.stderr]; | ||
execSync(cmd, { stdio: command.silent ? 'ignore' : stdio }); | ||
} catch (e) { | ||
stderr((e as Error).message); | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { assertIncludes } = require('../../../../utils.js'); | ||
|
||
module.exports = { | ||
description: 'onError event hook shell commands write to stderr', | ||
command: 'node wrapper.js -cw --watch.onError "echo error"', | ||
abortOnStderr(data) { | ||
if (data.includes('waiting for changes')) { | ||
return true; | ||
} | ||
}, | ||
stderr(stderr) { | ||
assertIncludes( | ||
stderr, | ||
`watch.onError $ echo error | ||
error` | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// missing `=` to trigger onError | ||
var main 42; | ||
|
||
export { main as default }; |
7 changes: 7 additions & 0 deletions
7
test/cli/samples/watch/watch-event-hooks-error/rollup.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default { | ||
input: 'main.js', | ||
output: { | ||
dir: '_actual', | ||
format: 'es' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
process.stdout.isTTY = true; | ||
process.stderr.isTTY = true; | ||
require('../../../../../dist/bin/rollup'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const { assertIncludes } = require('../../../../utils.js'); | ||
|
||
module.exports = { | ||
description: 'event hook shell commands write to stderr', | ||
command: | ||
'node wrapper.js -cw --watch.onStart "echo start" --watch.onBundleStart "echo bundleStart" --watch.onBundleEnd "echo bundleEnd" --watch.onEnd "echo end"', | ||
abortOnStderr(data) { | ||
process.stderr.write(data); | ||
if (data.includes('waiting for changes')) { | ||
return true; | ||
} | ||
}, | ||
stderr(stderr) { | ||
// assert each hook individually | ||
assertIncludes( | ||
stderr, | ||
`watch.onStart $ echo start | ||
start` | ||
); | ||
assertIncludes( | ||
stderr, | ||
`watch.onBundleStart $ echo bundleStart | ||
bundleStart` | ||
); | ||
assertIncludes( | ||
stderr, | ||
`watch.onBundleEnd $ echo bundleEnd | ||
bundleEnd` | ||
); | ||
assertIncludes( | ||
stderr, | ||
`watch.onEnd $ echo end | ||
end` | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var main = 42; | ||
|
||
export { main as default }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default { | ||
input: 'main.js', | ||
output: { | ||
dir: '_actual', | ||
format: 'es' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
process.stdout.isTTY = true; | ||
process.stderr.isTTY = true; | ||
require('../../../../../dist/bin/rollup'); |