@opencrvs/toolkit 2.0.0-rc.bd45274 → 2.0.0-rc.bd854e0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/README.md +15 -0
  2. package/build.sh +3 -2
  3. package/dist/application-config/index.js +24 -7
  4. package/dist/cli.js +3253 -47
  5. package/dist/commons/api/router.d.ts +21 -2
  6. package/dist/commons/conditionals/conditionals.d.ts +3 -3
  7. package/dist/commons/events/ActionConfig.d.ts +28 -2
  8. package/dist/commons/events/AdvancedSearchConfig.d.ts +12 -0
  9. package/dist/commons/events/CountryConfigQueryInput.d.ts +42 -0
  10. package/dist/commons/events/EventConfig.d.ts +2 -0
  11. package/dist/commons/events/EventIndex.d.ts +25 -0
  12. package/dist/commons/events/EventMetadata.d.ts +26 -0
  13. package/dist/commons/events/FieldConfig.d.ts +11 -0
  14. package/dist/commons/events/FieldType.d.ts +1 -0
  15. package/dist/commons/events/FieldTypeMapping.d.ts +1 -1
  16. package/dist/commons/events/WorkqueueConfig.d.ts +39 -0
  17. package/dist/commons/events/locations.d.ts +1 -0
  18. package/dist/commons/events/state/index.d.ts +8 -0
  19. package/dist/commons/events/state/utils.d.ts +9 -0
  20. package/dist/conditionals/index.js +10 -5
  21. package/dist/environment/custom-types.d.ts +33 -0
  22. package/dist/environment/custom-types.d.ts.map +1 -0
  23. package/dist/environment/custom-types.js +12 -0
  24. package/dist/environment/derived-variables.d.ts +134 -0
  25. package/dist/environment/derived-variables.d.ts.map +1 -0
  26. package/dist/environment/derived-variables.js +169 -0
  27. package/dist/environment/editor-questions.d.ts +4 -0
  28. package/dist/environment/editor-questions.d.ts.map +1 -0
  29. package/dist/environment/editor-questions.js +67 -0
  30. package/dist/environment/git.d.ts +5 -0
  31. package/dist/environment/git.d.ts.map +1 -0
  32. package/dist/environment/git.js +38 -0
  33. package/dist/environment/github.d.ts +26 -0
  34. package/dist/environment/github.d.ts.map +1 -0
  35. package/dist/environment/github.js +247 -0
  36. package/dist/environment/index.d.ts +5 -0
  37. package/dist/environment/index.d.ts.map +1 -0
  38. package/dist/environment/index.js +71 -0
  39. package/dist/environment/logger.d.ts +15 -0
  40. package/dist/environment/logger.d.ts.map +1 -0
  41. package/dist/environment/logger.js +25 -0
  42. package/dist/environment/manage-users.d.ts +11 -0
  43. package/dist/environment/manage-users.d.ts.map +1 -0
  44. package/dist/environment/manage-users.js +497 -0
  45. package/dist/environment/questions-helper.d.ts +15 -0
  46. package/dist/environment/questions-helper.d.ts.map +1 -0
  47. package/dist/environment/questions-helper.js +70 -0
  48. package/dist/environment/questions.d.ts +199 -0
  49. package/dist/environment/questions.d.ts.map +1 -0
  50. package/dist/environment/questions.js +412 -0
  51. package/dist/environment/setup-environment.d.ts +2 -0
  52. package/dist/environment/setup-environment.d.ts.map +1 -0
  53. package/dist/environment/setup-environment.js +794 -0
  54. package/dist/environment/ssh-keygen.d.ts +5 -0
  55. package/dist/environment/ssh-keygen.d.ts.map +1 -0
  56. package/dist/environment/ssh-keygen.js +21 -0
  57. package/dist/environment/swarm-to-k8s.d.ts +2 -0
  58. package/dist/environment/swarm-to-k8s.d.ts.map +1 -0
  59. package/dist/environment/swarm-to-k8s.js +173 -0
  60. package/dist/environment/templates.d.ts +27 -0
  61. package/dist/environment/templates.d.ts.map +1 -0
  62. package/dist/environment/templates.js +163 -0
  63. package/dist/environment/update-workflows.d.ts +2 -0
  64. package/dist/environment/update-workflows.d.ts.map +1 -0
  65. package/dist/environment/update-workflows.js +169 -0
  66. package/dist/environment/users.d.ts +2 -0
  67. package/dist/environment/users.d.ts.map +1 -0
  68. package/dist/environment/users.js +112 -0
  69. package/dist/environment/utils.d.ts +18 -0
  70. package/dist/environment/utils.d.ts.map +1 -0
  71. package/dist/environment/utils.js +100 -0
  72. package/dist/events/index.js +193 -68
  73. package/dist/migrations/v2.0/checkout-upstream-files.d.ts +1 -1
  74. package/dist/migrations/v2.0/checkout-upstream-files.d.ts.map +1 -1
  75. package/dist/migrations/v2.0/checkout-upstream-files.js +1 -1
  76. package/dist/migrations/v2.0/index.js +11 -6
  77. package/dist/notification/index.js +113 -9
  78. package/dist/scopes/index.d.ts +89 -1
  79. package/dist/scopes/index.js +10 -5
  80. package/dist/templates/charts-values/dependencies/values.override.yaml +4 -0
  81. package/dist/templates/charts-values/dependencies/values.yaml +83 -0
  82. package/dist/templates/charts-values/opencrvs-services/values.override.yaml +4 -0
  83. package/dist/templates/charts-values/opencrvs-services/values.yaml +81 -0
  84. package/dist/templates/charts-values/traefik/values.override.yaml +4 -0
  85. package/dist/templates/charts-values/traefik/values.yaml +115 -0
  86. package/dist/templates/inventory/inventory.template.yml +100 -0
  87. package/dist/templates/inventory/production.example.yml +77 -0
  88. package/dist/templates/inventory/qa.example.yml +48 -0
  89. package/opencrvs-toolkit-2.0.0-rc.bd854e0.tgz +0 -0
  90. package/package.json +15 -1
  91. package/tsconfig.tsbuildinfo +1 -1
  92. package/opencrvs-toolkit-2.0.0-rc.bd45274.tgz +0 -0
@@ -0,0 +1,497 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ *
7
+ * OpenCRVS is also distributed under the terms of the Civil Registration
8
+ * & Healthcare Disclaimer located at http://opencrvs.org/license.
9
+ *
10
+ * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
11
+ */
12
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ var desc = Object.getOwnPropertyDescriptor(m, k);
15
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
16
+ desc = { enumerable: true, get: function() { return m[k]; } };
17
+ }
18
+ Object.defineProperty(o, k2, desc);
19
+ }) : (function(o, m, k, k2) {
20
+ if (k2 === undefined) k2 = k;
21
+ o[k2] = m[k];
22
+ }));
23
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
24
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
25
+ }) : function(o, v) {
26
+ o["default"] = v;
27
+ });
28
+ var __importStar = (this && this.__importStar) || function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
42
+ });
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.manageUsers = manageUsers;
46
+ const os = __importStar(require("os"));
47
+ /* eslint-disable no-console */
48
+ const fs = __importStar(require("fs"));
49
+ const path = __importStar(require("path"));
50
+ function inquirerPrompt(name, options) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ const inquirer = yield import('@inquirer/prompts');
53
+ return inquirer[name](options);
54
+ });
55
+ }
56
+ const input = (options) => inquirerPrompt('input', options);
57
+ const select = (options) => inquirerPrompt('select', options);
58
+ const confirm = (options) => inquirerPrompt('confirm', options);
59
+ const editor = (options) => inquirerPrompt('editor', options);
60
+ const checkbox = (options) => inquirerPrompt('checkbox', options);
61
+ const logger_1 = require("./logger");
62
+ const utils_1 = require("./utils");
63
+ const templates_1 = require("./templates");
64
+ /**
65
+ * Updates users in Ansible inventory file
66
+ *
67
+ * @param {string} filePath - Path to the inventory file
68
+ * @param {User[]} users - Users array
69
+ */
70
+ function updateInventoryFile(filePath, users) {
71
+ var _a, _b;
72
+ const data = (0, utils_1.readYamlFile)(filePath);
73
+ if (!((_b = (_a = data === null || data === void 0 ? void 0 : data.all) === null || _a === void 0 ? void 0 : _a.vars) === null || _b === void 0 ? void 0 : _b.users)) {
74
+ throw Error(`'users' section not found within inventory file: ${filePath}`);
75
+ }
76
+ data.all.vars.users = users;
77
+ (0, utils_1.writeYamlFile)(filePath, data);
78
+ }
79
+ /**
80
+ * Parses the Ansible inventory file and extracts existing users
81
+ *
82
+ * @param {string} filePath - Path to the inventory file
83
+ * @returns {User[]} Array of existing users
84
+ */
85
+ function parseInventoryFile(filePath) {
86
+ try {
87
+ const data = (0, utils_1.readYamlFile)(filePath);
88
+ return (0, templates_1.getUsers)(data);
89
+ }
90
+ catch (_a) {
91
+ return [];
92
+ }
93
+ }
94
+ /**
95
+ * Gets the current system username
96
+ *
97
+ * @returns {string} Current OS username
98
+ */
99
+ function getCurrentUsername() {
100
+ return os.userInfo().username;
101
+ }
102
+ /**
103
+ * Reads SSH public keys from the current user's .ssh directory
104
+ *
105
+ * @returns {string[]} Array of SSH public keys found
106
+ */
107
+ function getCurrentUserSSHKeys() {
108
+ const homeDir = os.homedir();
109
+ const sshDir = path.join(homeDir, '.ssh');
110
+ const keys = [];
111
+ // Common public key filenames
112
+ const keyFiles = [
113
+ 'id_rsa.pub',
114
+ 'id_ecdsa.pub',
115
+ 'id_ed25519.pub',
116
+ 'id_dsa.pub'
117
+ ];
118
+ for (const keyFile of keyFiles) {
119
+ const keyPath = path.join(sshDir, keyFile);
120
+ try {
121
+ if (fs.existsSync(keyPath)) {
122
+ const keyContent = fs.readFileSync(keyPath, 'utf8').trim();
123
+ if (keyContent) {
124
+ keys.push(keyContent);
125
+ }
126
+ }
127
+ }
128
+ catch (error) {
129
+ // Silently skip if can't read the file
130
+ continue;
131
+ }
132
+ }
133
+ return keys;
134
+ }
135
+ function selectRole() {
136
+ return __awaiter(this, void 0, void 0, function* () {
137
+ return yield select({
138
+ message: 'Select user role:',
139
+ choices: [
140
+ { name: 'Admin (full access to OS and Kubernetes)', value: 'admin' },
141
+ { name: 'Operator (read-only OS, full Kubernetes)', value: 'operator' }
142
+ ]
143
+ });
144
+ });
145
+ }
146
+ function selectState() {
147
+ return __awaiter(this, void 0, void 0, function* () {
148
+ return yield select({
149
+ message: 'Select user state:',
150
+ choices: [
151
+ { name: 'Present (user is allowed to login)', value: 'present' },
152
+ { name: 'Absent (user account is disabled)', value: 'absent' }
153
+ ]
154
+ });
155
+ });
156
+ }
157
+ /**
158
+ * Asks if user wants to add current system user as the first user
159
+ *
160
+ * @returns {Promise<User | null>} User object if accepted, null if declined
161
+ */
162
+ function askToAddCurrentUser(existingUsers) {
163
+ return __awaiter(this, void 0, void 0, function* () {
164
+ const currentUsername = getCurrentUsername();
165
+ const currentKeys = getCurrentUserSSHKeys();
166
+ // Check if user already exists
167
+ if (existingUsers.find((u) => u.name === currentUsername)) {
168
+ (0, logger_1.info)(`ā„¹ļø User "${currentUsername}" already exists in inventory.`);
169
+ return null;
170
+ }
171
+ if (currentKeys.length === 0) {
172
+ // No SSH keys found for current user
173
+ return null;
174
+ }
175
+ const addCurrentUser = yield confirm({
176
+ message: `Add current user "${currentUsername}" with ${currentKeys.length} SSH key(s)?`,
177
+ default: true
178
+ });
179
+ if (!addCurrentUser) {
180
+ return null;
181
+ }
182
+ // Ask for role
183
+ const role = yield selectRole();
184
+ return {
185
+ name: currentUsername,
186
+ ssh_keys: currentKeys,
187
+ state: 'present',
188
+ role: role
189
+ };
190
+ });
191
+ }
192
+ /**
193
+ * Add a new user
194
+ */
195
+ function addNewUser(existingUsers) {
196
+ return __awaiter(this, void 0, void 0, function* () {
197
+ const name = yield input({
198
+ message: 'Enter username:',
199
+ validate: (value) => {
200
+ if (!value.trim()) {
201
+ return 'Username required';
202
+ }
203
+ if (existingUsers.find((u) => u.name === value.trim())) {
204
+ return `User "${value}" already exists`;
205
+ }
206
+ if (!/^[a-z_][a-z0-9_-]*[$]?$/.test(value.trim())) {
207
+ return 'Invalid username format (use lowercase, numbers, underscore, hyphen)';
208
+ }
209
+ return true;
210
+ }
211
+ });
212
+ const keysInput = yield editor({
213
+ message: 'Paste SSH public key(s) (one per line):',
214
+ default: ''
215
+ });
216
+ const ssh_keys = keysInput
217
+ .split('\n')
218
+ .map((key) => key.trim())
219
+ .filter((key) => key && !key.startsWith('#'));
220
+ if (ssh_keys.length === 0) {
221
+ (0, logger_1.warn)('āš ļø Warning: No SSH keys provided. User will not be able to login remotely.');
222
+ }
223
+ const role = yield selectRole();
224
+ // New users always active
225
+ const state = 'present';
226
+ return {
227
+ name: name.trim(),
228
+ ssh_keys,
229
+ state,
230
+ role
231
+ };
232
+ });
233
+ }
234
+ /**
235
+ * Display user summary
236
+ */
237
+ function displayUserSummary(users) {
238
+ if (users.length === 0) {
239
+ console.log('\nšŸ“‹ No users configured.\n');
240
+ return;
241
+ }
242
+ console.log('\n' + '='.repeat(70));
243
+ console.log(`šŸ“‹ User Configuration Summary - Total users: ${users.length}`);
244
+ console.log('='.repeat(70));
245
+ users.forEach((user, index) => {
246
+ const stateIcon = user.state === 'present' ? 'āœ…' : 'āŒ';
247
+ const roleIcon = user.role === 'admin' ? 'šŸ‘‘' : 'šŸ‘¤';
248
+ console.log(`${index + 1}. ${stateIcon} ${roleIcon} ${user.name.padEnd(20)} ` +
249
+ `Role: ${user.role.padEnd(10)} State: ${user.state.padEnd(10)} ` +
250
+ `SSH Keys: ${user.ssh_keys.length}`);
251
+ });
252
+ console.log('='.repeat(70) + '\n');
253
+ }
254
+ /**
255
+ * Edit an existing user with screen clearing for better UX
256
+ */
257
+ function editUser(user) {
258
+ return __awaiter(this, void 0, void 0, function* () {
259
+ const updatedUser = Object.assign({}, user);
260
+ let continueEditing = true;
261
+ // Function to clear screen and move cursor to top
262
+ const clearScreen = () => {
263
+ process.stdout.write('\x1Bc'); // Clear entire screen
264
+ // Alternative: process.stdout.write('\x1B[2J\x1B[0f'); // Clear screen + move cursor to top
265
+ };
266
+ // Function to display current user state
267
+ const displayUserState = () => {
268
+ clearScreen();
269
+ console.log('─'.repeat(70));
270
+ console.log(`šŸ“ Editing user: ${updatedUser.name}`);
271
+ console.log('─'.repeat(70));
272
+ console.log(`Current Role: ${updatedUser.role === 'admin' ? 'šŸ‘‘ admin' : 'šŸ‘¤ operator'}`);
273
+ console.log(`Current State: ${updatedUser.state === 'present' ? 'āœ… present' : 'āŒ absent'}`);
274
+ console.log(`Current SSH Keys: ${updatedUser.ssh_keys.length} key(s)`);
275
+ if (updatedUser.ssh_keys.length > 0) {
276
+ updatedUser.ssh_keys.forEach((key, idx) => {
277
+ const keyPreview = key.length > 50 ? key.substring(0, 47) + '...' : key;
278
+ console.log(` ${idx + 1}. ${keyPreview}`);
279
+ });
280
+ }
281
+ console.log('─'.repeat(70));
282
+ console.log(''); // Empty line for spacing
283
+ };
284
+ while (continueEditing) {
285
+ displayUserState();
286
+ const action = yield select({
287
+ message: 'What would you like to modify?',
288
+ choices: [
289
+ {
290
+ name: `Change role (current: ${updatedUser.role})`,
291
+ value: 'role'
292
+ },
293
+ {
294
+ name: `Change state (current: ${updatedUser.state})`,
295
+ value: 'state'
296
+ },
297
+ {
298
+ name: `Update SSH keys (current: ${updatedUser.ssh_keys.length} key(s))`,
299
+ value: 'keys'
300
+ },
301
+ {
302
+ name: 'šŸ’¾ Save changes and return',
303
+ value: 'save'
304
+ },
305
+ {
306
+ name: '🚫 Cancel and discard changes',
307
+ value: 'cancel'
308
+ }
309
+ ]
310
+ });
311
+ switch (action) {
312
+ case 'role':
313
+ const newRole = yield selectRole();
314
+ if (newRole !== updatedUser.role) {
315
+ updatedUser.role = newRole;
316
+ }
317
+ break;
318
+ case 'state':
319
+ const newState = yield selectState();
320
+ if (newState !== updatedUser.state) {
321
+ updatedUser.state = newState;
322
+ }
323
+ break;
324
+ case 'keys':
325
+ const keysInput = yield editor({
326
+ message: 'Update SSH public key(s) (one per line):',
327
+ default: updatedUser.ssh_keys.join('\n')
328
+ });
329
+ const newKeys = keysInput
330
+ .split('\n')
331
+ .map((key) => key.trim())
332
+ .filter((key) => key && !key.startsWith('#'));
333
+ if (newKeys.length === 0) {
334
+ clearScreen();
335
+ displayUserState();
336
+ const confirmRemoveKeys = yield confirm({
337
+ message: 'āš ļø Warning: All SSH keys will be removed. Continue with no SSH keys?',
338
+ default: false
339
+ });
340
+ if (confirmRemoveKeys) {
341
+ updatedUser.ssh_keys = [];
342
+ }
343
+ }
344
+ else {
345
+ updatedUser.ssh_keys = newKeys;
346
+ }
347
+ break;
348
+ case 'save':
349
+ const hasChanges = updatedUser.role !== user.role ||
350
+ updatedUser.state !== user.state ||
351
+ JSON.stringify(updatedUser.ssh_keys) !== JSON.stringify(user.ssh_keys);
352
+ clearScreen();
353
+ if (hasChanges) {
354
+ console.log('\nāœ… Changes saved for user:', updatedUser.name, '\n');
355
+ }
356
+ else {
357
+ console.log('\nā„¹ļø No changes made\n');
358
+ }
359
+ continueEditing = false;
360
+ break;
361
+ case 'cancel':
362
+ clearScreen();
363
+ const confirmCancel = yield confirm({
364
+ message: 'Discard all changes?',
365
+ default: false
366
+ });
367
+ if (confirmCancel) {
368
+ console.log('\n🚫 Changes discarded\n');
369
+ return user; // Return original user
370
+ }
371
+ break;
372
+ }
373
+ }
374
+ return updatedUser;
375
+ });
376
+ }
377
+ /**
378
+ * Main function to manage users
379
+ */
380
+ function manageUsers(inventoryPath) {
381
+ return __awaiter(this, void 0, void 0, function* () {
382
+ let users = fs.existsSync(inventoryPath)
383
+ ? parseInventoryFile(inventoryPath)
384
+ : [];
385
+ if (users.length > 0) {
386
+ displayUserSummary(users);
387
+ }
388
+ let continueManaging = true;
389
+ while (continueManaging) {
390
+ const action = yield select({
391
+ message: 'What would you like to do?',
392
+ choices: [
393
+ { name: 'āž• Add new user', value: 'add' },
394
+ { name: 'āœļø Edit existing user', value: 'edit' },
395
+ { name: 'šŸ—‘ļø Remove user', value: 'remove' },
396
+ { name: 'šŸ‘¤ Add current system user', value: 'add-current' },
397
+ { name: 'šŸ“‹ View all users', value: 'view' },
398
+ { name: 'šŸ’¾ Save and exit', value: 'save' },
399
+ { name: '🚪 Exit without saving', value: 'exit' }
400
+ ]
401
+ });
402
+ switch (action) {
403
+ case 'add':
404
+ const newUser = yield addNewUser(users);
405
+ if (newUser) {
406
+ users.push(newUser);
407
+ console.log(`āœ… User "${newUser.name}" added.\n`);
408
+ }
409
+ break;
410
+ case 'add-current':
411
+ const currentUser = yield askToAddCurrentUser(users);
412
+ if (currentUser) {
413
+ users.push(currentUser);
414
+ console.log(`āœ… Current user "${currentUser.name}" added.\n`);
415
+ }
416
+ break;
417
+ case 'edit':
418
+ if (users.length === 0) {
419
+ console.log('āš ļø No users to edit.\n');
420
+ break;
421
+ }
422
+ const userToEdit = yield select({
423
+ message: 'Select user to edit:',
424
+ choices: users.map((u) => ({
425
+ name: `${u.name} (${u.role}, ${u.state})`,
426
+ value: u.name
427
+ }))
428
+ });
429
+ const userIndex = users.findIndex((u) => u.name === userToEdit);
430
+ if (userIndex !== -1) {
431
+ users[userIndex] = yield editUser(users[userIndex]);
432
+ console.log(`āœ… User "${userToEdit}" updated.\n`);
433
+ }
434
+ break;
435
+ case 'remove':
436
+ if (users.length === 0) {
437
+ console.log('āš ļø No users to remove.\n');
438
+ break;
439
+ }
440
+ const usersToRemove = yield checkbox({
441
+ message: 'Select user(s) to remove:',
442
+ choices: users.map((u) => ({
443
+ name: `${u.name} (${u.role}, ${u.state})`,
444
+ value: u.name
445
+ }))
446
+ });
447
+ if (usersToRemove.length > 0) {
448
+ const confirmRemove = yield confirm({
449
+ message: `Remove ${usersToRemove.length} user(s)?`,
450
+ default: false
451
+ });
452
+ if (confirmRemove) {
453
+ users = users.filter((u) => !usersToRemove.includes(u.name));
454
+ console.log(`āœ… Removed ${usersToRemove.length} user(s).\n`);
455
+ }
456
+ }
457
+ break;
458
+ case 'view':
459
+ displayUserSummary(users);
460
+ break;
461
+ case 'save':
462
+ if (users.length === 0) {
463
+ (0, logger_1.info)('\nšŸ“‹ No users configured.\n');
464
+ continueManaging = false;
465
+ break;
466
+ }
467
+ displayUserSummary(users);
468
+ const confirmSave = yield confirm({
469
+ message: 'Save changes end exit?',
470
+ default: true
471
+ });
472
+ if (confirmSave && fs.existsSync(inventoryPath)) {
473
+ updateInventoryFile(inventoryPath, users);
474
+ console.log(`\nāœ… Changes saved to ${inventoryPath}\n`);
475
+ }
476
+ continueManaging = false;
477
+ break;
478
+ case 'exit':
479
+ if (users.length === 0) {
480
+ (0, logger_1.info)('\nšŸ“‹ No users configured.\n');
481
+ continueManaging = false;
482
+ break;
483
+ }
484
+ const confirmExit = yield confirm({
485
+ message: 'Exit without saving changes?',
486
+ default: false
487
+ });
488
+ if (confirmExit) {
489
+ console.log('šŸ‘‹ Exited without saving.\n');
490
+ continueManaging = false;
491
+ }
492
+ break;
493
+ }
494
+ }
495
+ return users;
496
+ });
497
+ }
@@ -0,0 +1,15 @@
1
+ import { Choice } from 'prompts';
2
+ export declare function getIndexFromChoices(choices: Choice[], value: string): number;
3
+ type SelectWithCustomOptions = {
4
+ message: string;
5
+ choices: readonly {
6
+ name: string;
7
+ value: string;
8
+ }[];
9
+ customLabel?: string;
10
+ customInputMessage?: string;
11
+ initial?: string;
12
+ };
13
+ export declare function selectWithCustom(config: SelectWithCustomOptions): Promise<string>;
14
+ export {};
15
+ //# sourceMappingURL=questions-helper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"questions-helper.d.ts","sourceRoot":"","sources":["../../src/environment/questions-helper.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAYhC,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,UASnE;AAED,KAAK,uBAAuB,GAAG;IAC7B,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,SAAS;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;IACnD,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,uBAAuB,GAC9B,OAAO,CAAC,MAAM,CAAC,CAkCjB"}
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ *
7
+ * OpenCRVS is also distributed under the terms of the Civil Registration
8
+ * & Healthcare Disclaimer located at http://opencrvs.org/license.
9
+ *
10
+ * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
11
+ */
12
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
+ return new (P || (P = Promise))(function (resolve, reject) {
15
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+ });
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.getIndexFromChoices = getIndexFromChoices;
23
+ exports.selectWithCustom = selectWithCustom;
24
+ function input(options) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ const inquirer = yield import('@inquirer/prompts');
27
+ return inquirer.input(options);
28
+ });
29
+ }
30
+ function select(options) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ const inquirer = yield import('@inquirer/prompts');
33
+ return inquirer.select(options);
34
+ });
35
+ }
36
+ function getIndexFromChoices(choices, value) {
37
+ let idx = 0;
38
+ for (const choice of choices) {
39
+ if (choice.value === value) {
40
+ return idx;
41
+ }
42
+ idx++;
43
+ }
44
+ return 0;
45
+ }
46
+ function selectWithCustom(config) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ const { message, choices, initial = undefined, customLabel = 'Other...', customInputMessage = 'Enter custom value:' } = config;
49
+ const CUSTOM = '__custom__';
50
+ const selected = yield select({
51
+ message,
52
+ choices: [
53
+ ...choices,
54
+ {
55
+ value: CUSTOM,
56
+ name: customLabel
57
+ }
58
+ ],
59
+ default: initial
60
+ });
61
+ if (selected === CUSTOM) {
62
+ const customValue = yield input({
63
+ message: customInputMessage,
64
+ validate: (value) => value.trim().length > 0 ? true : 'Value cannot be empty'
65
+ });
66
+ return customValue;
67
+ }
68
+ return selected;
69
+ });
70
+ }