Skip to content

Commit

Permalink
feat: add "watch" option to cms config (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenle authored Oct 7, 2024
1 parent 75e2121 commit 9fe5fb9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-lamps-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blinkk/root-cms': patch
---

feat: add "watch" option to cms config
34 changes: 22 additions & 12 deletions packages/root-cms/core/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ export type CMSPluginOptions = {
* Adjust console output verbosity. Default is `'info'`.
*/
logLevel?: 'info' | 'warn' | 'error' | 'silent' | 'debug';

/**
* Whether to enable/disable file watching in dev (e.g. for generating
* `root-cms.d.ts`). Defaults to `true`.
*/
watch?: boolean;
};

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

/**
* Watches for .schema.ts file changes and generates a root-cms.d.ts file.
*/
onFileChange: (
eventName: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
filepath: string
) => {
if (filepath.endsWith('.schema.ts')) {
generateTypes();
}
},

/**
* Attaches CMS-specific middleware to the Root.js server.
*/
Expand Down Expand Up @@ -588,6 +582,22 @@ export function cmsPlugin(options: CMSPluginOptions): CMSPlugin {
},
};

if (options.watch !== false) {
plugin.onFileChange = (
eventName: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
filepath: string
) => {
if (filepath.endsWith('.schema.ts')) {
// Avoid loading the `generate-types.js` module if it is not needed.
// See: https://github.com/blinkk/rootjs/issues/426
import('../cli/generate-types.js').then((generateTypesModule) => {
const generateTypes = generateTypesModule.generateTypes;
generateTypes();
});
}
};
}

return plugin;
}

Expand Down

0 comments on commit 9fe5fb9

Please sign in to comment.