@ply-ct/ply
Version:
REST API Automated Testing
198 lines • 7.12 kB
JavaScript
;
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.metas = exports.merge = exports.load = exports.dump = void 0;
const os_1 = require("os");
const jsYaml = __importStar(require("js-yaml"));
const util = __importStar(require("./util"));
function dump(obj, indent) {
return jsYaml.dump(obj, { noCompatMode: true, skipInvalid: true, indent, lineWidth: -1 });
}
exports.dump = dump;
/**
* @param assignLines Here 'force' converts strings, numbers, booleans and nulls to objects
* so that they can contain __start && __end properties (__ prop contains raw value).
*/
function load(file, contents, assignLines = false) {
const lines = {};
const loadOptions = { filename: file };
if (assignLines) {
loadOptions.listener = function (op, state) {
if (assignLines &&
op === 'open' &&
state.kind === 'scalar' &&
state.lineIndent === 0 /* && lines[state.result] === undefined */) {
lines[state.result] = state.line;
}
};
}
const obj = jsYaml.load(contents, loadOptions);
if (obj && assignLines) {
const contentLines = util.lines(contents);
let lastObjProp;
Object.keys(obj).forEach((key) => {
const line = lines[key];
if (typeof line !== 'undefined') {
if (typeof obj[key] === 'object') {
if (!obj[key]) {
obj[key] = {};
}
const objProp = obj[key];
objProp.__start = line;
if (lastObjProp &&
typeof lastObjProp.__start !== 'undefined' &&
typeof objProp.__start !== 'undefined') {
lastObjProp.__end = getEndLine(contentLines, lastObjProp.__start, objProp.__start - 1);
}
lastObjProp = objProp;
}
else if (assignLines === 'force' && obj[key] !== undefined) {
obj[key] = {
__: obj[key],
__start: line,
__end: line
};
}
}
});
if (lastObjProp && typeof lastObjProp.__start !== 'undefined') {
lastObjProp.__end = getEndLine(contentLines, lastObjProp.__start);
}
}
return obj;
}
exports.load = load;
function getEndLine(contentLines, start, end = undefined) {
const reversedLines = getLines(contentLines, start, end).reverse();
let endLine = typeof end === 'undefined' ? start + reversedLines.length - 1 : end;
for (let i = 0; i < reversedLines.length && endLine > start; i++) {
const line = reversedLines[i].trim();
if (!line || line.startsWith('#')) {
endLine--;
}
else {
break;
}
}
return endLine;
}
/**
* Returns content lines where index is between start and end - 1.
* If end is not supplied, read to end of contentLines array.
*/
function getLines(contentLines, start, end) {
return contentLines.reduce((lines, line, i) => {
if (i >= start && (!end || i <= end)) {
lines.push(line);
}
return lines;
}, new Array());
}
function isCommentOrBlank(line) {
const trimmed = line.trim();
return !trimmed || trimmed.startsWith('#');
}
/**
* Merge a raw object into yaml content, respecting existing comments.
*/
function merge(file, yaml, delta, indent = 2) {
const yamlLines = util.lines(yaml);
const outLines = [];
const curObj = load(file, yaml, 'force');
const curKeys = Object.keys(curObj);
const delKeys = Object.keys(delta);
const trailingBlanksAndComments = (lines) => {
const trailers = [];
for (let i = lines.length - 1; i >= 0; i--) {
if (isCommentOrBlank(lines[i])) {
trailers.unshift(lines[i]);
}
else {
return trailers;
}
}
return trailers;
};
// leading comments and blanks
for (const yamlLine of yamlLines) {
if (isCommentOrBlank(yamlLine)) {
outLines.push(yamlLine);
}
else {
break;
}
}
for (let i = 0; i < curKeys.length; i++) {
const key = curKeys[i];
const keyObj = curObj[key];
let curLines;
if (i < curKeys.length - 1) {
const nextObj = curObj[curKeys[i + 1]];
curLines = yamlLines.slice(keyObj.__start, nextObj.__start);
}
else {
curLines = yamlLines.slice(keyObj.__start, yamlLines.length);
}
if (delKeys.includes(key)) {
const delVal = delta[key];
if (delVal !== undefined) {
const delLines = util.lines(dump({ [key]: delVal }, indent));
outLines.push(...delLines.slice(0, delLines.length - 1));
}
const trailing = trailingBlanksAndComments(curLines);
outLines.push(...trailing);
}
else {
outLines.push(...curLines);
}
}
// append delta additions
for (const key of delKeys) {
const delVal = delta[key];
if (delVal !== undefined && !curKeys.includes(key)) {
outLines.push(...util.lines(dump({ [key]: delVal }, indent)));
}
}
return outLines.join(os_1.EOL);
}
exports.merge = merge;
/**
* Meta comments (trimmed with trailing semis removed)
*/
function metas(yaml) {
return util.lines(yaml).reduce((metaLines, line) => {
const trimmed = line.trim();
const hashBang = trimmed.indexOf('#!');
if (hashBang >= 0 && hashBang < trimmed.length - 2) {
let meta = trimmed.substring(hashBang + 2).trim();
if (meta.endsWith(';'))
meta = meta.substring(0, meta.length - 1);
metaLines.push(meta);
}
return metaLines;
}, []);
}
exports.metas = metas;
//# sourceMappingURL=yaml.js.map