2021-10-31 19:47:48 -04:00

59 lines
1.3 KiB
TypeScript

import { resolve } from "path"
import { Vault } from "./models/vault"
import type { IAdapter } from "./adapters"
import { asyncMap } from "./util"
export type { Vault } from "./models/vault"
export { Category, FieldType } from "./models"
interface IOptions {
/**
* Path to `.opvault` directory
*/
path: string
/**
* Adapter used to interact with the file system and cryptography modules
*/
adapter?: IAdapter
}
/**
* OnePassword instance
*/
export class OnePassword {
readonly #path: string
readonly #adapter: IAdapter
constructor({ path, adapter = require("./adapters/node").adapter }: IOptions) {
this.#adapter = adapter
this.#path = path
}
/**
* @returns A list of names of profiles of the current vault.
*/
async getProfileNames() {
const [fs, path] = [this.#adapter.fs, this.#path]
const children = await fs.readdir(path)
const profiles: string[] = []
await asyncMap(children, async child => {
const fullPath = resolve(path, child)
if (
(await fs.isDirectory(fullPath)) &&
(await fs.exists(resolve(fullPath, "profile.js")))
) {
profiles.push(child)
}
})
return profiles
}
/**
* @returns A OnePassword Vault instance.
*/
async getProfile(profileName: string) {
return await Vault.of(this.#path, profileName, this.#adapter)
}
}