Skip to content

Commit e6be867

Browse files
committed
feat: add watch option
1 parent 21e9d52 commit e6be867

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ can change openapi file name
3939

4040
path to an aspida config file
4141

42+
### `-w`, `--watch`
43+
44+
enable watch mode
45+
4246
### `--version`
4347

4448
display version

package-lock.json

Lines changed: 7 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
],
3232
"dependencies": {
3333
"aspida": "^1.14.0",
34+
"chokidar": "^3.6.0",
3435
"openapi-types": "^12.1.3",
3536
"typescript-json-schema": "^0.64.0"
3637
},

src/cli.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { getConfigs } from 'aspida/dist/cjs/getConfigs';
22
import minimist from 'minimist';
33
import build from '.';
4-
import type { ConfigFile } from './getConfig';
4+
import type { ConfigFile, PartialConfig } from './getConfig';
5+
import { watchIndexFiles } from './watchIndexFiles';
56

67
export const run = (args: string[]) => {
78
const argv: Record<string, string | undefined> = minimist(args, {
8-
string: ['version', 'config', 'output'],
9-
alias: { v: 'version', c: 'config', o: 'output' },
9+
string: ['version', 'config', 'output', 'watch'],
10+
alias: { v: 'version', c: 'config', o: 'output', w: 'watch' },
1011
});
1112

1213
if (argv.version !== undefined) {
@@ -18,16 +19,24 @@ export const run = (args: string[]) => {
1819

1920
if (configs.length > 1) {
2021
build(configs);
22+
23+
if (argv.watch !== undefined) {
24+
configs.forEach((config) => watchIndexFiles(config.input, () => build(config)));
25+
}
26+
2127
return;
2228
}
2329

2430
const config = configs[0];
25-
26-
build({
31+
const option: PartialConfig = {
2732
...config,
2833
openapi: {
2934
...config.openapi,
3035
outputFile: argv.output ?? config.openapi?.outputFile,
3136
},
32-
});
37+
};
38+
39+
build(option);
40+
41+
if (argv.watch !== undefined) watchIndexFiles(config.input, () => build(option));
3342
};

src/watchIndexFiles.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import chokidar from 'chokidar';
2+
3+
export const watchIndexFiles = (dir: string, callback: () => void) => {
4+
chokidar
5+
.watch(dir, { ignoreInitial: true, ignored: /^(?!.*\/index\.ts$).*$/ })
6+
.on('all', callback);
7+
};

0 commit comments

Comments
 (0)