tldraw/apps/docs/watcher.ts
David Sheldrick 9d895aab13
[docs] Fix missing Persistence page (#2828)
Fixes #2826, extracted from #2680 

The problem is that we had two different articles whose ids were being
derived as `persistence`, the `persistence.mdx` file and the
`persistence/` example.

I've

1. Made it an error for two articles to have the same id
2. Renamed the `persistence/` article to `local-storage` since that's
the API it's using.

### Change Type

- [ ] `patch` — Bug fix
- [ ] `minor` — New feature
- [ ] `major` — Breaking change
- [ ] `dependencies` — Changes to package dependencies[^1]
- [x] `documentation` — Changes to the documentation only[^2]
- [ ] `tests` — Changes to any test code only[^2]
- [ ] `internal` — Any other changes that don't affect the published
package[^2]
- [ ] I don't know

[^1]: publishes a `patch` release, for devDependencies use `internal`
[^2]: will not publish a new version

### Test Plan

1. Add a step-by-step description of how to test your PR here.
3.

- [ ] Unit Tests
- [ ] End to end tests

### Release Notes

- Add a brief release note for your PR here.
2024-02-13 16:19:17 +00:00

35 lines
938 B
TypeScript

import fs from 'fs'
import { WebSocketServer } from 'ws'
import { refreshContent } from './scripts/functions/refreshContent'
import { debounce } from './utils/debounce'
import { nicelog } from './utils/nicelog'
refreshContent({ silent: true })
fs.watch(
'content',
{ persistent: true, recursive: true },
debounce(async (eventType, fileName) => {
nicelog(`Refreshing after ${eventType}: ${fileName}`)
// todo: if a file was only updated, then only update the file that changed, any links that point to it, etc.
try {
await refreshContent({ silent: true })
clients.forEach((ws) => ws.send('refresh'))
} catch (e: any) {
nicelog(`x Could not refresh content: ${e.message}`)
}
}, 250)
)
const wss = new WebSocketServer({ port: 3201 })
const clients = new Set<any>()
wss.on('connection', function connection(ws) {
clients.add(ws)
ws.on('error', console.error)
ws.on('close', () => {
clients.delete(ws)
})
})