Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 12 additions & 27 deletions launcher/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import os from 'node:os'
import path from 'node:path'
import url, { fileURLToPath } from 'node:url'
import { getCurrentScope, init } from '@sentry/electron/main'
import chokidar from 'chokidar'
import debounceFn from 'debounce-fn'
import electron, { app, BrowserWindow, dialog, ipcMain } from 'electron'
import Store from 'electron-store'
Expand Down Expand Up @@ -285,7 +284,7 @@ if (!lock) {

let restartCounter = 0

/** @type {ReturnType<typeof chokidar.watch> | null} */
/** @type {import('fs').FSWatcher | null} */
let watcher = null
/** @type {boolean} */
let pendingWatcher = false
Expand All @@ -307,34 +306,16 @@ if (!lock) {

const newPath0 = uiConfig.get('dev_modules_path')
if (newPath0 && (await fs.pathExists(newPath0))) {
// Watch for changes in the modules
// Watch for changes in the module dev folder.
const devModulesPath = path.resolve(newPath0)
watcher = chokidar.watch('.', {
ignoreInitial: true,
cwd: devModulesPath,
ignored: (path, stats) => {
if (
stats?.isFile() &&
!path.endsWith('.mjs') &&
!path.endsWith('.js') &&
!path.endsWith('.cjs') &&
!path.endsWith('.json')
) {
return true
}
if (path.includes('node_modules')) {
return true
}
return false
},
})
const allowedExtensions = ['.mjs', '.js', '.cjs', '.json']
watcher = fs.watch(devModulesPath, { recursive: true, persistent: true }, (_event, filename) => {
if (!filename) return
if (filename.includes('node_modules')) return
if (!allowedExtensions.some((ext) => filename.endsWith(ext))) return

watcher.on('error', (error) => {
customLog(`Watcher error: ${error}`, 'Application')
})

watcher.on('all', (event, filename) => {
const moduleDirName = filename.split(path.sep)[0]
if (!moduleDirName) return

let fn = cachedDebounces[moduleDirName]
if (!fn) {
Expand All @@ -359,6 +340,10 @@ if (!lock) {
}
fn()
})

watcher.on('error', (error) => {
customLog(`Watcher error: ${error}`, 'Application')
})
}
} catch (e) {
customLog(`Failed to restart watcher: ${e}`, 'Application')
Expand Down
89 changes: 38 additions & 51 deletions tools/dev.mts
Original file line number Diff line number Diff line change
Expand Up @@ -143,59 +143,46 @@ const mainWatcher = chokidar
})

if (devModulesPath) {
// Stagger module watcher startup to avoid FD spike during initialization
mainWatcher.on('ready', () => {
chokidar
.watch('.', {
cwd: devModulesPath,
ignoreInitial: true,
ignored: (filePath, stats) => {
if (
stats?.isFile() &&
!filePath.endsWith('.mjs') &&
!filePath.endsWith('.js') &&
!filePath.endsWith('.cjs') &&
!filePath.endsWith('.json')
) {
return true
}
if (filePath.includes('node_modules')) {
return true
}
return false
},
})
.on('all', (event, filename) => {
const moduleDirName = filename.split(path.sep)[0]
// Module changed

let fn = cachedDebounces[moduleDirName]
if (!fn) {
fn = debounceFn(
() => {
console.log('Sending reload for module:', moduleDirName)
if (node) {
node.send({
messageType: 'reload-extra-module',
fullpath: path.join(devModulesPath, moduleDirName),
})
}
},
{
after: true,
before: false,
wait: 1000,
if (!fs.existsSync(devModulesPath)) {
console.warn(`Module watcher path does not exist: ${devModulesPath}`)
} else {
const allowedExtensions = ['.mjs', '.js', '.cjs', '.json']
const moduleWatcher = fs.watch(devModulesPath, { recursive: true, persistent: true }, (_event, filename) => {
if (!filename) return
if (filename.includes('node_modules')) return
if (!allowedExtensions.some((ext) => filename.endsWith(ext))) return

const moduleDirName = filename.split(path.sep)[0]
if (!moduleDirName) return
// Module changed

let fn = cachedDebounces[moduleDirName]
if (!fn) {
fn = debounceFn(
() => {
console.log('Sending reload for module:', moduleDirName)
if (node) {
node.send({
messageType: 'reload-extra-module',
fullpath: path.join(devModulesPath, moduleDirName),
})
}
)
cachedDebounces[moduleDirName] = fn
}
},
{
after: true,
before: false,
wait: 1000,
}
)
cachedDebounces[moduleDirName] = fn
}

fn()
})
.on('error', (error) => {
console.warn(`Module watcher error: ${error}`)
})
})
fn()
})
moduleWatcher.on('error', (error) => {
console.warn(`Module watcher error: ${error}`)
})
}
}

async function start() {
Expand Down
Loading