From ccf6df623a87c46b1dc0d6d1e57b9d7170b80ac1 Mon Sep 17 00:00:00 2001 From: Gao Yang Date: Wed, 19 Jul 2023 22:28:16 +0800 Subject: [PATCH] Add an option --access-token to login using an access token --- README.md | 21 +++++++++++---------- actions.js | 9 ++++++--- cli.js | 3 ++- constants.js | 4 +++- tests/cli.test.js | 8 +++++++- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f88ad47..d594fac 100644 --- a/README.md +++ b/README.md @@ -51,24 +51,25 @@ Usage: nrm [options] [command] use Change registry to registry add [home] Add one custom registry login [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 Your user name for this registry - -p --password Your password for this registry - -e --email Your email for this registry + -a --always-auth Set is always auth + -u --username Your user name for this registry + -p --password Your password for this registry + -e --email Your email for this registry + -t --access-token Your access token for this registry set-hosted-repo Set hosted npm repository for a custom registry to publish packages set-scope Associating a scope with a registry del-scope Remove a scope set Set custom registry attribute - -a --attr Set custorm registry attribute - -v --value Set custorm registry value + -a --attr Set custorm registry attribute + -v --value Set custorm registry value del Delete one custom registry rename Set custom registry name home [browser] Open the homepage of registry with optional browser publish [|] 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 Set access - -o --otp [otpcode] Set otpcode - -dr --dry-run Set is dry run + -t --tag [tag] Add tag + -a --access 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 diff --git a/actions.js b/actions.js index ab454ef..cfe5dd2 100644 --- a/actions.js +++ b/actions.js @@ -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(); @@ -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; } @@ -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) { @@ -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], })); diff --git a/cli.js b/cli.js index afd5565..098994b 100755 --- a/cli.js +++ b/cli.js @@ -34,7 +34,8 @@ program .option('-u, --username ', 'Your user name for this registry') .option('-p, --password ', 'Your password for this registry') .option('-e, --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 ', '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 diff --git a/constants.js b/constants.js index 96c6007..dcfb6d4 100644 --- a/constants.js +++ b/constants.js @@ -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'); @@ -16,6 +17,7 @@ module.exports = { NPMRC, REGISTRIES, AUTH, + AUTH_TOKEN, ALWAYS_AUTH, REPOSITORY, REGISTRY, diff --git a/tests/cli.test.js b/tests/cli.test.js index 817371a..1097eaf 100644 --- a/tests/cli.test.js +++ b/tests/cli.test.js @@ -146,14 +146,20 @@ describe('nrm command which needs to add a custom registry', () => { it('login [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(); }); });