Skip to content

Commit 69665af

Browse files
authored
Merge pull request #2210 from hey-api/fix/bin-watch
fix(cli): correctly handle watch mode
2 parents c778f69 + 5b1362a commit 69665af

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

.changeset/young-years-push.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix(cli): correctly detect watch mode

packages/openapi-ts-tests/test/openapi-ts.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export default defineConfig(() => {
6969
// project: 'upload-openapi-spec',
7070
validate_EXPERIMENTAL: true,
7171
// version: '1.0.0',
72-
// watch: 5_000,
7372
// watch: {
7473
// enabled: true,
7574
// interval: 500,

packages/openapi-ts/bin/index.cjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,13 @@ async function start() {
129129
}
130130

131131
const context = await createClient(userConfig);
132-
if (!context[0] || !context[0].config.watch) {
132+
if (
133+
!context[0] ||
134+
!context[0].config ||
135+
!context[0].config.input ||
136+
!context[0].config.input.watch ||
137+
!context[0].config.input.watch.enabled
138+
) {
133139
process.exit(0);
134140
}
135141
} catch {

packages/openapi-ts/src/createClient.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -132,26 +132,8 @@ export const compileInputPath = (
132132
return result;
133133
};
134134

135-
const logInputPath = ({
136-
config,
137-
inputPath,
138-
watch,
139-
}: {
140-
config: Config;
141-
inputPath: ReturnType<typeof compileInputPath>;
142-
watch?: boolean;
143-
}) => {
144-
if (config.logs.level === 'silent') {
145-
return;
146-
}
147-
148-
if (watch) {
149-
console.clear();
150-
}
151-
152-
const baseString = watch
153-
? colors.magenta('Input changed, generating from')
154-
: colors.cyan('Generating from');
135+
const logInputPath = (inputPath: ReturnType<typeof compileInputPath>) => {
136+
const baseString = colors.cyan('Generating from');
155137

156138
if (typeof inputPath.path === 'string') {
157139
const baseInput = isPlatformPath(inputPath.path)
@@ -192,18 +174,20 @@ export const createClient = async ({
192174
}: {
193175
config: Config;
194176
templates: Templates;
177+
/**
178+
* Always falsy on the first run, truthy on subsequent runs.
179+
*/
195180
watch?: WatchValues;
196181
}) => {
197182
const inputPath = compileInputPath(config.input);
198183
const { timeout } = config.input.watch;
199184

200185
const watch: WatchValues = _watch || { headers: new Headers() };
201186

202-
logInputPath({
203-
config,
204-
inputPath,
205-
watch: Boolean(_watch),
206-
});
187+
// on first run, print the message as soon as possible
188+
if (config.logs.level !== 'silent' && !_watch) {
189+
logInputPath(inputPath);
190+
}
207191

208192
Performance.start('spec');
209193
const { data, error, response } = await getSpec({
@@ -227,6 +211,13 @@ export const createClient = async ({
227211
let context: IR.Context | undefined;
228212

229213
if (data) {
214+
// on subsequent runs in watch mode, print the mssage only if we know we're
215+
// generating the output
216+
if (config.logs.level !== 'silent' && _watch) {
217+
console.clear();
218+
logInputPath(inputPath);
219+
}
220+
230221
Performance.start('input.patch');
231222
patchOpenApiSpec({ patchOptions: config.input.patch, spec: data });
232223
Performance.end('input.patch');

0 commit comments

Comments
 (0)