@ply-ct/ply
Version:
REST API Automated Testing
134 lines • 3.94 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.Location = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const util_1 = require("./util");
/**
* Abstraction for a URL or file location.
*/
class Location {
/**
* @param path url or file path (backslashes are replaced with slashes)
*/
constructor(path) {
this.path = (0, util_1.fwdSlashes)(path);
if (this.path.endsWith('/')) {
this.path = this.path.substring(0, this.path.length - 1);
}
}
get lastSlash() {
const ls = this.path.lastIndexOf('/');
if (ls !== -1) {
return ls;
}
}
/**
* Undefined if no parent path
*/
get parent() {
if (this.lastSlash) {
return this.path.substring(0, this.lastSlash);
}
}
get name() {
return this.lastSlash ? this.path.substring(this.lastSlash + 1) : this.path;
}
/**
* Name without extension
*/
get base() {
const name = this.name;
const lastDot = name.lastIndexOf('.');
if (lastDot > 0 && lastDot < name.length - 1) {
return name.substring(0, lastDot);
}
else {
return name;
}
}
get ext() {
const name = this.name;
const lastDot = name.lastIndexOf('.');
if (lastDot > 0 && lastDot < name.length - 1) {
return name.substring(lastDot + 1);
}
}
/**
* Returns zero for URLs, undefined if file does not exist
*/
get timestamp() {
if (this.isUrl) {
return 0;
}
else if (fs.existsSync(this.path)) {
return fs.statSync(this.path).mtimeMs;
}
}
get isYaml() {
return this.ext === 'yaml' || this.ext === 'yml';
}
get scheme() {
if (this.path.startsWith('https://')) {
return 'https';
}
else if (this.path.startsWith('http://')) {
return 'http';
}
}
get isUrl() {
return !!this.scheme;
}
get isAbsolute() {
return this.isUrl || path.isAbsolute(this.path);
}
get absolute() {
if (this.isUrl) {
return this.path;
}
else {
return (0, util_1.fwdSlashes)(path.normalize(path.resolve(this.path)));
}
}
/**
* TODO: handle urls
*/
isChildOf(parent) {
const relative = this.relativeTo(parent);
return relative.length > 0 && !relative.startsWith('..') && !path.isAbsolute(relative);
}
/**
* TODO: handle urls
*/
relativeTo(parent) {
return (0, util_1.fwdSlashes)(path.normalize(path.relative(parent, this.path)));
}
toString() {
return this.path;
}
}
exports.Location = Location;
//# sourceMappingURL=location.js.map