Skip to content

Commit

Permalink
Support setting flags globally and locally using cosmiconfig (#354)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
itaisteinherz and sindresorhus committed Mar 31, 2019
1 parent c7d4cd0 commit 9dac7aa
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 9 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -31,12 +31,14 @@
"any-observable": "^0.3.0",
"async-exit-hook": "^2.0.1",
"chalk": "^2.3.0",
"cosmiconfig": "^5.1.0",
"del": "^3.0.0",
"execa": "^1.0.0",
"github-url-from-git": "^1.5.0",
"has-yarn": "^1.0.0",
"hosted-git-info": "^2.7.1",
"inquirer": "^6.2.1",
"is-installed-globally": "^0.1.0",
"is-scoped": "^1.0.0",
"issue-regex": "^2.0.0",
"listr": "^0.14.3",
Expand Down
46 changes: 46 additions & 0 deletions readme.md
Expand Up @@ -87,6 +87,52 @@ Run `np` without arguments to launch the interactive UI that guides you through
<img src="screenshot-ui.png" width="1290">


## Config

`np` can be configured both locally and globally. When using the global `np` binary, you can configure any of the CLI flags in either a `.np-config.js` or `.np-config.json` file in the home directory. When using the local `np` binary, for example, in a `npm run` script, you can configure `np` by setting the flags in either a top-level `np` field in `package.json` or in a `.np-config.js` or `.np-config.json` file in the project directory.

Currently, these are the flags you can configure:

- `anyBranch` - Allow publishing from any branch (`false` by default).
- `cleanup` - Cleanup `node_modules` (`true` by default).
- `yolo` - Skip cleanup and testing (`false` by default).
- `publish` - Publish (`true` by default).
- `tag` - Publish under a given dist-tag (`latest` by default).
- `yarn` - Use yarn if possible (`true` by default).
- `contents` - Subdirectory to publish (`.` by default).

For example, this configures `np` to never use Yarn and to use `dist` as the subdirectory to publish:

`package.json`
```json
{
"name": "superb-package",
"np": {
"yarn": false,
"contents": "dist"
}
}
```

`.np-config.json`
```json
{
"yarn": false,
"contents": "dist"
}
```

`.np-config.js`
```js
module.exports = {
yarn: false,
contents: 'dist'
};
```

_**Note:** The global config only applies when using the global `np` binary, and is never inherited when using a local binary._


## Tips

### npm hooks
Expand Down
30 changes: 21 additions & 9 deletions source/cli.js
Expand Up @@ -6,6 +6,7 @@ const logSymbols = require('log-symbols');
const meow = require('meow');
const updateNotifier = require('update-notifier');
const hasYarn = require('has-yarn');
const config = require('./config');
const {isPackageNameAvailable} = require('./npm/util');
const version = require('./version');
const util = require('./util');
Expand Down Expand Up @@ -40,22 +41,19 @@ const cli = meow(`
type: 'boolean'
},
cleanup: {
type: 'boolean',
default: true
type: 'boolean'
},
yolo: {
type: 'boolean'
},
publish: {
type: 'boolean',
default: true
type: 'boolean'
},
tag: {
type: 'string'
},
yarn: {
type: 'boolean',
default: hasYarn()
type: 'boolean'
},
contents: {
type: 'string'
Expand All @@ -68,16 +66,30 @@ updateNotifier({pkg: cli.pkg}).notify();
(async () => {
const pkg = util.readPkg();

const isAvailable = cli.flags.publish ? await isPackageNameAvailable(pkg) : false;
const defaultFlags = {
cleanup: true,
publish: true,
yarn: hasYarn()
};

const localConfig = await config();

const flags = {
...defaultFlags,
...localConfig,
...cli.flags
};

const isAvailable = flags.publish ? await isPackageNameAvailable(pkg) : false;

const options = cli.input.length > 0 ?
{
...cli.flags,
...flags,
confirm: true,
exists: !isAvailable,
version: cli.input[0]
} :
await ui({...cli.flags, exists: !isAvailable}, pkg);
await ui({...flags, exists: !isAvailable}, pkg);

if (!options.confirm) {
process.exit(0);
Expand Down
21 changes: 21 additions & 0 deletions source/config.js
@@ -0,0 +1,21 @@
'use strict';
const os = require('os');
const isInstalledGlobally = require('is-installed-globally');
const pkgDir = require('pkg-dir');
const cosmiconfig = require('cosmiconfig');

module.exports = async () => {
const searchDir = isInstalledGlobally ? os.homedir() : await pkgDir();
const searchPlaces = ['.np-config.json', '.np-config.js'];
if (!isInstalledGlobally) {
searchPlaces.push('package.json');
}

const explorer = cosmiconfig('np', {
searchPlaces,
stopDir: searchDir
});
const {config} = (await explorer.search(searchDir)) || {};

return config;
};

0 comments on commit 9dac7aa

Please sign in to comment.