Skip to content

Commit

Permalink
feat(commands): option to specify .npmrc path with use (#13)
Browse files Browse the repository at this point in the history
* chore(readme): update readme

* feat(commands): option to specify .npmrc path with `use`
  • Loading branch information
trs authored May 18, 2018
1 parent a2fdf6e commit 825de3e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 42 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

## Synopsis

`npmum` is a command line tool to manage your npm login user.
`npmum` is a command line tool to manage your current `npm login` user.

By storing tokens under a username alias, the token in `~/.npmrc` will be updated based on the username.
If you've ever needed to publish to npm with multiple users, this tool is for you.

`npmum` stores tokens under a username alias. This allows you to easily switch between users by changing the token in `~/.npmrc` based on the username alias.

## Install

Expand All @@ -30,9 +32,18 @@ $ npm install npmum -g

## Usage

- First, obtain a token for your user from [npmjs.org](https://www.npmjs.com/)
- Run `npmum add <YOUR_USER_NAME>` and paste your token when prompted
- You can also pass in the token with the `--token` option
- Now, whenever you want to change to this user, simply run `npmum use <YOUR_USER_NAME>`
- Use `npmum ls` to list your users and the current active user
- To remove a user, run `npmum rm <YOUR_USER_NAME>`

## API

### Add

Will prompt you for a token to add under the name alias.
Prompts you for a token to add under the username alias.
You can also provide the token via `-t`/`--token`.

```
Expand All @@ -41,23 +52,23 @@ $ npmum add <user>[ --token <token>]

### Use

Use the provided user.
Use the token for provided username alias.

```
$ npmum use <user>
```

### Remove

Remove a user from the config.
Remove a user token from the config.

```
$ npmum rm <user>
```

### List

List users and their truncated tokens.
List users and their truncated tokens, along with the current selected user.

```
$ npmum ls
Expand Down
21 changes: 0 additions & 21 deletions src/commands/login.js

This file was deleted.

17 changes: 12 additions & 5 deletions src/commands/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,32 @@ function _writeNpmrc(path, text) {
});
}

function handle(name) {
function handle(name, options = {}) {
const user = storage.getUser(name);
const npmrcPath = `${require('os').homedir()}/.npmrc`;
const npmrcPath = options.path || `${require('os').homedir()}/.npmrc`;

return Promise.resolve()
.then(() => {
if (!user) throw new errors.UserNotFound();
if (!user.token) throw new errors.InvalidUserToken();

if (!fs.existsSync(npmrcPath)) return '';
return use._readNpmrc(npmrcPath);
})
.then(text => {
const regexp = /(_authToken=)(.*)/i;
const updatedText = text.replace(regexp, `$1${user.token}`);
return use._writeNpmrc(npmrcPath, updatedText);
if (regexp.test(text)) {
const updateText = text.replace(regexp, `$1${user.token}`);
return updateText;
}

const setNewText = `//registry.npmjs.org/:_authToken=${user.token}`;
return setNewText;
})
.then(text => use._writeNpmrc(npmrcPath, text))
.then(() => {
storage.setCurrentUser(name);
console.log(`NPM login user: ${name}.`);
console.log(`Set npm login user: "${name}".`);
return true;
})
.catch(errors.handle);
Expand Down
2 changes: 1 addition & 1 deletion src/errors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function handle(err) {
const message = err.message || 'Unknown error';
const code = err.code || 1;
console.log(message);
console.error(message);

process.exitCode = code;
return false;
Expand Down
14 changes: 5 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,29 @@ const ls = require('./commands/ls');
const rm = require('./commands/rm');
const use = require('./commands/use');
const add = require('./commands/add');
// const {login} = require('./commands/login');

updateNotifier({pkg: {name, version}, updateCheckInterval: 1000 * 60 * 60 * 6})
.notify({isGlobal: true});

function setup(argv) {
program.version(version);

// program.command('login')
// .description('Add user via npm login')
// .action(login);

program.command('add <name>')
.description('Add token with name')
.description('Add a user alias to an npm token')
.option('-t, --token <token>', 'Set token via command')
.action(add.handle);

program.command('ls')
.description('List token names')
.description('List user tokens and the current user')
.action(ls.handle);

program.command('rm')
.description('Remove token by name')
.description('Remove user token by name')
.action(rm.handle);

program.command('use <name>')
.description('Use token by name')
.description('Use user token by name')
.option('-p, --path <path>', 'Specify path to .npmrc')
.action(use.handle);

program.parse(argv);
Expand Down

0 comments on commit 825de3e

Please sign in to comment.