Skip to content

Commit

Permalink
refactor: add npm argument to otplease
Browse files Browse the repository at this point in the history
  • Loading branch information
jumoel committed Jun 29, 2022
1 parent d0af95f commit a5e86d5
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/auth/sso.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const login = async (npm, { creds, registry, scope }) => {
authType: ssoType,
}

const { token, sso } = await otplease(opts,
const { token, sso } = await otplease(npm, opts,
opts => profile.loginCouch(auth.username, auth.password, opts)
)

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/access.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class Access extends BaseCommand {

modifyPackage (pkg, opts, fn, requireScope = true) {
return this.getPackage(pkg, requireScope)
.then(pkgName => otplease(opts, opts => fn(pkgName, opts)))
.then(pkgName => otplease(this.npm, opts, opts => fn(pkgName, opts)))
}

async getPackage (name, requireScope) {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/deprecate.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Deprecate extends BaseCommand {
packument.versions[v].deprecated = msg
})

return otplease(this.npm.flatOptions, opts => fetch(uri, {
return otplease(this.npm, this.npm.flatOptions, opts => fetch(uri, {
...opts,
spec: p,
method: 'PUT',
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/dist-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class DistTag extends BaseCommand {
},
spec,
}
await otplease(reqOpts, reqOpts => regFetch(url, reqOpts))
await otplease(this.npm, reqOpts, reqOpts => regFetch(url, reqOpts))
this.npm.output(`+${t}: ${spec.name}@${version}`)
}

Expand All @@ -142,7 +142,7 @@ class DistTag extends BaseCommand {
method: 'DELETE',
spec,
}
await otplease(reqOpts, reqOpts => regFetch(url, reqOpts))
await otplease(this.npm, reqOpts, reqOpts => regFetch(url, reqOpts))
this.npm.output(`-${tag}: ${spec.name}@${version}`)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Hook extends BaseCommand {
static ignoreImplicitWorkspace = true

async exec (args) {
return otplease({
return otplease(this.npm, {
...this.npm.flatOptions,
}, (opts) => {
switch (args[0]) {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/org.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Org extends BaseCommand {
}

async exec ([cmd, orgname, username, role], cb) {
return otplease({
return otplease(this.npm, {
...this.npm.flatOptions,
}, opts => {
switch (cmd) {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class Owner extends BaseCommand {

const dataPath = `/${spec.escapedName}/-rev/${encodeURIComponent(data._rev)}`
try {
const res = await otplease(this.npm.flatOptions, opts => {
const res = await otplease(this.npm, this.npm.flatOptions, opts => {
return npmFetch.json(dataPath, {
...opts,
method: 'PUT',
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class Profile extends BaseCommand {

newUser[prop] = value

const result = await otplease(conf, conf => npmProfile.set(newUser, conf))
const result = await otplease(this.npm, conf, conf => npmProfile.set(newUser, conf))

if (this.npm.config.get('json')) {
this.npm.output(JSON.stringify({ [prop]: result[prop] }, null, 2))
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Publish extends BaseCommand {
log.notice('', `Publishing to ${outputRegistry}${dryRun ? ' (dry-run)' : ''}`)

if (!dryRun) {
await otplease(opts, opts => libpub(manifest, tarballData, opts))
await otplease(this.npm, opts, opts => libpub(manifest, tarballData, opts))
}

if (spec.type === 'directory' && !ignoreScripts) {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Team extends BaseCommand {
// XXX: "description" option to libnpmteam is used as a description of the
// team, but in npm's options, this is a boolean meaning "show the
// description in npm search output". Hence its being set to null here.
await otplease({ ...this.npm.flatOptions }, opts => {
await otplease(this.npm, { ...this.npm.flatOptions }, opts => {
entity = entity.replace(/^@/, '')
switch (cmd) {
case 'create': return this.create(entity, opts)
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Token extends BaseCommand {
})
await Promise.all(
toRemove.map(key => {
return otplease(conf, conf => {
return otplease(this.npm, conf, conf => {
return profile.removeToken(key, conf)
})
})
Expand All @@ -146,7 +146,7 @@ class Token extends BaseCommand {
const validCIDR = this.validateCIDRList(cidr)
log.info('token', 'creating')
return pulseTillDone.withPromise(
otplease(conf, conf => {
otplease(this.npm, conf, conf => {
return profile.createToken(password, readonly, validCIDR, conf)
})
)
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class Unpublish extends BaseCommand {
}

if (!dryRun) {
await otplease(opts, opts => libunpub(spec, opts))
await otplease(this.npm, opts, opts => libunpub(spec, opts))
}
if (!silent) {
this.npm.output(`- ${pkgName}${pkgVersion}`)
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/otplease.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
async function otplease (opts, fn) {
async function otplease (npm, opts, fn) {
try {
return await fn(opts)
} catch (err) {
Expand Down
20 changes: 14 additions & 6 deletions test/lib/utils/otplease.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const otplease = t.mock('../../../lib/utils/otplease.js', {

t.test('returns function results on success', async (t) => {
const fn = () => 'test string'
const result = await otplease({}, fn)
const result = await otplease(null, {}, fn)
t.equal('test string', result)
})

Expand All @@ -26,7 +26,7 @@ t.test('returns function results on otp success', async (t) => {
}
throw Object.assign(new Error('nope'), { code: 'EOTP' })
}
const result = await otplease({}, fn)
const result = await otplease(null, {}, fn)
t.equal('success', result)
})

Expand All @@ -51,7 +51,7 @@ t.test('prompts for otp for EOTP', async (t) => {
t.end()
}

await otplease({ some: 'prop' }, fn)
await otplease(null, { some: 'prop' }, fn)
})

t.test('prompts for otp for 401', async (t) => {
Expand All @@ -78,7 +78,7 @@ t.test('prompts for otp for 401', async (t) => {
t.end()
}

await otplease({ some: 'prop' }, fn)
await otplease(null, { some: 'prop' }, fn)
})

t.test('does not prompt for non-otp errors', async (t) => {
Expand All @@ -95,7 +95,11 @@ t.test('does not prompt for non-otp errors', async (t) => {
throw new Error('nope')
}

t.rejects(otplease({ some: 'prop' }, fn), { message: 'nope' }, 'rejects with the original error')
t.rejects(
otplease(null, { some: 'prop' }, fn),
{ message: 'nope' },
'rejects with the original error'
)
})

t.test('does not prompt if stdin or stdout is not a tty', async (t) => {
Expand All @@ -112,5 +116,9 @@ t.test('does not prompt if stdin or stdout is not a tty', async (t) => {
throw Object.assign(new Error('nope'), { code: 'EOTP' })
}

t.rejects(otplease({ some: 'prop' }, fn), { message: 'nope' }, 'rejects with the original error')
t.rejects(
otplease(null, { some: 'prop' }, fn),
{ message: 'nope' },
'rejects with the original error'
)
})

0 comments on commit a5e86d5

Please sign in to comment.