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

Add an option --access-token <token> to login using an access token #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,25 @@ Usage: nrm [options] [command]
use <registry> Change registry to registry
add <registry> <url> [home] Add one custom registry
login <registry> [value] Set authorize information for a registry with a base64 encoded string or username and pasword
-a --always-auth Set is always auth
-u --username <username> Your user name for this registry
-p --password <password> Your password for this registry
-e --email <email> Your email for this registry
-a --always-auth Set is always auth
-u --username <username> Your user name for this registry
-p --password <password> Your password for this registry
-e --email <email> Your email for this registry
-t --access-token <token> Your access token for this registry
set-hosted-repo <registry> <value> Set hosted npm repository for a custom registry to publish packages
set-scope <scopeName> <value> Associating a scope with a registry
del-scope <scopeName> Remove a scope
set <registryName> Set custom registry attribute
-a --attr <attr> Set custorm registry attribute
-v --value <value> Set custorm registry value
-a --attr <attr> Set custorm registry attribute
-v --value <value> Set custorm registry value
del <registry> Delete one custom registry
rename <registryName> <newName> Set custom registry name
home <registry> [browser] Open the homepage of registry with optional browser
publish [<tarball>|<folder>] Publish package to current registry if current registry is a custom registry. The field 'repository' of current custom registry is required running this command. If you're not using custom registry, this command will run npm publish directly
-t --tag [tag] Add tag
-a --access <public|restricted> Set access
-o --otp [otpcode] Set otpcode
-dr --dry-run Set is dry run
-t --tag [tag] Add tag
-a --access <public|restricted> Set access
-o --otp [otpcode] Set otpcode
-dr --dry-run Set is dry run
test [registry] Show the response time for one or all registries
help Print this help

Expand Down
9 changes: 6 additions & 3 deletions actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const {
isInternalRegistry,
} = require('./helpers');

const { NRMRC, NPMRC, AUTH, EMAIL, ALWAYS_AUTH, REPOSITORY, REGISTRY, HOME } = require('./constants');
const { NRMRC, NPMRC, AUTH, AUTH_TOKEN, EMAIL, ALWAYS_AUTH, REPOSITORY, REGISTRY, HOME } = require('./constants');

async function onList() {
const currentRegistry = await getCurrentRegistry();
Expand Down Expand Up @@ -101,7 +101,7 @@ async function onAdd(name, url, home) {
printSuccess(`Add registry ${name} success, run ${chalk.green('nrm use ' + name)} command to use ${name} registry.`);
}

async function onLogin(name, base64, { alwaysAuth, username, password, email }) {
async function onLogin(name, base64, { alwaysAuth, username, password, email, accessToken }) {
if (await isRegistryNotFound(name) || await isInternalRegistry(name, 'set authorization information of')) {
return;
}
Expand All @@ -112,8 +112,10 @@ async function onLogin(name, base64, { alwaysAuth, username, password, email })
registry[AUTH] = base64;
} else if (username && password) {
registry[AUTH] = Buffer.from(`${username}:${password}`).toString('base64');
} else if (accessToken) {
registry[AUTH_TOKEN] = accessToken;
} else {
return exit('Authorization information in base64 format or username & password is required');
return exit('Authorization information in base64 format or username & password or accessToken is required');
}

if (alwaysAuth) {
Expand All @@ -133,6 +135,7 @@ async function onLogin(name, base64, { alwaysAuth, username, password, email })
const npmrc = await readFile(NPMRC);
await writeFile(NPMRC, Object.assign(npmrc, {
[AUTH]: registry[AUTH],
[AUTH_TOKEN]: registry[AUTH_TOKEN],
[ALWAYS_AUTH]: registry[ALWAYS_AUTH],
[EMAIL]: registry[EMAIL],
}));
Expand Down
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ program
.option('-u, --username <username>', 'Your user name for this registry')
.option('-p, --password <password>', 'Your password for this registry')
.option('-e, --email <email>', 'Your email for this registry')
.description('Set authorize information for a custom registry with a base64 encoded string or username and password')
.option('-t, --access-token <token>', 'Your access token for this registry')
.description('Set authorize information for a custom registry with a base64 encoded string or username and password or access token')
.action(actions.onLogin);

program
Expand Down
4 changes: 3 additions & 1 deletion constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const REGISTRIES = require('./registries.json');

const HOME = 'home';
const AUTH = '_auth';
const AUTH_TOKEN = '_authToken';
const EMAIL = 'email';
const REGISTRY = 'registry';
const REPOSITORY = 'repository';
const ALWAYS_AUTH = 'always-auth';
const REGISTRY_ATTRS = [REGISTRY, HOME, AUTH, ALWAYS_AUTH];
const REGISTRY_ATTRS = [REGISTRY, HOME, AUTH, AUTH_TOKEN, ALWAYS_AUTH];
const NRMRC = path.join(process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'], '.nrmrc');
const NPMRC = path.join(process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'], '.npmrc');

Expand All @@ -16,6 +17,7 @@ module.exports = {
NPMRC,
REGISTRIES,
AUTH,
AUTH_TOKEN,
ALWAYS_AUTH,
REPOSITORY,
REGISTRY,
Expand Down
8 changes: 7 additions & 1 deletion tests/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,20 @@ describe('nrm command which needs to add a custom registry', () => {
it('login <name> [base64]', async () => {
const username = 'username';
const password = 'password';
const token = 'npmTkUsr01PkgABCdefG';

await coffee.spawn('nrm', ['login',`${__REGISTRY__}`,'-u', `${username}`, '-p', `${password}`], { shell: isWin })
.expect('stdout', /success/g)
.expect('code', 0)
.end();

await coffee.spawn('nrm', ['login',`${__REGISTRY__}`,'-t', `${token}`], { shell: isWin })
.expect('stdout', /success/g)
.expect('code', 0)
.end();

await coffee.spawn('nrm', ['login',`${__REGISTRY__}`], { shell: isWin })
.expect('stderr', /Authorization information in base64 format or username & password is required/g)
.expect('stderr', /Authorization information in base64 format or username & password or accessToken is required/g)
.end();
});
});
Expand Down