appium-lg-webos-driver
Version:
LG WebOS support for Appium
200 lines • 7.1 kB
JavaScript
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _BaseValueWrapper_value;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValueBox = exports.DEFAULT_SUFFIX = exports.BaseValueWrapper = void 0;
const env_paths_1 = __importDefault(require("env-paths"));
const promises_1 = require("node:fs/promises");
const slugify_1 = __importDefault(require("slugify"));
const node_path_1 = __importDefault(require("node:path"));
/**
* A value which stores its value in a file on disk
*
* This class is not intended to be instantiated directly
*
*/
class BaseValueWrapper {
/**
* Slugifies the name
* @param name Name of value
* @param looker Reader fn
* @param putter Writer fn
* @param encoding Defaults to `utf8`
*/
constructor(name, looker, putter, encoding = 'utf8') {
this.name = name;
this.encoding = encoding;
/**
* Underlying value
*/
_BaseValueWrapper_value.set(this, void 0);
this.id = (0, slugify_1.default)(name, { lower: true });
Object.defineProperties(this, {
looker: { value: looker },
putter: { value: putter },
value: {
get() {
return __classPrivateFieldGet(this, _BaseValueWrapper_value, "f");
},
enumerable: true,
},
});
}
/**
* {@inheritdoc IValueWrapper.read}
*/
async look() {
__classPrivateFieldSet(this, _BaseValueWrapper_value, await this.looker(this), "f");
return __classPrivateFieldGet(this, _BaseValueWrapper_value, "f");
}
/**
* {@inheritdoc IValueWrapper.write}
*/
async put(value) {
await this.putter(this, value);
__classPrivateFieldSet(this, _BaseValueWrapper_value, value, "f");
}
}
exports.BaseValueWrapper = BaseValueWrapper;
_BaseValueWrapper_value = new WeakMap();
/**
* @see {@linkcode ValueBoxOpts}
*/
exports.DEFAULT_SUFFIX = 'valuebox';
/**
* Main entry point for use of this module
*
* Manages multiple values.
*/
class ValueBox {
constructor(name, { dir, suffix = exports.DEFAULT_SUFFIX, defaultCtor: ctor = BaseValueWrapper } = {}) {
this.name = name;
/**
* Factory function for creating new {@linkcode ValueWrapper}s}
*/
this.wrapperIds = new Set();
this.containerId = (0, slugify_1.default)(name, { lower: true });
this.ctor = ctor;
this.dir = dir ?? node_path_1.default.join((0, env_paths_1.default)(this.containerId).data, suffix);
}
/**
* "mkdirp"'s the value directory and writes it to disk
* @param value ValueWrapper to write
* @param value Value to write to the value
*/
async put(wrapper, value) {
await this.init();
await (0, promises_1.writeFile)(node_path_1.default.join(this.dir, wrapper.id), value, wrapper.encoding);
}
/**
* "mkdirp"'s the value directory
*/
async init() {
await (0, promises_1.mkdir)(this.dir, { recursive: true });
}
/**
* Removes _all_ values from disk by truncating the value directory.
*
* Convniently removes everything else in the value directory, even if it isn't a value!
*/
async recycleAll() {
try {
await (0, promises_1.rm)(this.dir, { recursive: true, force: true });
}
catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
}
/**
* Reads a value in a value from disk
* @param value ValueWrapper to read
*/
async look(wrapper) {
try {
return (await (0, promises_1.readFile)(node_path_1.default.join(this.dir, wrapper.id), {
encoding: wrapper.encoding,
}));
}
catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
}
/**
* Removes a value from disk.
*
* This does _not_ destroy the `ValueWrapper` instance in memory, nor does it allow the `id` to be reused.
* @param wrapper ValueWrapper to drop
*/
async recycle(wrapper) {
try {
await (0, promises_1.unlink)(node_path_1.default.join(this.dir, wrapper.id));
}
catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
}
/**
* Create a new {@linkcode ValueWrapper}. Reads the value from disk, if present.
* @param name Name of value
* @param encoding Encoding of value; defaults to `utf8`
* @returns New value
*/
async createWrapper(name, encoding) {
const wrapper = new this.ctor(name, this.look.bind(this), this.put.bind(this), encoding);
if (this.wrapperIds.has(wrapper.id)) {
throw new Error(`ValueWrapper with id "${wrapper.id}" already exists`);
}
try {
await wrapper.look();
}
catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
this.wrapperIds.add(wrapper.id);
return wrapper;
}
/**
* Creates a {@linkcode ValueWrapper} then immediately writes a value to it.
* If there was anything on disk, it is overwritten.
* @param name Name of value
* @param value Value to write
* @returns New `ValueWrapper` w/ value of `value`
*/
async createWrapperWithValue(name, value, encoding) {
const wrapper = await this.createWrapper(name, encoding);
await wrapper.put(value);
return wrapper;
}
/**
* Creates a new {@linkcode ValueBox}
* @param name Name of value container
* @param opts Options
* @returns New value
*/
static create(name, opts) {
return new ValueBox(name, opts);
}
}
exports.ValueBox = ValueBox;
//# sourceMappingURL=valuebox.js.map