Files
opvault.js/packages/opvault.js/src/index.ts
2021-11-05 03:17:18 -04:00

65 lines
1.5 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 type { Item } from "./models/item"
export type { Attachment, AttachmentMetadata } from "./models/attachment"
export type { ItemField, ItemSection } from "./types"
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 = process.browser ? null : require("./adapters/node").nodeAdapter,
}: 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)
}
}