@zendesk/zcli-core
Version:
ZCLI core libraries and services
63 lines (62 loc) • 2.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const plugins_1 = require("@oclif/plugin-plugins/lib/plugins");
const path = require("path");
const os_1 = require("os");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageJson = require('../../package.json');
class SecureStore {
constructor() {
this.serviceName = 'zcli';
this.pluginsPath = path.join((0, os_1.homedir)(), '/.local/share/zcli');
this.packageName = 'keytar';
this.keytarPath = path.join(this.pluginsPath, 'node_modules', this.packageName);
this.keytar = undefined;
}
async installKeytar() {
const packageTag = `${this.packageName}@${packageJson.optionalDependencies.keytar}`;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const plugins = new plugins_1.default({ dataDir: this.pluginsPath, cacheDir: this.pluginsPath });
try {
await plugins.createPJSON();
await plugins.yarn.exec(['add', '--force', packageTag], { cwd: this.pluginsPath, verbose: false });
}
catch (error) {
// TODO: add telemetry so we know when this fails
}
}
async loadKeytar() {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
this.keytar = require(this.keytarPath);
}
catch (error) {
await this.installKeytar();
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
this.keytar = require(this.keytarPath);
}
catch (error) {
// TODO: add telemetry so we know when this fails
}
}
return this.keytar;
}
setSecret(account, secret) {
var _a;
return (_a = this.keytar) === null || _a === void 0 ? void 0 : _a.setPassword(this.serviceName, account, secret);
}
getSecret(account) {
var _a;
return (_a = this.keytar) === null || _a === void 0 ? void 0 : _a.getPassword(this.serviceName, account);
}
deleteSecret(account) {
var _a;
return (_a = this.keytar) === null || _a === void 0 ? void 0 : _a.deletePassword(this.serviceName, account);
}
getAllCredentials() {
var _a;
return (_a = this.keytar) === null || _a === void 0 ? void 0 : _a.findCredentials(this.serviceName);
}
}
exports.default = SecureStore;