foundry-dsa41-game/node_modules/@joneff/baka/dist/templated-path.js

87 lines
3.1 KiB
JavaScript

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var templated_path_exports = {};
__export(templated_path_exports, {
replacePathVariables: () => replacePathVariables
});
module.exports = __toCommonJS(templated_path_exports);
var import_path = __toESM(require("path"));
const REGEXP = /\[([\w]+)\]/gi;
function parsePath(filePath) {
const pathData = {};
pathData.file = filePath.startsWith("./") ? filePath.slice(2) : filePath;
pathData.ext = import_path.default.extname(pathData.file);
pathData.base = import_path.default.basename(pathData.file);
pathData.name = pathData.base.slice(0, pathData.base.length - pathData.ext.length);
pathData.path = pathData.file.slice(0, pathData.file.length - pathData.base.length);
return pathData;
}
function replacePathVariables(template, filePath) {
const replacements = /* @__PURE__ */ new Map();
let result = template;
let pathData = filePath;
if (pathData) {
if (typeof pathData === "string") {
pathData = parsePath(pathData);
}
replacements.set("file", replacer(pathData.file));
replacements.set("path", replacer(pathData.path, true));
replacements.set("base", replacer(pathData.base));
replacements.set("name", replacer(pathData.name));
replacements.set("ext", replacer(pathData.ext, true));
}
if (typeof result === "function") {
result = result(pathData);
}
result = result.replace(REGEXP, (match, content) => {
const _replacer = replacements.get(content);
if (_replacer !== void 0) {
return _replacer(content, result);
}
return match;
});
return result;
}
function replacer(value, allowEmpty) {
function fn(match, input) {
if (typeof value === "function") {
value = value();
}
if (value === null || value === void 0) {
if (!allowEmpty) {
throw new Error(
`Variable ${match} not implemented in this context: ${input}`
);
}
return "";
}
return `${value}`;
}
return fn;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
replacePathVariables
});