Skip to content

Commit

Permalink
feat(otplease): Expose getOneTimePassword() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Jul 16, 2019
1 parent cf56622 commit 44b9f70
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
17 changes: 17 additions & 0 deletions core/otplease/__tests__/otplease.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ describe("@lerna/otplease", () => {

expect.hasAssertions();
});

describe("getOneTimePassword()", () => {
it("defaults message argument", async () => {
await otplease.getOneTimePassword();

expect(prompt.input).toHaveBeenCalledWith(
"This operation requires a one-time password:",
expect.any(Object)
);
});

it("accepts custom message", async () => {
await otplease.getOneTimePassword("foo bar");

expect(prompt.input).toHaveBeenCalledWith("foo bar", expect.any(Object));
});
});
});

function makeTestCallback(otp, result) {
Expand Down
5 changes: 3 additions & 2 deletions core/otplease/otplease.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const semaphore = {
};

module.exports = otplease;
module.exports.getOneTimePassword = getOneTimePassword;

function otplease(fn, _opts, otpCache) {
// NOTE: do not use 'otpCache' as a figgy-pudding provider directly as the
Expand Down Expand Up @@ -91,9 +92,9 @@ function attempt(fn, opts, otpCache) {
});
}

function getOneTimePassword() {
function getOneTimePassword(message = "This operation requires a one-time password:") {
// Logic taken from npm internals: https://git.io/fNoMe
return prompt.input("This operation requires a one-time password:", {
return prompt.input(message, {
filter: otp => otp.replace(/\s+/g, ""),
validate: otp =>
(otp && /^[\d ]+$|^[A-Fa-f0-9]{64,64}$/.test(otp)) ||
Expand Down

0 comments on commit 44b9f70

Please sign in to comment.