-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add global specification watcher (#220)
Co-authored-by: Lukasz Gornicki <[email protected]> Co-authored-by: Maciej Urbańczyk <[email protected]>
- Loading branch information
1 parent
663b260
commit 1021b6b
Showing
5 changed files
with
82 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { flags } from '@oclif/command'; | ||
|
||
export const watchFlag = flags.boolean({ | ||
char: 'w', | ||
description: 'Enable watch mode' | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import chokidar from 'chokidar'; | ||
import chalk from 'chalk'; | ||
import Command from './base'; | ||
import { Specification } from './models/SpecificationFile'; | ||
|
||
const GreenLog = chalk.hex('#00FF00'); | ||
const OrangeLog = chalk.hex('#FFA500'); | ||
const CHOKIDAR_CONFIG = { | ||
// awaitWriteFinish: true // Used for large size specification files. | ||
|
||
}; | ||
const WATCH_MESSAGES = { | ||
logOnStart: (filePath: string) => console.log(GreenLog(`Watching AsyncAPI file at ${filePath}\n`)), | ||
logOnChange: (handlerName: string) => console.log(OrangeLog(`Change detected, running ${handlerName}\n`)), | ||
logOnAutoDisable: (docVersion: 'old' | 'new' | '' = '') => console.log(OrangeLog(`Watch mode for ${docVersion || 'AsyncAPI'} file was not enabled.`), OrangeLog('\nINFO: Watch works only with files from local file system\n')) | ||
}; | ||
|
||
const CHOKIDAR_INSTANCE_STORE = new Map<string, boolean>(); | ||
|
||
export type specWatcherParams = { | ||
spec: Specification, | ||
handler: Command, | ||
handlerName: string, | ||
label?: string, | ||
docVersion?: 'old' | 'new'; | ||
} | ||
|
||
export const specWatcher = (params: specWatcherParams) => { | ||
if (!params.spec.getFilePath()) { return WATCH_MESSAGES.logOnAutoDisable(params.docVersion); } | ||
if (CHOKIDAR_INSTANCE_STORE.get(params.label || '_default')) { return; } | ||
|
||
const filePath = params.spec.getFilePath() as string; | ||
try { | ||
WATCH_MESSAGES.logOnStart(filePath); | ||
chokidar | ||
.watch(filePath, CHOKIDAR_CONFIG) | ||
.on('change', async () => { | ||
WATCH_MESSAGES.logOnChange(params.handlerName); | ||
try { | ||
await params.handler.run(); | ||
} catch (err) { | ||
await params.handler.catch(err); | ||
} | ||
}); | ||
CHOKIDAR_INSTANCE_STORE.set(params.label || '_default', true); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters