bcup-cli 1.3.0 → 1.3.1

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