favacli 0.0.7 → 0.0.9

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.
@@ -19,12 +19,12 @@ class BaseCommand extends Command {
19
19
  async execute() {
20
20
  const { lockedRepresentationString, settings } = await init();
21
21
  this.settings = settings;
22
- if (lockedRepresentationString) {
23
- this.twoFaLib = await loadVault(lockedRepresentationString, this.verbose);
22
+ if (lockedRepresentationString && this.requireTwoFaLib) {
23
+ this.twoFaLib = await loadVault(lockedRepresentationString, settings, this.verbose);
24
24
  }
25
25
  else {
26
26
  if (this.requireTwoFaLib) {
27
- throw new Error('TwoFaLib is required');
27
+ throw new Error('No vault loaded, was it created?');
28
28
  }
29
29
  }
30
30
  const json = await this.exec();
@@ -17,7 +17,7 @@ class VaultCreateCommand extends BaseCommand {
17
17
  description: 'Create a new encrypted vault',
18
18
  details: `
19
19
  This command creates a new encrypted vault file to store your 2FA entries.
20
-
20
+
21
21
  You will be prompted to enter a passphrase that will be used to encrypt the vault.
22
22
  The passphrase will be securely stored in your system's keychain.
23
23
  `,
@@ -30,11 +30,11 @@ class VaultCreateCommand extends BaseCommand {
30
30
  }));
31
31
  const { twoFaLib } = await twoFaLibVaultCreationUtils.createNewTwoFaLibVault(passphrase);
32
32
  twoFaLib.addEventListener(TwoFaLibEvent.Changed, (ev) => {
33
- return fs.writeFile('vault.json', ev.detail.newLockedRepresentationString);
33
+ return fs.writeFile(this.settings.vaultLocation, ev.detail.newLockedRepresentationString);
34
34
  });
35
35
  await Promise.all([
36
36
  twoFaLib.forceSave(),
37
- keytar.setPassword('2falib', 'vault-passphrase', passphrase),
37
+ keytar.setPassword('favacli', 'vault-passphrase', passphrase),
38
38
  ]);
39
39
  return { success: true };
40
40
  }
@@ -0,0 +1,14 @@
1
+ import BaseCommand from '../../BaseCommand.mjs';
2
+ declare class VaultDeleteCommand extends BaseCommand {
3
+ static paths: string[][];
4
+ requireTwoFaLib: boolean;
5
+ static usage: import("clipanion").Usage;
6
+ exec(): Promise<{
7
+ success: boolean;
8
+ cancelled: boolean;
9
+ } | {
10
+ success: boolean;
11
+ cancelled?: undefined;
12
+ }>;
13
+ }
14
+ export default VaultDeleteCommand;
@@ -0,0 +1,33 @@
1
+ import fs from 'node:fs/promises';
2
+ import { confirm } from '@inquirer/prompts';
3
+ import BaseCommand from '../../BaseCommand.mjs';
4
+ class VaultDeleteCommand extends BaseCommand {
5
+ constructor() {
6
+ super(...arguments);
7
+ this.requireTwoFaLib = false;
8
+ }
9
+ static { this.paths = [['vault', 'delete']]; }
10
+ static { this.usage = BaseCommand.Usage({
11
+ category: 'Vault',
12
+ description: 'Delete the current vault',
13
+ details: `
14
+ This command deletes the current vault file.
15
+ You will be asked to confirm before deletion.
16
+ `,
17
+ examples: [['Delete the current vault', 'vault delete']],
18
+ }); }
19
+ async exec() {
20
+ const shouldDelete = await confirm({
21
+ message: 'Are you sure you want to delete the vault? This action cannot be undone.',
22
+ default: false,
23
+ });
24
+ if (!shouldDelete) {
25
+ this.output('Vault deletion cancelled.\n');
26
+ return { success: false, cancelled: true };
27
+ }
28
+ await fs.rm(this.settings.vaultLocation);
29
+ this.output('Vault deleted successfully.\n');
30
+ return { success: true };
31
+ }
32
+ }
33
+ export default VaultDeleteCommand;
package/build/main.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { Cli, Builtins } from 'clipanion';
3
3
  import VaultCreateCommand from './commands/vault/create.mjs';
4
+ import VaultDeleteCommand from './commands/vault/delete.mjs';
4
5
  import EntriesAddCommand from './commands/entries/add.mjs';
5
6
  import EntriesListCommand from './commands/entries/list.mjs';
6
7
  import EntriesSearchCommand from './commands/entries/search.mjs';
@@ -11,21 +12,18 @@ const nodeRuntimeMajorVersion = parseInt(process.version.split('.')[0]);
11
12
  if (nodeRuntimeMajorVersion < 20) {
12
13
  throw new Error('Node.js version must be 20 or higher');
13
14
  }
14
- const [...args] = process.argv;
15
+ const [, , ...args] = process.argv;
15
16
  const cli = new Cli({
16
17
  binaryLabel: 'FavaCli',
17
18
  binaryName: `favacli`,
18
- binaryVersion: '0.0.7',
19
+ binaryVersion: '0.0.8',
19
20
  });
20
21
  cli.register(VaultCreateCommand);
22
+ cli.register(VaultDeleteCommand);
21
23
  cli.register(EntriesAddCommand);
22
24
  cli.register(EntriesListCommand);
23
25
  cli.register(EntriesSearchCommand);
24
26
  cli.register(SyncSetServerUrlCommand);
25
27
  cli.register(SyncConnect);
26
28
  cli.register(Builtins.HelpCommand);
27
- if (args[0].endsWith('node')) {
28
- args.splice(0, 1);
29
- args.splice(0, 1);
30
- }
31
29
  void cli.runExit(args);
@@ -1,8 +1,9 @@
1
1
  import type { LockedRepresentationString } from 'favalib';
2
- import type { EmptyObject } from 'type-fest';
3
- export type Settings = EmptyObject;
2
+ export interface Settings {
3
+ vaultLocation: string;
4
+ }
4
5
  declare const init: () => Promise<{
5
- settings: EmptyObject;
6
+ settings: Settings;
6
7
  lockedRepresentationString: LockedRepresentationString;
7
8
  }>;
8
9
  export default init;
@@ -1,5 +1,9 @@
1
1
  import fs from 'node:fs/promises';
2
- const defaultSettings = {};
2
+ import path from 'node:path';
3
+ import envPaths from 'env-paths';
4
+ const getDefaultSettings = () => ({
5
+ vaultLocation: path.join(envPaths('favacli').data, 'vault.json'),
6
+ });
3
7
  const readFile = async (path) => {
4
8
  try {
5
9
  return (await fs.readFile(path)).toString();
@@ -18,13 +22,21 @@ const readJSONFile = async (path) => {
18
22
  }
19
23
  return JSON.parse(contents);
20
24
  };
25
+ const ensureDirectoriesExists = async (...paths) => {
26
+ for (const p of paths) {
27
+ const directory = path.dirname(p);
28
+ await fs.mkdir(directory, { recursive: true });
29
+ }
30
+ };
21
31
  const init = async () => {
22
- let settings = await readJSONFile('settings.json');
32
+ const settingsLocation = path.join(envPaths('favacli').config, 'settings.json');
33
+ let settings = await readJSONFile(settingsLocation);
23
34
  if (!settings) {
24
- settings = defaultSettings;
25
- await fs.writeFile('settings.json', JSON.stringify(settings, null, 2));
35
+ settings = getDefaultSettings();
36
+ await ensureDirectoriesExists(settingsLocation, settings.vaultLocation);
37
+ await fs.writeFile(settingsLocation, JSON.stringify(settings, null, 2));
26
38
  }
27
- const lockedRepresentationString = (await readFile('vault.json'));
39
+ const lockedRepresentationString = (await readFile(settings.vaultLocation));
28
40
  return { settings, lockedRepresentationString };
29
41
  };
30
42
  export default init;
@@ -1,3 +1,4 @@
1
1
  import { type LockedRepresentationString } from 'favalib';
2
- declare const loadVault: (vaultData: LockedRepresentationString, verbose?: boolean) => Promise<import("favalib").TwoFaLib>;
2
+ import { Settings } from './init.mjs';
3
+ declare const loadVault: (vaultData: LockedRepresentationString, settings: Settings, verbose?: boolean) => Promise<import("favalib").TwoFaLib>;
3
4
  export default loadVault;
@@ -4,11 +4,11 @@ import { getTwoFaLibVaultCreationUtils, TwoFaLibEvent, } from 'favalib';
4
4
  import NodeCryptoProvider from 'favalib/cryptoProviders/node';
5
5
  const cryptoLib = new NodeCryptoProvider();
6
6
  const twoFaLibVaultCreationUtils = getTwoFaLibVaultCreationUtils(cryptoLib, 'cli', ['cli']);
7
- const loadVault = async (vaultData, verbose = false) => {
8
- const passphrase = (await keytar.getPassword('2falib', 'vault-passphrase'));
7
+ const loadVault = async (vaultData, settings, verbose = false) => {
8
+ const passphrase = (await keytar.getPassword('favacli', 'vault-passphrase'));
9
9
  const twoFaLib = await twoFaLibVaultCreationUtils.loadTwoFaLibFromLockedRepesentation(vaultData, passphrase);
10
10
  twoFaLib.addEventListener(TwoFaLibEvent.Changed, (ev) => {
11
- return fs.writeFile('vault.json', ev.detail.newLockedRepresentationString);
11
+ return fs.writeFile(settings.vaultLocation, ev.detail.newLockedRepresentationString);
12
12
  });
13
13
  twoFaLib.addEventListener(TwoFaLibEvent.Log, (ev) => {
14
14
  if (ev.detail.severity !== 'info' || verbose) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "favacli",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "",
@@ -9,7 +9,8 @@
9
9
  "@inquirer/prompts": "^7.0.1",
10
10
  "bufferutil": "^4.0.8",
11
11
  "clipanion": "^4.0.0-rc.4",
12
- "favalib": "^0.0.1",
12
+ "env-paths": "^3.0.0",
13
+ "favalib": "^0.0.2",
13
14
  "keytar": "^7.9.0",
14
15
  "node-loader": "^2.0.0",
15
16
  "ts-loader": "^9.5.1",