From 6aa8933f20baf3d95839897f066708ca51494754 Mon Sep 17 00:00:00 2001 From: Rotzbua Date: Tue, 28 May 2024 13:38:15 +0200 Subject: [PATCH] feat: migrate to `read@v3` BREAKING: `read` requires node >=14.17 --- lib/commands/hash.js | 17 +++++++++-------- lib/commands/login.js | 6 +++--- lib/prompt.js | 4 ++-- package.json | 4 ++-- test/lib/commands/hash_spec.js | 14 +++++++------- test/lib/commands/login_spec.js | 6 +++--- 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/commands/hash.js b/lib/commands/hash.js index 02273c3..99a24cb 100644 --- a/lib/commands/hash.js +++ b/lib/commands/hash.js @@ -13,8 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ - -var prompt = require("../prompt"); +const prompt = require("../prompt"); try { bcrypt = require('bcrypt'); } catch(e) { bcrypt = require('bcryptjs'); } @@ -23,14 +22,16 @@ function command(argv,result) { console.warn("hash-pw command does not support json format output"); } return new Promise(resolve => { - prompt.read({prompt:"Password:",silent: true},function(err, password) { - if (password) { - result.log(bcrypt.hashSync(password, 8)); - } - resolve(); - }); + prompt.read({prompt: "Password:", silent: true}) + .then(password => { + if(password) { + result.log(bcrypt.hashSync(password, 8)); + } + }) + .finally(() => resolve()); }); } + command.alias = "hash-pw"; command.usage = command.alias; command.description = "Creates a password hash suitable for use with adminAuth or httpNodeAuth"; diff --git a/lib/commands/login.js b/lib/commands/login.js index d9aca19..06365ab 100644 --- a/lib/commands/login.js +++ b/lib/commands/login.js @@ -16,7 +16,7 @@ var request = require("../request"); var config = require("../config"); -var prompt = require("../prompt"); +const prompt = require("../prompt"); function command(argv,result) { config.tokens(null); @@ -24,8 +24,8 @@ function command(argv,result) { return new Promise((resolve,reject) => { if (resp.type) { if (resp.type == "credentials") { - prompt.read({prompt:"Username:"},function(err, username) { - prompt.read({prompt:"Password:",silent: true},function(err, password) { + prompt.read({prompt:"Username:"}).then(function(username) { + prompt.read({prompt:"Password:",silent: true}).then(function(password) { request.request('/auth/token', { method: "POST", data: { diff --git a/lib/prompt.js b/lib/prompt.js index c6cb65b..5e70ad8 100644 --- a/lib/prompt.js +++ b/lib/prompt.js @@ -14,11 +14,11 @@ * limitations under the License. **/ -var read = require("read"); +const {read} = require("read") // This exists purely to provide a place to hook in a unit test mock of the // read module module.exports = { - read: read + read: read, }; diff --git a/package.json b/package.json index b207590..7753d58 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ }, "main": "lib/index.js", "engines": { - "node": ">=14" + "node": ">=14.17" }, "contributors": [ { @@ -35,7 +35,7 @@ "enquirer": "^2.3.6", "minimist": "^1.2.8", "mustache": "^4.2.0", - "read": "^1.0.7" + "read": "^3.0.1" }, "devDependencies": { "mocha": "^10.2.0", diff --git a/test/lib/commands/hash_spec.js b/test/lib/commands/hash_spec.js index d5a0d88..b02f45e 100644 --- a/test/lib/commands/hash_spec.js +++ b/test/lib/commands/hash_spec.js @@ -16,7 +16,7 @@ var command = require("../../../lib/commands/hash"); -var prompt = require("../../../lib/prompt"); +const prompt = require("../../../lib/prompt"); var should = require("should"); var sinon = require("sinon"); @@ -34,8 +34,8 @@ describe("commands/hash-pw", function() { prompt.read.restore(); }); it('generates a bcrypt hash of provided password',function(done) { - sinon.stub(prompt,"read").callsFake(function(opts,callback) { - callback(null,"a-test-password"); + sinon.stub(prompt, "read").callsFake(function(opts) { + return Promise.resolve("a-test-password"); }); command({},result).then(function() { @@ -48,8 +48,8 @@ describe("commands/hash-pw", function() { }); }); it('ignores blank password',function(done) { - sinon.stub(prompt,"read").callsFake(function(opts,callback) { - callback(null,""); + sinon.stub(prompt, "read").callsFake(function(opts) { + return Promise.resolve(""); }); command({},result).then(function() { @@ -58,8 +58,8 @@ describe("commands/hash-pw", function() { }); }); it('ignores null password',function(done) { - sinon.stub(prompt,"read").callsFake(function(opts,callback) { - callback(null,null); + sinon.stub(prompt, "read").callsFake(function(opts) { + return Promise.resolve(null); }); command({},result).then(function() { diff --git a/test/lib/commands/login_spec.js b/test/lib/commands/login_spec.js index dac0379..ebf86eb 100644 --- a/test/lib/commands/login_spec.js +++ b/test/lib/commands/login_spec.js @@ -28,11 +28,11 @@ var result = require("./result_helper"); describe("commands/list", function() { beforeEach(function() { sinon.stub(config,"tokens").callsFake(function(token) {}); - sinon.stub(prompt,"read").callsFake(function(opts,callback) { + sinon.stub(prompt, "read").callsFake(function(opts) { if (/Username/.test(opts.prompt)) { - callback(null,"username"); + return Promise.resolve("username"); } else if (/Password/.test(opts.prompt)) { - callback(null,"password"); + return Promise.resolve("password"); } }); });