diff --git a/config.js b/config.js index c13a21c..5eb4058 100644 --- a/config.js +++ b/config.js @@ -3,11 +3,24 @@ * @return {object} */ module.exports = { - path: { - windows: './index.js', - darwin: './index.js', - ubuntu: './index.js', - testPlatform: './test.exe', - linux: './index.js' + windows: { + path: './index.js', + command: '' + }, + darwin: { + path: './test.sh', + command: 'sh test.sh' + }, + ubuntu: { + path: './test.sh', + command: 'sh test.sh' + }, + testPlatform: { + path: './test.exe', + command: 'sh test.sh' + }, + linux: { + path: './test.sh', + command: 'sh test.sh' } }; \ No newline at end of file diff --git a/index.js b/index.js index 7881041..98943e0 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const validateSafe = require('./lib/validate-safe'); const validatePlatform = require('./lib/validate-platform'); const validateExe = require('./lib/validate-exe'); const command = require('./lib/command'); +const execute = require('./lib/execute'); /** * Get user password @@ -14,26 +15,25 @@ const command = require('./lib/command'); * @param {string} safe * @return {string} */ -module.exports = (user, appID, safe) => { + +module.exports = (user, appID, safe) => { return new Promise((resolve, reject) => { - try { - // validate user input - validateUser(user); - // validate appID input - validateAppID(appID); - // validate safe input - validateSafe(safe); - // validate platform - validatePlatform(); - // validate executable file - validateExe(); - // set command string - const cmd = command(user, appID, safe); - // execute shell command with parameters - // validate result - resolve('password'); - } catch (error) { - reject(error); - } + // validate user input + validateUser(user); + // validate appID input + validateAppID(appID); + // validate safe input + validateSafe(safe); + // validate platform + validatePlatform(); + // validate executable file + validateExe(); + // set command string + const cmd = command(user, appID, safe); + // execute shell command + const password = execute(cmd); + // return result + resolve(password); }); -}; +}; + diff --git a/lib/command.js b/lib/command.js index f6884a6..5bfc54d 100644 --- a/lib/command.js +++ b/lib/command.js @@ -9,6 +9,7 @@ const config = require('../config'); */ module.exports = (user, appID, safe) => { const platform = process.platform; - const command = `${config.path[platform]} GetPassword /p AppDescs.AppID=${appID} /p Query="Safe=${safe};Folder=root;Object=${user}" /o Password`; + // const command = `${config.path[platform]} GetPassword /p AppDescs.AppID=${appID} /p Query="Safe=${safe};Folder=root;Object=${user}" /o Password`; + const command = config[platform].command; return command; }; \ No newline at end of file diff --git a/lib/execute.js b/lib/execute.js index e69de29..b31112a 100644 --- a/lib/execute.js +++ b/lib/execute.js @@ -0,0 +1,13 @@ +const execSync = require('child_process').execSync; + +/** + * Execute shell command + * @param {string} command + * @return {string} + */ +module.exports = command => { + return new Promise((resolve, reject) => { + const result = execSync(command); + resolve(result.toString()); + }); +}; diff --git a/lib/execute.test.js b/lib/execute.test.js deleted file mode 100644 index e69de29..0000000 diff --git a/lib/validate-exe.js b/lib/validate-exe.js index 3f4480e..05178b9 100644 --- a/lib/validate-exe.js +++ b/lib/validate-exe.js @@ -7,7 +7,7 @@ const config = require('../config'); module.exports = testPlatform => { try { const platform = testPlatform || process.platform; - const exe = config.path[platform]; + const exe = config[platform].path; const exists = fs.existsSync(exe); if (!exists) { throw new Error(`Executable ${exe} not exists`); diff --git a/lib/validate-platform.js b/lib/validate-platform.js index 191b188..e4e8930 100644 --- a/lib/validate-platform.js +++ b/lib/validate-platform.js @@ -6,7 +6,7 @@ const config = require('../config'); module.exports = testPlatform => { try { const platform = testPlatform || process.platform; - const valid = config.path[platform]; + const valid = config[platform]; if (!valid) { throw new Error(`Platform ${platform} is not supported`); } diff --git a/package.json b/package.json index 9fff397..2d54cce 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "@iiddoo/aim-password-retrieve", - "version": "2.1.1", + "version": "2.1.2", "description": "Retrieve password for user.", "main": "index.js", "scripts": { "test": "mocha --reporter spec", - "update": "npm version patch -m 'Version %s - add sweet badges'", + "update": "npm version patch -m 'Version %s - add badges'", "cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- -R spec test/*" }, "repository": { diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..4d0bd0c --- /dev/null +++ b/test.sh @@ -0,0 +1,2 @@ +sleep 1 +echo "password" \ No newline at end of file diff --git a/test/execute.test.js b/test/execute.test.js new file mode 100644 index 0000000..adf5c30 --- /dev/null +++ b/test/execute.test.js @@ -0,0 +1,17 @@ +const execute = require('../lib/execute'); +const { expect } = require('chai'); + +describe('Execute', ()=> { + it('Should throw error', done => { + expect(async ()=> { + try { + execute(); + } catch (error) {} + }).to.throw + done() + }); + it('Should return string', async () => { + const result = await execute('cd ./ && sh test.sh'); + expect(result).to.be.a('string'); + }); +}); diff --git a/test/index.test.js b/test/index.test.js index b342050..acef636 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -3,8 +3,15 @@ const { expect } = require('chai'); describe('Index', ()=> { it('Should throw error', done => { - expect(index()).to.throw - done() + expect(async () => { + try { + const pwd = await index(); + return pwd; + } catch (error) { + throw new Error(error); + } + }).to.throw + done(); }); it('Should return password', async () => { const pwd = await index('user', 'my_appID', 'my_safe'); diff --git a/test/validate-exe.test.js b/test/validate-exe.test.js index e1f3f99..54d1d6a 100644 --- a/test/validate-exe.test.js +++ b/test/validate-exe.test.js @@ -9,4 +9,3 @@ describe('Validate executable', ()=> { expect(validate()).to.equal(); }); }); - diff --git a/test/validate-safe.test.js b/test/validate-safe.test.js index 8149edd..bc2e31c 100644 --- a/test/validate-safe.test.js +++ b/test/validate-safe.test.js @@ -15,4 +15,3 @@ describe('Validate safe', ()=> { expect(validate('safe_name')).to.equal(); }); }); -