Skip to content

Commit 9fe5fb9

Browse files
authored
feat: add "watch" option to cms config (#427)
1 parent 75e2121 commit 9fe5fb9

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

.changeset/giant-lamps-matter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@blinkk/root-cms': patch
3+
---
4+
5+
feat: add "watch" option to cms config

packages/root-cms/core/plugin.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ export type CMSPluginOptions = {
162162
* Adjust console output verbosity. Default is `'info'`.
163163
*/
164164
logLevel?: 'info' | 'warn' | 'error' | 'silent' | 'debug';
165+
166+
/**
167+
* Whether to enable/disable file watching in dev (e.g. for generating
168+
* `root-cms.d.ts`). Defaults to `true`.
169+
*/
170+
watch?: boolean;
165171
};
166172

167173
export type CMSPlugin = Plugin & {
@@ -433,18 +439,6 @@ export function cmsPlugin(options: CMSPluginOptions): CMSPlugin {
433439
cms: path.resolve(__dirname, './app.js'),
434440
}),
435441

436-
/**
437-
* Watches for .schema.ts file changes and generates a root-cms.d.ts file.
438-
*/
439-
onFileChange: (
440-
eventName: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
441-
filepath: string
442-
) => {
443-
if (filepath.endsWith('.schema.ts')) {
444-
generateTypes();
445-
}
446-
},
447-
448442
/**
449443
* Attaches CMS-specific middleware to the Root.js server.
450444
*/
@@ -588,6 +582,22 @@ export function cmsPlugin(options: CMSPluginOptions): CMSPlugin {
588582
},
589583
};
590584

585+
if (options.watch !== false) {
586+
plugin.onFileChange = (
587+
eventName: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
588+
filepath: string
589+
) => {
590+
if (filepath.endsWith('.schema.ts')) {
591+
// Avoid loading the `generate-types.js` module if it is not needed.
592+
// See: https://github.com/blinkk/rootjs/issues/426
593+
import('../cli/generate-types.js').then((generateTypesModule) => {
594+
const generateTypes = generateTypesModule.generateTypes;
595+
generateTypes();
596+
});
597+
}
598+
};
599+
}
600+
591601
return plugin;
592602
}
593603

0 commit comments

Comments
 (0)