@ply-ct/ply
Version:
REST API Automated Testing
228 lines • 7.29 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Storage = exports.AsyncStorage = void 0;
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const util = __importStar(require("./util"));
const location_1 = require("./location");
/**
* Abstracts storage to file system or html localStorage.
* TODO: rename Storage to SyncStorage, and this to Storage,
* and phase out SyncStorage.
*/
class AsyncStorage {
/**
* @param location file or logical path
*/
constructor(location) {
this.location = new location_1.Location(location);
if (typeof localStorage !== 'undefined') {
this.localStorage = localStorage;
}
}
async exists() {
if (this.localStorage) {
return this.localStorage.getItem(this.location.path) !== null;
}
else {
try {
await fs.promises.access(this.location.path);
return true;
}
catch (err) {
return false;
}
}
}
async read() {
if (this.localStorage) {
return this.localStorage.getItem(this.location.path);
}
else {
if (await this.exists()) {
return await fs.promises.readFile(this.location.path, 'utf-8');
}
}
}
/**
* For file system storage, all newlines are replaced with OS-appropriate
*/
async write(contents) {
if (this.localStorage) {
this.localStorage.setItem(this.location.path, contents);
}
else {
await this.mkdir();
await fs.promises.writeFile(this.location.path, contents.replace(/\r?\n/, os.EOL));
}
}
/**
* For file system storage, all newlines are replaced with OS-appropriate
*/
async append(contents) {
if (this.localStorage) {
const exist = this.localStorage.getItem(this.location.path);
this.localStorage.setItem(this.location.path, exist ? exist + contents : contents);
}
else {
await this.mkdir();
await fs.promises.appendFile(this.location.path, contents.replace(/\r?\n/, os.EOL));
}
}
async insert(contents, start) {
let newLines = util.lines(contents);
const exist = await this.read();
if (exist) {
const existLines = util.lines(exist);
const preLines = start > 0 ? existLines.slice(0, start) : [];
const postLines = existLines.slice(start);
newLines = [...preLines, ...newLines, ...postLines];
}
await this.write(newLines.join('\n'));
}
async padLines(start, lines) {
await this.insert(''.padStart(lines - 1, '\n'), start);
}
async clear() {
await this.write('');
}
async remove() {
if (this.localStorage) {
this.localStorage.removeItem(this.location.path);
}
else {
if (fs.existsSync(this.location.path)) {
await fs.promises.unlink(this.location.path);
}
}
}
/**
* TODO local storage
*/
async mkdir() {
if (this.location.parent) {
await fs.promises.mkdir(this.location.parent, { recursive: true });
}
}
toString() {
return this.location.toString();
}
}
exports.AsyncStorage = AsyncStorage;
/**
* Abstracts storage to file system or html localStorage.
*/
class Storage {
/**
* @param location file or logical path
*/
constructor(location) {
this.location = new location_1.Location(location);
if (typeof localStorage !== 'undefined') {
this.localStorage = localStorage;
}
}
get exists() {
if (this.localStorage) {
return this.localStorage.getItem(this.location.path) !== null;
}
else {
return fs.existsSync(this.location.path);
}
}
read() {
if (this.localStorage) {
return this.localStorage.getItem(this.location.path);
}
else {
if (fs.existsSync(this.location.path)) {
return fs.readFileSync(this.location.path, 'utf-8');
}
}
}
/**
* For file system storage, all newlines are replaced with OS-appropriate
*/
write(contents) {
if (this.localStorage) {
this.localStorage.setItem(this.location.path, contents);
}
else {
this.mkdir();
fs.writeFileSync(this.location.path, contents.replace(/\r?\n/, os.EOL));
}
}
/**
* For file system storage, all newlines are replaced with OS-appropriate
*/
append(contents) {
if (this.localStorage) {
const exist = this.localStorage.getItem(this.location.path);
this.localStorage.setItem(this.location.path, exist ? exist + contents : contents);
}
else {
this.mkdir();
fs.appendFileSync(this.location.path, contents.replace(/\r?\n/, os.EOL));
}
}
insert(contents, start) {
let newLines = util.lines(contents);
const exist = this.read();
if (exist) {
const existLines = util.lines(exist);
const preLines = start > 0 ? existLines.slice(0, start) : [];
const postLines = existLines.slice(start);
newLines = [...preLines, ...newLines, ...postLines];
}
this.write(newLines.join('\n'));
}
padLines(start, lines) {
this.insert(''.padStart(lines - 1, '\n'), start);
}
clear() {
this.write('');
}
remove() {
if (this.localStorage) {
this.localStorage.removeItem(this.location.path);
}
else {
if (fs.existsSync(this.location.path)) {
fs.unlinkSync(this.location.path);
}
}
}
mkdir() {
if (this.location.parent) {
fs.mkdirSync(this.location.parent, { recursive: true });
}
}
toString() {
return this.location.toString();
}
}
exports.Storage = Storage;
//# sourceMappingURL=storage.js.map