Skip to content

Commit

Permalink
fix: move the fig complete import to an optional require (#1080)
Browse files Browse the repository at this point in the history
By moving the `@fig/complete-commander` package to an optional require
done at runtime, we can avoid jest trying to import it and causing an
error about ESM packages. This should also generally resolve any errors
even if someone needs to use the completion package, due to the forced
use of the CJS require statement

ref: #1077
  • Loading branch information
jmcdo29 authored Dec 4, 2023
2 parents 66bbbe2 + 72b2a00 commit 4596445
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/stale-pillows-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'nest-commander': patch
---

Move the fig completion package to an optional import to get around jest
throwing an error about esm packages
12 changes: 9 additions & 3 deletions packages/nest-commander/src/completion.factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { addCompletionSpecCommand } from '@fig/complete-commander';
import type { INestApplicationContext } from '@nestjs/common';
import { Command } from 'commander';
import type { CompletionFactoryOptions } from './completion.factory.interface';
Expand Down Expand Up @@ -50,7 +48,15 @@ export class CompletionFactory {
}

if (parsedOptions.fig) {
addCompletionSpecCommand(commander);
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { addCompletionSpecCommand } = require('@fig/complete-commander');
addCompletionSpecCommand(commander);
} catch {
throw new Error(
`There was a problem creating the fig completion. Did you make sure to install "@fig/complete-commander"?`,
);
}
}
}

Expand Down

0 comments on commit 4596445

Please sign in to comment.