Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make options required #260

Merged
merged 4 commits into from Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions readme.md
Expand Up @@ -45,7 +45,7 @@ const cli = meow(`
$ foo unicorns --rainbow
🌈 unicorns 🌈
`, {
importMeta: import.meta,
importMeta: import.meta, // This is required
flags: {
rainbow: {
type: 'boolean',
Expand All @@ -66,7 +66,7 @@ foo(cli.input.at(0), cli.flags);

## API

### meow(helpText, options?)
### meow(helpText, options)
### meow(options)

Returns an `object` with:
Expand All @@ -91,6 +91,7 @@ Type: `object`

##### importMeta

**Required**\
Type: `object`

Pass in [`import.meta`](https://nodejs.org/dist/latest/docs/api/esm.html#esm_import_meta). This is used to find the correct package.json file.
Expand Down
6 changes: 3 additions & 3 deletions source/index.d.ts
Expand Up @@ -398,7 +398,7 @@ const cli = meow(`
$ foo unicorns --rainbow
🌈 unicorns 🌈
`, {
importMeta: import.meta,
importMeta: import.meta, // This is required
flags: {
rainbow: {
type: 'boolean',
Expand All @@ -416,5 +416,5 @@ const cli = meow(`
foo(cli.input.at(0), cli.flags);
```
*/
export default function meow<Flags extends AnyFlags>(helpMessage: string, options?: Options<Flags>): Result<Flags>;
export default function meow<Flags extends AnyFlags>(options?: Options<Flags>): Result<Flags>;
export default function meow<Flags extends AnyFlags>(helpMessage: string, options: Options<Flags>): Result<Flags>;
export default function meow<Flags extends AnyFlags>(options: Options<Flags>): Result<Flags>;
3 changes: 2 additions & 1 deletion test-d/build.test-d.ts
Expand Up @@ -6,7 +6,8 @@ type AnyFlag = NonNullable<NonNullable<Parameters<typeof meow>[0]>['flags']>[str

const importMeta = import.meta;

expectType<Result<never>>(meow('Help text'));
expectError(meow('Help text'));
expectError(meow('Help text', {}));
expectType<Result<never>>(meow('Help text', {importMeta}));
expectAssignable<{flags: {foo: number}}>(
meow({importMeta: import.meta, flags: {foo: {type: 'number', isRequired: true}}}),
Expand Down
3 changes: 2 additions & 1 deletion test-d/index.test-d.ts
Expand Up @@ -6,7 +6,8 @@ type AnyFlag = NonNullable<NonNullable<Parameters<typeof meow>[0]>['flags']>[str

const importMeta = import.meta;

expectType<Result<never>>(meow('Help text'));
expectError(meow('Help text'));
expectError(meow('Help text', {}));
expectType<Result<never>>(meow('Help text', {importMeta, hardRejection: false}));
expectAssignable<{flags: {foo: number}}>(
meow({importMeta: import.meta, flags: {foo: {type: 'number', isRequired: true}}}),
Expand Down
53 changes: 37 additions & 16 deletions test/options/import-meta.js
@@ -1,24 +1,45 @@
import test from 'ava';
import meow from '../../source/index.js';

test('main', t => {
t.notThrows(() => meow({importMeta: import.meta}));
const verifyImportMeta = test.macro((t, {cli, error}) => {
if (error) {
t.throws(cli, {message: 'The `importMeta` option is required. Its value must be `import.meta`.'});
} else {
t.notThrows(cli);
}
});

test('with help shortcut', t => {
t.notThrows(() => (
meow(`
unicorns
rainbows
`, {
importMeta: import.meta,
})
));
test('main', verifyImportMeta, {
cli: () => meow({
importMeta: import.meta,
}),
});

test('invalid package url', t => {
t.throws(
() => meow({importMeta: '/path/to/package'}),
{message: 'The `importMeta` option is required. Its value must be `import.meta`.'},
);
test('with help shortcut', verifyImportMeta, {
cli: () => meow(`
unicorns
rainbows
`, {
importMeta: import.meta,
}),
});

test('invalid package url', verifyImportMeta, {
cli: () => meow({importMeta: '/path/to/package'}),
error: true,
});

test('throws if unset', verifyImportMeta, {
cli: () => meow('foo', {}),
error: true,
});

test('throws if unset - options only', verifyImportMeta, {
cli: () => meow({}),
error: true,
});

test('throws if unset - help shortcut only', verifyImportMeta, {
cli: () => meow('foo'),
error: true,
});