Skip to content

Commit

Permalink
chore: fix module flow issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Feb 17, 2024
1 parent 645acc3 commit 3a4ebe0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 50 deletions.
1 change: 0 additions & 1 deletion launcher/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ if (!lock) {
})

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

let fn = cachedDebounces[moduleDirName]
Expand Down
1 change: 1 addition & 0 deletions module-local-dev/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*
!package.json
!.gitignore
4 changes: 4 additions & 0 deletions module-local-dev/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"packageManager": "[email protected]",
"license": "MIT"
}
111 changes: 62 additions & 49 deletions tools/dev.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,46 @@ const cachedDebounces = {}
chokidar
.watch(['**/*.mjs', '**/*.js', '**/*.cjs', '**/*.json'], {
ignoreInitial: true,
ignored: ['**/node_modules/**', './webui/', './launcher/', './dist/', './test/'],
cwd: '..',
ignored: ['**/node_modules/**', 'webui', 'launcher', 'module-local-dev', 'tools', 'test'],
})
.on('all', (event, filename) => {
const fullpath = path.resolve(filename)
if (fullpath.startsWith(devModulesPath)) {
const moduleDirName = fullpath.slice(devModulesPath.length + 1).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: 100,
}
)
cachedDebounces[moduleDirName] = fn
}
// Something else changed
restart()
})

fn()
} else {
// Something else changed
restart()
chokidar
.watch(['**/*.mjs', '**/*.js', '**/*.cjs', '**/*.json'], {
ignoreInitial: true,
cwd: devModulesPath,
ignored: ['**/node_modules/**'],
})
.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: 100,
}
)
cachedDebounces[moduleDirName] = fn
}

fn()
})

await start()
Expand All @@ -81,31 +87,38 @@ async function start() {
})
}

function restart() {
if (node) {
// Check if process has already exited
if (node.exitCode !== null) node = null
const restart = debounceFn(
() => {
if (node) {
// Check if process has already exited
if (node.exitCode !== null) node = null

// Try and kill the process
if (node && !node.kill()) {
console.error('Failed to kill')
process.exit(1)
// Try and kill the process
if (node && !node.kill()) {
console.error('Failed to kill')
process.exit(1)
}
}
}

console.log('********')
console.log('RESTARTING')
console.log('********')
console.log('********')
console.log('RESTARTING')
console.log('********')

if (!node) {
start()
} else if (node.listenerCount('close') === 0) {
node.on('close', () => {
node = null
if (!node) {
start()
})
} else if (node.listenerCount('close') === 0) {
node.on('close', () => {
node = null
start()
})
}
},
{
after: true,
before: false,
wait: 100,
}
}
)

function signalHandler(signal) {
process.exit()
Expand Down

0 comments on commit 3a4ebe0

Please sign in to comment.