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

Allow user to pass in package URL for package reading #182

Merged
merged 14 commits into from May 5, 2021
1 change: 1 addition & 0 deletions estest/index.js
Expand Up @@ -13,6 +13,7 @@ meow(
🌈 unicorns 🌈
`,
{
packagePath: import.meta.url,
flags: {
rainbow: {
type: 'boolean',
Expand Down
8 changes: 8 additions & 0 deletions index.d.ts
Expand Up @@ -151,6 +151,7 @@ export interface Options<Flags extends AnyFlags> {
$ foo
🌈 unicorns✨🌈
`, {
packagePath: import.meta.url,
booleanDefault: undefined,
flags: {
rainbow: {
Expand Down Expand Up @@ -206,6 +207,13 @@ export interface Options<Flags extends AnyFlags> {
@default true
*/
readonly allowUnknownFlags?: boolean;

/**
Directory to start looking for the module package.json file.

@default process.cwd()
*/
readonly packagePath?: string;
}

type TypedFlag<Flag extends AnyFlag> =
Expand Down
12 changes: 8 additions & 4 deletions index.js
@@ -1,11 +1,13 @@
import {dirname} from 'node:path';
import {fileURLToPath} from 'node:url';
import buildParserOptions from 'minimist-options';
import parseArguments from 'yargs-parser';
import camelCaseKeys from 'camelcase-keys';
import decamelize from 'decamelize';
import decamelizeKeys from 'decamelize-keys';
import trimNewlines from 'trim-newlines';
import redent from 'redent';
import {readPackageUpSync} from 'read-pkg-up';
import {readPackageSync} from 'read-pkg';
LitoMore marked this conversation as resolved.
Show resolved Hide resolved
import hardRejection from 'hard-rejection';
import normalizePackageData from 'normalize-package-data';

Expand Down Expand Up @@ -97,18 +99,20 @@ const validateFlags = (flags, options) => {
}
};

const meow = (helpText, options) => {
const meow = (helpText, options = {}) => {
if (typeof helpText !== 'string') {
options = helpText;
helpText = '';
}

const foundPackage = readPackageUpSync({
const foundPackage = readPackageSync({
cwd: options.packagePath ? dirname(fileURLToPath(options.packagePath)) : process.cwd(),
normalize: false
});

options = {
pkg: foundPackage ? foundPackage.packageJson : {},
packagePath: process.cwd(),
LitoMore marked this conversation as resolved.
Show resolved Hide resolved
pkg: foundPackage || {},
argv: process.argv.slice(2),
flags: {},
inferType: false,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -49,6 +49,7 @@
"hard-rejection": "^2.1.0",
"minimist-options": "4.1.0",
"normalize-package-data": "^3.0.2",
"read-pkg": "^6.0.0",
"read-pkg-up": "^8.0.0",
"redent": "^3.0.0",
"trim-newlines": "^4.0.0",
Expand All @@ -59,7 +60,6 @@
"ava": "^3.15.0",
"execa": "^5.0.0",
"indent-string": "^5.0.0",
"read-pkg": "^6.0.0",
"tsd": "^0.14.0",
"xo": "^0.39.1"
},
Expand Down
54 changes: 11 additions & 43 deletions readme.md
Expand Up @@ -26,52 +26,11 @@ $ npm install meow
$ ./foo-app.js unicorns --rainbow
```

**CommonJS**

```js
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const foo = require('.');

const cli = meow(`
Usage
$ foo <input>

Options
--rainbow, -r Include a rainbow

Examples
$ foo unicorns --rainbow
🌈 unicorns 🌈
`, {
flags: {
rainbow: {
type: 'boolean',
alias: 'r'
}
}
});
/*
{
input: ['unicorns'],
flags: {rainbow: true},
...
}
*/

foo(cli.input[0], cli.flags);
```

**ES Modules**

```js
#!/usr/bin/env node
import {createRequire} from 'module';
import meow from 'meow';
import foo from './lib/index.js';

const meow = createRequire(import.meta.url)('meow');

const cli = meow(`
Usage
$ foo <input>
Expand All @@ -83,6 +42,7 @@ const cli = meow(`
$ foo unicorns --rainbow
🌈 unicorns 🌈
`, {
packagePath: import.meta.url,
flags: {
rainbow: {
type: 'boolean',
Expand Down Expand Up @@ -126,6 +86,13 @@ Shortcut for the `help` option.

Type: `object`

##### packagePath

Type: `string`\
Default: `process.cwd()`

Directory to start looking for the module package.json file.

##### flags

Type: `object`
Expand Down Expand Up @@ -253,7 +220,7 @@ __Caution: Explicitly specifying `undefined` for `booleanDefault` has different
Example:

```js
const meow = require('meow');
const meow from 'meow';

const cli = meow(`
Usage
Expand All @@ -268,6 +235,7 @@ const cli = meow(`
$ foo
🌈 unicorns✨🌈
`, {
packagePath: import.meta.url,
booleanDefault: undefined,
flags: {
rainbow: {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/fixture-allow-unknown-flags.js
@@ -1,7 +1,9 @@
#!/usr/bin/env node
import path from 'node:path';
import meow from '../../index.js';

const cli = meow({
packagePath: path.join(import.meta.url, '../..'),
description: 'Custom description',
help: `
Usage
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/fixture-required-function.js
@@ -1,7 +1,9 @@
#!/usr/bin/env node
import path from 'node:path';
import meow from '../../index.js';

const cli = meow({
packagePath: path.join(import.meta.url, '../..'),
description: 'Custom description',
help: `
Usage
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/fixture-required-multiple.js
@@ -1,7 +1,9 @@
#!/usr/bin/env node
import path from 'node:path';
import meow from '../../index.js';

const cli = meow({
packagePath: path.join(import.meta.url, '../..'),
description: 'Custom description',
help: `
Usage
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/fixture-required.js
@@ -1,8 +1,10 @@
#!/usr/bin/env node
'use strict';
import path from 'node:path';
import meow from '../../index.js';

const cli = meow({
packagePath: path.join(import.meta.url, '../..'),
description: 'Custom description',
help: `
Usage
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/fixture.js
@@ -1,7 +1,9 @@
#!/usr/bin/env node
import path from 'node:path';
import meow from '../../index.js';

const cli = meow({
packagePath: path.join(import.meta.url, '../..'),
description: 'Custom description',
help: `
Usage
Expand Down