Skip to content

Commit

Permalink
fix: move config watcher to graph command (#4362)
Browse files Browse the repository at this point in the history
* chore: move config watcher to graph command

* chore: npm run docs

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
anmonteiro and kodiakhq[bot] committed Mar 1, 2022
1 parent 3db51e2 commit 8df364e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
1 change: 1 addition & 0 deletions docs/commands/dev.md
Expand Up @@ -42,6 +42,7 @@ netlify dev
netlify dev
netlify dev -d public
netlify dev -c "hugo server -w" --targetPort 1313
netlify dev --graph
BROWSER=none netlify dev # disable browser auto opening
```

Expand Down
28 changes: 1 addition & 27 deletions src/commands/base-command.js
@@ -1,5 +1,4 @@
// @ts-check
const events = require('events')
const process = require('process')
const { format } = require('util')

Expand Down Expand Up @@ -29,7 +28,6 @@ const {
pollForToken,
sortOptions,
track,
watchDebounced,
} = require('../utils')

// Netlify CLI client id. Lives in bot@netlify.com
Expand Down Expand Up @@ -121,11 +119,6 @@ class BaseCommand extends Command {
await this.init(actionCommand)
debug(`${name}:preAction`)('end')
})
.hook('postAction', async () => {
if (this.configWatcherHandle) {
await this.configWatcherHandle.close()
}
})
)
}

Expand Down Expand Up @@ -437,27 +430,10 @@ class BaseCommand extends Command {
const globalConfig = await getGlobalConfig()
const { NetlifyAPI } = await jsClient

const configWatcher = new events.EventEmitter()

// Only set up a watcher if we know the config path.
if (configPath) {
const configWatcherHandle = await watchDebounced(configPath, {
depth: 1,
onChange: async () => {
const { config: newConfig } = await actionCommand.getConfig({ cwd, state, token, ...apiUrlOpts })

const normalizedNewConfig = normalizeConfig(newConfig)
configWatcher.emit('change', normalizedNewConfig)
},
})

// chokidar handler
this.configWatcherHandle = configWatcherHandle
}

actionCommand.netlify = {
// api methods
api: new NetlifyAPI(token || '', apiOpts),
apiOpts,
repositoryRoot,
// current site context
site: {
Expand All @@ -480,8 +456,6 @@ class BaseCommand extends Command {
globalConfig,
// state of current site dir
state,
// netlify.toml file watcher
configWatcher,
}
debug(`${this.name()}:init`)('end')
}
Expand Down
37 changes: 32 additions & 5 deletions src/commands/dev/dev.js
@@ -1,4 +1,5 @@
// @ts-check
const events = require('events')
const path = require('path')
const process = require('process')
const { promisify } = require('util')
Expand Down Expand Up @@ -35,13 +36,17 @@ const {
exit,
generateNetlifyGraphJWT,
getSiteInformation,
getToken,
injectEnvVariables,
log,
normalizeConfig,
openBrowser,
processOnExit,
startForwardProxy,
startLiveTunnel,
startProxy,
warn,
watchDebounced,
} = require('../../utils')

const { createDevExecCommand } = require('./dev-exec')
Expand Down Expand Up @@ -147,9 +152,7 @@ const runCommand = (command, env = {}) => {

return await cleanupBeforeExit({ exitCode: 1 })
})
;['SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGHUP', 'exit'].forEach((signal) => {
process.on(signal, async () => await cleanupBeforeExit({}))
})
processOnExit(async () => await cleanupBeforeExit({}))

return commandProcess
}
Expand Down Expand Up @@ -417,9 +420,31 @@ const dev = async (options, command) => {
return oneGraphSessionId
}

//
const configWatcher = new events.EventEmitter()

// Only set up a watcher if we know the config path.
const { configPath } = command.netlify.site
if (configPath) {
// chokidar handle
command.configWatcherHandle = await watchDebounced(configPath, {
depth: 1,
onChange: async () => {
const cwd = options.cwd || process.cwd()
const [token] = await getToken(options.auth)
const { config: newConfig } = await command.getConfig({ cwd, state, token, ...command.netlify.apiUrlOpts })

const normalizedNewConfig = normalizeConfig(newConfig)
configWatcher.emit('change', normalizedNewConfig)
},
})

processOnExit(async () => {
await command.configWatcherHandle.close()
})
}

// Set up a handler for config changes.
command.netlify.configWatcher.on('change', (newConfig) => {
configWatcher.on('change', (newConfig) => {
command.netlify.config = newConfig
stopWatchingCLISessions()
createOrResumeSession()
Expand Down Expand Up @@ -486,8 +511,10 @@ const createDevCommand = (program) => {
'netlify dev',
'netlify dev -d public',
'netlify dev -c "hugo server -w" --targetPort 1313',
'netlify dev --graph',
'BROWSER=none netlify dev # disable browser auto opening',
])
.action(dev)
}

module.exports = { createDevCommand }
8 changes: 8 additions & 0 deletions src/utils/dev.js
Expand Up @@ -215,9 +215,17 @@ const generateNetlifyGraphJWT = ({ authlifyTokenId, netlifyToken, siteId }) => {
)
}

const processOnExit = (fn) => {
const signals = ['SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGHUP', 'exit']
signals.forEach((signal) => {
process.on(signal, fn)
})
}

module.exports = {
getSiteInformation,
injectEnvVariables,
acquirePort,
generateNetlifyGraphJWT,
processOnExit,
}

1 comment on commit 8df364e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 443 MB

Please sign in to comment.