-
Notifications
You must be signed in to change notification settings - Fork 2
Add custom watched and ignored files #8
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
base: main
Are you sure you want to change the base?
Changes from all commits
d3d369a
0aaef44
7131cd6
3749317
c2273f7
6c7c4f7
67bc681
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 |
---|---|---|
|
@@ -28,7 +28,7 @@ module Chokidar = { | |
type watchOptions = {ignored?: array<string>, ignoreInitial?: bool} | ||
|
||
@send | ||
external watch: (t, string, ~options: watchOptions=?) => Watcher.t = "watch" | ||
external watch: (t, array<string>, ~options: watchOptions=?) => Watcher.t = "watch" | ||
} | ||
|
||
module Glob = { | ||
|
@@ -46,6 +46,14 @@ module Glob = { | |
external glob: glob = "default" | ||
} | ||
|
||
module MicroMatch = { | ||
type mm = { | ||
makeRe: string => RegExp.t | ||
} | ||
@module("micromatch") | ||
external mm: mm = "default" | ||
} | ||
|
||
Comment on lines
+49
to
+56
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.
|
||
module Hash = { | ||
type createHash | ||
@module("crypto") external createHash: createHash = "createHash" | ||
|
@@ -166,6 +174,24 @@ type handleOtherCommandConfig<'config> = { | |
|
||
type setupConfig = {args: CliArgs.t} | ||
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. Initially, I had tinkered with adding a parameter here, which would have avoided the breaking change to type setupConfig = {
args: CliArgs.t,
addWatcher: watcher => unit
} However, I realized that with this API, an embed developer could then start doing funny things with |
||
|
||
type watcherOnChangeConfig = { | ||
file: string, | ||
runGeneration: (~files: array<string>=?) => promise<unit>, | ||
} | ||
|
||
type watcher = { | ||
filePattern: string, | ||
onChange: watcherOnChangeConfig => promise<unit> | ||
} | ||
|
||
@unboxed | ||
type setupResult<'config> = | ||
| SetupResult({ | ||
config: 'config, | ||
additionalFileWatchers?: array<watcher>, | ||
additionalIgnorePatterns?: array<string> | ||
}) | ||
Comment on lines
+187
to
+193
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. Initially used a plain-ol' record here, but that caused some inference issues due to the |
||
|
||
type onWatchConfig<'config> = { | ||
config: 'config, | ||
runGeneration: (~files: array<string>=?) => promise<unit>, | ||
|
@@ -174,14 +200,14 @@ type onWatchConfig<'config> = { | |
|
||
type t<'config> = { | ||
fileName: FileName.t, | ||
setup: setupConfig => promise<'config>, | ||
setup: setupConfig => promise<setupResult<'config>>, | ||
generate: generateConfig<'config> => promise<result<generated, string>>, | ||
cliHelpText: string, | ||
handleOtherCommand?: handleOtherCommandConfig<'config> => promise<unit>, | ||
onWatch?: onWatchConfig<'config> => promise<unit>, | ||
} | ||
|
||
let defaultSetup = async _ => () | ||
let defaultSetup = async (_): setupResult<_> => SetupResult({config: ()}) | ||
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. As promised, the return type is a breaking change. |
||
|
||
let make = ( | ||
~extensionPattern, | ||
|
@@ -348,6 +374,12 @@ let cleanUpExtraFiles = (t, ~outputDir, ~sourceFileModuleName="*", ~keepThese=[] | |
}) | ||
} | ||
|
||
type customWatchedFile = { | ||
glob: string, | ||
regexp: RegExp.t, | ||
onChange: watcherOnChangeConfig => promise<unit> | ||
} | ||
|
||
let runCli = async (t, ~args: option<array<string>>=?) => { | ||
let debugging = ref(false) | ||
let debug = msg => | ||
|
@@ -375,8 +407,17 @@ let runCli = async (t, ~args: option<array<string>>=?) => { | |
open Process | ||
process->exitWithCode(0) | ||
| Some("generate") => | ||
let config = await t.setup({args: args}) | ||
let watch = args->CliArgs.hasArg("--watch") | ||
let SetupResult({config, ?additionalFileWatchers, ?additionalIgnorePatterns}) = await t.setup({ | ||
args: args, | ||
}) | ||
let customWatchedFiles = | ||
Option.getOr(additionalFileWatchers, []) | ||
->Array.map(w => { | ||
glob: w.filePattern, | ||
regexp: MicroMatch.mm.makeRe(w.filePattern), | ||
onChange: w.onChange | ||
}) | ||
let pathToGeneratedDir = switch args->CliArgs.getArgValue(["--output"]) { | ||
| None => | ||
panic(`--output must be set. It controls into what directory all generated files are emitted.`) | ||
|
@@ -488,20 +529,29 @@ let runCli = async (t, ~args: option<array<string>>=?) => { | |
|
||
if watch { | ||
await runGeneration() | ||
Console.log(`Watching for changes in ${src}...`) | ||
let watchedFiles = | ||
Array.map(customWatchedFiles, f => f.glob) | ||
->Array.concat([`${src}/**/*.res`]) | ||
Console.log(`Watching the following patterns for file changes...`) | ||
Array.forEach(watchedFiles, f => Console.log(`- ${f}`)) | ||
Comment on lines
+535
to
+536
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. Tweaked the display here so we can show all the globs we're keeping an eye on. |
||
|
||
let _theWatcher = | ||
Chokidar.watcher | ||
->Chokidar.watch( | ||
`${src}/**/*.res`, | ||
watchedFiles, | ||
~options={ | ||
ignored: ["**/node_modules", pathToGeneratedDir], | ||
ignored: | ||
Option.getOr(additionalIgnorePatterns, []) | ||
->Array.concat(["**/node_modules", pathToGeneratedDir]), | ||
ignoreInitial: true, | ||
}, | ||
) | ||
->Chokidar.Watcher.onChange(async file => { | ||
debug(`[changed]: ${file}`) | ||
await runGeneration(~files=[file]) | ||
switch Array.find(customWatchedFiles, w => RegExp.test(w.regexp, file)) { | ||
| Some({onChange}) => await onChange({ runGeneration, file }) | ||
| None => await runGeneration(~files=[file]) | ||
} | ||
}) | ||
->Chokidar.Watcher.onAdd(async file => { | ||
debug(`[added]: ${file}`) | ||
|
@@ -534,8 +584,10 @@ let runCli = async (t, ~args: option<array<string>>=?) => { | |
| Some(otherCommand) => | ||
switch t.handleOtherCommand { | ||
| None => Console.log(t.cliHelpText) | ||
| Some(handleOtherCommand) => | ||
await handleOtherCommand({args, command: otherCommand, config: await t.setup({args: args})}) | ||
| Some(handleOtherCommand) => { | ||
let SetupResult({config}) = await t.setup({args: args}) | ||
await handleOtherCommand({args, command: otherCommand, config}) | ||
} | ||
} | ||
| None => Console.log(t.cliHelpText) | ||
} | ||
|
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.
Now that we might have multiple globs, swapped out the overload we're hitting.