bcup-cli 1.3.1 → 1.3.3

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 (2) hide show
  1. package/dist/cli.js +114 -0
  2. package/package.json +2 -2
package/dist/cli.js ADDED
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env node
2
+ import arg from 'arg';
3
+ import flatten from 'lodash/flatten';
4
+ import uniqBy from 'lodash/uniqBy';
5
+ import { name, version, description, author, license, repository } from '../package.json';
6
+ import { Vault, FileDatasource, Credentials, init } from 'buttercup';
7
+ import { homedir } from 'os';
8
+ import clipboardy from 'clipboardy';
9
+ import chalk from 'chalk';
10
+ import promptSync from 'prompt-sync';
11
+ import history from 'prompt-sync-history';
12
+ export function cli(args) {
13
+ const options = parseArgumentsIntoOptions(args);
14
+ const debug = options.debug || false;
15
+ if (debug)
16
+ console.log(args);
17
+ const bcupFile = options.bcupPath || `${homedir()}/.bcup-cli/vault.bcup`;
18
+ const prompt = promptSync({
19
+ history: history() //open history file
20
+ });
21
+ console.log(`= ${name} v${version} (${description}) =`);
22
+ console.log(`author: ${author}`);
23
+ console.log(`license: ${license}`);
24
+ console.log(`repository: ${repository}\n`);
25
+ const password = prompt('Vault password: ', { echo: '*' });
26
+ if (password == undefined || password.length == 0)
27
+ return;
28
+ init();
29
+ const datasourceCredentials = Credentials.fromDatasource({
30
+ type: 'file',
31
+ path: bcupFile
32
+ }, password);
33
+ const fileDatasource = new FileDatasource(datasourceCredentials);
34
+ const credentials = Credentials.fromPassword(password);
35
+ let num;
36
+ let entries;
37
+ let titleSearch;
38
+ let promptText;
39
+ try {
40
+ fileDatasource
41
+ .load(credentials)
42
+ .then(({ history }) => Vault.createFromHistory(history))
43
+ .then(vault => {
44
+ do {
45
+ console.log("\n");
46
+ promptText = `Search text in folder or title. You can use RegExp (${titleSearch}): `;
47
+ titleSearch = prompt(promptText, titleSearch);
48
+ const re = new RegExp(titleSearch, 'i');
49
+ entries = vault.findEntriesByProperty('title', re);
50
+ const groups = vault.findGroupsByTitle(re);
51
+ const groupEntries = groups.map(group => group.getEntries());
52
+ entries = flatten(groupEntries).concat(entries);
53
+ if (debug)
54
+ console.log(entries);
55
+ entries = uniqBy(entries, entry => entry.id);
56
+ console.log('0 exit');
57
+ entries.forEach((entry, idx) => {
58
+ const title = emphSearch(String(entry.getProperty('title')), titleSearch);
59
+ const username = entry.getProperty('username');
60
+ const url = entry.getProperty('URL');
61
+ const group = emphSearch(entry.getGroup().getTitle(), titleSearch);
62
+ const sep = ` ${chalk.bold('|')} `;
63
+ console.log(`${idx + 1} ${group}${chalk.bold('->')}${title}${sep}url: ${url}${sep}username: ${username}`);
64
+ });
65
+ console.log("\n");
66
+ do {
67
+ num = prompt('Select entry to copy or press enter to search again: ');
68
+ if (debug)
69
+ console.log('num: "' + num + '"');
70
+ if (invalidSelection(num, entries.length)) {
71
+ console.log(`wrong selection (0-${entries.length})`);
72
+ }
73
+ if (num === '0')
74
+ return;
75
+ } while (invalidSelection(num, entries.length));
76
+ if (num === '')
77
+ continue;
78
+ const pass = entries[Number(num) - 1].getProperty('password');
79
+ clipboardy.writeSync(String(pass));
80
+ } while (true);
81
+ });
82
+ }
83
+ catch (error) {
84
+ console.error(error);
85
+ }
86
+ }
87
+ function invalidSelection(num, max) {
88
+ return num == undefined || (num.length > 0 && (!num.match(/\d+/) || Number(num) > max || Number(num) < 0));
89
+ }
90
+ function emphSearch(string, titleSearch) {
91
+ const re = new RegExp(titleSearch, 'ig');
92
+ let res;
93
+ let newString = string;
94
+ while ((res = re.exec(string)) !== null) {
95
+ newString = newString.slice(0, res.index) + chalk.red(res[0]) + newString.slice(res.index + res[0].length);
96
+ }
97
+ return newString;
98
+ }
99
+ function parseArgumentsIntoOptions(rawArgs) {
100
+ const args = arg({
101
+ '--debug': Boolean,
102
+ '--bcup-path': String,
103
+ '-d': '--debug',
104
+ '-b': '--bcup-path',
105
+ }, {
106
+ argv: rawArgs.slice(2),
107
+ });
108
+ return {
109
+ debug: args['--debug'] || false,
110
+ bcupPath: args['--bcup-path'],
111
+ };
112
+ }
113
+ cli(process.argv);
114
+ //# sourceMappingURL=cli.js.map
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "bcup-cli",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Lightweight Buttercup password manager cli",
5
5
  "type": "module",
6
6
  "bin": {
7
- "bcup-cli": "bin/cli.mjs"
7
+ "bcup-cli": "dist/cli.js"
8
8
  },
9
9
  "main": "index.js",
10
10
  "scripts": {