UNPKG

3.59 kBJavaScriptView Raw
1"use strict";
2
3// Utils.
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.syncFile = syncFile;
9exports.exec = exec;
10
11var _utils = require('./utils');
12
13var utils = _interopRequireWildcard(_utils);
14
15var _fs = require('fs');
16
17var _fs2 = _interopRequireDefault(_fs);
18
19var _prompt = require('prompt');
20
21var _prompt2 = _interopRequireDefault(_prompt);
22
23function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
25function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
26
27// Sync file function.
28
29
30// FS.
31function syncFile(program) {
32 // Read params.
33 var passwordFile = program.args[0];
34 var realm = program.args[1];
35 var username = program.args[2];
36
37 // Encode file data.
38 var writeData = utils.encode(program);
39
40 // Collectors.
41 var found = false;
42 var newLines = [];
43
44 // Not creating file.
45 if (!program.create) {
46 // Check if file exists.
47 if (!_fs2.default.existsSync(passwordFile)) {
48 console.error('Cannot modify file ' + passwordFile + '; use \'-c\' to create it.');
49 return;
50 }
51
52 // Read lines.
53 var lines = _fs2.default.readFileSync(passwordFile, 'UTF-8').split("\n");
54
55 // Loop lines.
56 lines.forEach(function (line) {
57 if (line.indexOf(username + ':' + realm + ':') === 0) {
58 found = true;
59 newLines.push(writeData);
60 console.log('Changing password for user ' + username + ' in realm ' + realm + '.');
61 } else {
62 newLines.push(line);
63 }
64 });
65 }
66
67 // Adding user to existing file.
68 if (!found) {
69 newLines.push(writeData);
70 console.log('Adding password for user ' + username + ' in realm ' + realm + '.');
71 }
72
73 // Write data.
74 _fs2.default.writeFileSync(passwordFile, newLines.join("\n") + "\n", 'UTF-8');
75}
76
77// Read password.
78
79
80// Prompt module.
81function readPassword(program) {
82 _prompt2.default.message = "";
83 _prompt2.default.delimiter = "";
84
85 var passportOption = [{ name: 'password', description: 'New password:', hidden: true }];
86 var rePassportOption = [{ name: 'rePassword', description: 'Re-type new password:', hidden: true }];
87
88 // Try to read password.
89 _prompt2.default.get(passportOption, function (err, result) {
90 if (!err) {
91 (function () {
92 var password = result.password;
93 setTimeout(function () {
94 _prompt2.default.get(rePassportOption, function (err, result) {
95 if (!err && password == result.rePassword) {
96 program.args.push(password);
97
98 try {
99 syncFile(program);
100 } catch (err) {
101 console.error(err.message);
102 }
103 } else {
104 console.error("\nPassword verification error.");
105 }
106 });
107 }, 50);
108 })();
109 } else {
110 console.error("\nPassword verification error.");
111 }
112 });
113}
114
115// Process command.
116function exec(program) {
117 if (program.args.length === 3) {
118 readPassword(program);
119 } else {
120 program.help();
121 }
122}
\No newline at end of file