diff --git a/lib/commands/token.js b/lib/commands/token.js index b248d6a789a1f..1fd0b6ad4d37e 100644 --- a/lib/commands/token.js +++ b/lib/commands/token.js @@ -73,15 +73,17 @@ class Token extends BaseCommand { const parseable = this.npm.config.get('parseable') log.info('token', 'getting list') const tokens = await paginate('/-/npm/v1/tokens', this.npm.flatOptions) + this.generateTokenIds(tokens, 6) if (json) { output.buffer(tokens) return } if (parseable) { - output.standard(['key', 'token', 'created', 'readonly', 'CIDR whitelist'].join('\t')) + output.standard(['name', 'key', 'token', 'created', 'readonly', 'CIDR whitelist'].join('\t')) tokens.forEach(token => { output.standard( [ + token.name, token.key, token.token, token.created, @@ -92,11 +94,11 @@ class Token extends BaseCommand { }) return } - this.generateTokenIds(tokens, 6) + const chalk = this.npm.chalk for (const token of tokens) { const created = String(token.created).slice(0, 10) - output.standard(`${chalk.blue('Token')} ${token.token}… with id ${chalk.cyan(token.id)} created ${created}`) + output.standard(`${chalk.blue('Token')} ${token.token}… with name ${chalk.magenta(token.name)} key ${chalk.cyan(token.key)} created ${created}`) if (token.cidr_whitelist) { output.standard(`with IP whitelist: ${chalk.green(token.cidr_whitelist.join(','))}`) } @@ -246,13 +248,13 @@ class Token extends BaseCommand { generateTokenIds (tokens, minLength) { for (const token of tokens) { - token.id = token.key + // token.id = token.key for (let ii = minLength; ii < token.key.length; ++ii) { const match = tokens.some( ot => ot !== token && ot.key.slice(0, ii) === token.key.slice(0, ii) ) if (!match) { - token.id = token.key.slice(0, ii) + token.key = token.key.slice(0, ii) break } } diff --git a/test/lib/commands/token.js b/test/lib/commands/token.js index 56ab906d921be..5a10c99c8b1ab 100644 --- a/test/lib/commands/token.js +++ b/test/lib/commands/token.js @@ -13,6 +13,7 @@ const tokens = [ { key: 'abcd1234abcd1234', token: 'efgh5678efgh5678', + name: 'abcd001', cidr_whitelist: null, readonly: false, created: now, @@ -21,6 +22,7 @@ const tokens = [ { key: 'abcd1256', token: 'hgfe8765', + name: 'abcd002', cidr_whitelist: ['192.168.1.1/32'], readonly: true, created: now, @@ -63,9 +65,9 @@ t.test('token list', async t => { registry.getTokens(tokens) await npm.exec('token', []) t.strictSame(outputs, [ - `Token efgh5678efgh5678… with id abcd123 created ${now.slice(0, 10)}`, + `Token efgh5678efgh5678… with name abcd001 key abcd123 created ${now.slice(0, 10)}`, '', - `Token hgfe8765… with id abcd125 created ${now.slice(0, 10)}`, + `Token hgfe8765… with name abcd002 key abcd125 created ${now.slice(0, 10)}`, 'with IP whitelist: 192.168.1.1/32', '', ]) @@ -86,7 +88,9 @@ t.test('token list json output', async t => { registry.getTokens(tokens) await npm.exec('token', ['list']) const parsed = JSON.parse(joinedOutput()) - t.match(parsed, tokens, 'prints the json parsed tokens') + t.match(parsed, tokens.map(token => { + return { ...token, key: token.key.slice(0, 6) } + }), 'prints the json parsed tokens') }) t.test('token list parseable output', async t => { @@ -104,9 +108,9 @@ t.test('token list parseable output', async t => { registry.getTokens(tokens) await npm.exec('token', []) t.strictSame(outputs, [ - 'key\ttoken\tcreated\treadonly\tCIDR whitelist', - `abcd1234abcd1234\tefgh5678efgh5678\t${now}\tfalse\t`, - `abcd1256\thgfe8765\t${now}\ttrue\t192.168.1.1/32`, + 'name\tkey\ttoken\tcreated\treadonly\tCIDR whitelist', + `abcd001\tabcd123\tefgh5678efgh5678\t${now}\tfalse\t`, + `abcd002\tabcd125\thgfe8765\t${now}\ttrue\t192.168.1.1/32`, ]) })