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

Support setting flags globally and locally using cosmiconfig #354

Merged
merged 11 commits into from
Mar 31, 2019
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
"@samverschueren/stream-to-observable": "^0.3.0",
"any-observable": "^0.3.0",
"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
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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.

itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
Currently, these are the flags you can configure:

- `anyBranch` - Allow publishing from any branch (`false` by default).
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
- `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"
}
```
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved

`.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
Original file line number Diff line number Diff line change
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 @@ -73,16 +71,30 @@ process.on('SIGINT', () => {
(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
Original file line number Diff line number Diff line change
@@ -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;
};