Skip to content

Commit

Permalink
Config autoreload
Browse files Browse the repository at this point in the history
  • Loading branch information
beagleknight committed Dec 3, 2024
1 parent 0468fff commit 3ef969a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

## TODO (road to opensource)

- [ ] cli
- [ ] config file
- [ ] socket path review
- [x] cli
- [x] config file + reload
- [x] socket path review
- [ ] git rebase
- [ ] rake tasks
- [ ] dependency graph and layers
- [ ] minio and aws
- [ ] global cache commiter + git
- [ ] initial generate
- [ ] lock file
2 changes: 1 addition & 1 deletion shadowdog.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "./src/config/schema.json",
"cache": {
"enabled": false
"enabled": true
},
"watchers": [
{
Expand Down
47 changes: 37 additions & 10 deletions src/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import { notifyState } from './notifications'
import { runTask } from './tasks'
import { logMessage } from './utils'
import chalk from 'chalk'
import { Client } from 'minio'

const watchFiles = (config: ConfigFile, socketPath: string) => {
const client = config.cache.enabled ? createClient() : null

return Promise.all(
const setupWatchers = (config: ConfigFile, socketPath: string, client: Client | null) => {
return Promise.all<chokidar.FSWatcher>(
config.watchers
.filter(({ files, enabled = true }) => {
if (!enabled) {
Expand Down Expand Up @@ -182,7 +181,7 @@ const watchFiles = (config: ConfigFile, socketPath: string) => {
)} folders.`,
)

resolve(null)
resolve(watcher)
}

watcher.on('change', debounce(onFileChange, config.debounceTime))
Expand All @@ -193,6 +192,38 @@ const watchFiles = (config: ConfigFile, socketPath: string) => {
)
}

const watchFiles = async (configFilePath: string, socketPath: string) => {
let currentWatchers: chokidar.FSWatcher[] = []
let currentConfig = loadConfig(configFilePath)

fs.mkdirpSync(currentConfig.cache.path)

const client = currentConfig.cache.enabled ? createClient() : null

const configWatcher = chokidar.watch(configFilePath, {
ignoreInitial: true,
})

configWatcher.on(
'change',
debounce(async () => {
logMessage(`🔃 Configuration file has been changed. Restarting Shadowdog...`)
try {
currentConfig = loadConfig(configFilePath)
await Promise.all(currentWatchers.map((watcher) => watcher.close()))
currentWatchers = await setupWatchers(currentConfig, socketPath, client)
logMessage(`🐕 Shadowdog has been restarted successfully.`)
} catch (error) {
logMessage(`🚨 Error while restarting Shadowdog: ${(error as Error).message}`)
}
}, currentConfig.debounceTime),
)

currentWatchers = await setupWatchers(currentConfig, socketPath, client)

return currentWatchers
}

export const runDaemon = async (configFilePath: string, socketPath: string) => {
logMessage(
`
Expand All @@ -206,11 +237,7 @@ export const runDaemon = async (configFilePath: string, socketPath: string) => {
false,
)

const config = loadConfig(configFilePath)

fs.mkdirpSync(config.cache.path)

await watchFiles(config, socketPath)
await watchFiles(configFilePath, socketPath)

logMessage('🚀 Shadowdog is ready to watch your files!')

Expand Down

0 comments on commit 3ef969a

Please sign in to comment.