-
Notifications
You must be signed in to change notification settings - Fork 108
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
Add support for filesystem polling #1176
Changes from all commits
5826027
01446b4
7d36084
08ccb49
d890186
48d07bc
6879ab1
00cc231
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 |
---|---|---|
|
@@ -50,7 +50,7 @@ export type Agent = 'npm' | 'nodeRun' | 'pnpm' | 'yarnClassic' | 'yarnBerry'; | |
|
||
export interface Options { | ||
script: ScriptReference; | ||
watch: boolean; | ||
watch: false | {strategy: 'event'} | {strategy: 'poll'; interval: number}; | ||
extraArgs: string[] | undefined; | ||
numWorkers: number; | ||
cache: 'local' | 'github' | 'none'; | ||
|
@@ -277,7 +277,10 @@ function getArgvOptions( | |
// npm 8.11.0 | ||
// - Like npm 6, except there is no "npm_config_argv" environment variable. | ||
return { | ||
watch: process.env['npm_config_watch'] !== undefined, | ||
watch: | ||
process.env['npm_config_watch'] !== undefined | ||
? readWatchConfigFromEnv() | ||
: false, | ||
extraArgs: process.argv.slice(2), | ||
}; | ||
} | ||
|
@@ -421,7 +424,7 @@ function findRemainingArgsFromNpmConfigArgv( | |
function parseRemainingArgs( | ||
args: string[], | ||
): Pick<Options, 'watch' | 'extraArgs'> { | ||
let watch = false; | ||
let watch: Options['watch'] = false; | ||
let extraArgs: string[] = []; | ||
const unrecognized = []; | ||
for (let i = 0; i < args.length; i++) { | ||
|
@@ -430,7 +433,7 @@ function parseRemainingArgs( | |
extraArgs = args.slice(i + 1); | ||
break; | ||
} else if (arg === '--watch') { | ||
watch = true; | ||
watch = readWatchConfigFromEnv(); | ||
} else { | ||
unrecognized.push(arg); | ||
} | ||
|
@@ -448,3 +451,47 @@ function parseRemainingArgs( | |
extraArgs, | ||
}; | ||
} | ||
|
||
const DEFAULT_WATCH_STRATEGY = {strategy: 'event'} as const; | ||
const DEFAULT_WATCH_INTERVAL = 500; | ||
|
||
/** | ||
* Interpret the WIREIT_WATCH_* environment variables. | ||
*/ | ||
function readWatchConfigFromEnv(): Options['watch'] { | ||
switch (process.env['WIREIT_WATCH_STRATEGY']) { | ||
case 'event': | ||
case '': | ||
case undefined: { | ||
return DEFAULT_WATCH_STRATEGY; | ||
} | ||
case 'poll': { | ||
let interval = DEFAULT_WATCH_INTERVAL; | ||
const intervalStr = process.env['WIREIT_WATCH_POLL_MS']; | ||
if (intervalStr) { | ||
const parsed = Number(intervalStr); | ||
if (Number.isNaN(parsed) || parsed <= 0) { | ||
console.error( | ||
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. If this is continuing to operate with the fallback value, should this be 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. Both go to stderr, but in some configurations warnings might get filtered out? I guess because it's definitely something wrong, I think it should be an error to maximize the chance of somebody seeing it. |
||
`⚠️ Expected WIREIT_WATCH_POLL_MS to be a positive integer, ` + | ||
`got ${JSON.stringify(intervalStr)}. Using default interval of ` + | ||
`${DEFAULT_WATCH_INTERVAL}ms.`, | ||
); | ||
} else { | ||
interval = parsed; | ||
} | ||
} | ||
return { | ||
strategy: 'poll', | ||
interval, | ||
}; | ||
} | ||
default: { | ||
console.error( | ||
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. Same here. |
||
`⚠️ Unrecognized WIREIT_WATCH_STRATEGY: ` + | ||
`${JSON.stringify(process.env['WIREIT_WATCH_STRATEGY'])}. ` + | ||
`Using default strategy of ${DEFAULT_WATCH_STRATEGY.strategy}.`, | ||
); | ||
return DEFAULT_WATCH_STRATEGY; | ||
} | ||
} | ||
} |
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.
Style nit: I definitely prefer avoid fall-through myself. Would this be easier as an if/else statement?
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.
This whole file (and other files) uses this style of switching on
state
. We're doing exhaustive checking for every state for every transition. I actually kinda like using fallthrough here, it's just the most concise way to write out all the permutations.