Skip to content

Commit

Permalink
feat: add watch option
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Aug 17, 2024
1 parent 21e9d52 commit e6be867
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ can change openapi file name

path to an aspida config file

### `-w`, `--watch`

enable watch mode

### `--version`

display version
Expand Down
16 changes: 7 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
],
"dependencies": {
"aspida": "^1.14.0",
"chokidar": "^3.6.0",
"openapi-types": "^12.1.3",
"typescript-json-schema": "^0.64.0"
},
Expand Down
21 changes: 15 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { getConfigs } from 'aspida/dist/cjs/getConfigs';
import minimist from 'minimist';
import build from '.';
import type { ConfigFile } from './getConfig';
import type { ConfigFile, PartialConfig } from './getConfig';
import { watchIndexFiles } from './watchIndexFiles';

export const run = (args: string[]) => {
const argv: Record<string, string | undefined> = minimist(args, {
string: ['version', 'config', 'output'],
alias: { v: 'version', c: 'config', o: 'output' },
string: ['version', 'config', 'output', 'watch'],
alias: { v: 'version', c: 'config', o: 'output', w: 'watch' },
});

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

if (configs.length > 1) {
build(configs);

if (argv.watch !== undefined) {
configs.forEach((config) => watchIndexFiles(config.input, () => build(config)));
}

return;
}

const config = configs[0];

build({
const option: PartialConfig = {
...config,
openapi: {
...config.openapi,
outputFile: argv.output ?? config.openapi?.outputFile,
},
});
};

build(option);

if (argv.watch !== undefined) watchIndexFiles(config.input, () => build(option));
};
7 changes: 7 additions & 0 deletions src/watchIndexFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import chokidar from 'chokidar';

export const watchIndexFiles = (dir: string, callback: () => void) => {
chokidar
.watch(dir, { ignoreInitial: true, ignored: /^(?!.*\/index\.ts$).*$/ })
.on('all', callback);
};

0 comments on commit e6be867

Please sign in to comment.