UNPKG

ghoststools

Version:

A collection of functions (tools) I commonly use

97 lines (91 loc) 3.78 kB
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 __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { __markAsModule(target); for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __reExport = (target, module2, desc) => { if (module2 && typeof module2 === "object" || typeof module2 === "function") { for (let key of __getOwnPropNames(module2)) if (!__hasOwnProp.call(target, key) && key !== "default") __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); } return target; }; var __toModule = (module2) => { return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); }; // src/index.ts __export(exports, { castToArray: () => castToArray, flattenPaths: () => flattenPaths, fullNormalize: () => fullNormalize, posixify: () => posixify, readdirRecursive: () => readdirRecursive, removeKeys: () => removeKeys, stripExt: () => stripExt, stripTrailingSlash: () => stripTrailingSlash }); // src/keys.ts var removeKeys = /* @__PURE__ */ __name((object, keys) => { if (!Array.isArray(keys)) keys = [keys]; const entries = Object.entries(object); return entries.reduce((obj, [k, v]) => { if (keys.includes(k)) return obj; else return { ...obj, [k]: v }; }, {}); }, "removeKeys"); // src/cast.ts var castToArray = /* @__PURE__ */ __name((item) => Array.isArray(item) ? item : [item], "castToArray"); // src/fs/files.ts var import_fs = __toModule(require("fs")); var import_path = __toModule(require("path")); var readdirRecursive = /* @__PURE__ */ __name((cwd, options = {}) => { const paths = []; for (const raw of (0, import_fs.readdirSync)(cwd)) { const path = (0, import_path.resolve)(cwd, raw); const stat = (0, import_fs.statSync)(path); if (options.filter && !options.filter(raw, path)) continue; if (stat.isDirectory()) paths.push(...readdirRecursive(path, options)); else paths.push(path); } return paths; }, "readdirRecursive"); var flattenPaths = /* @__PURE__ */ __name((input, options = {}) => castToArray(input).map((path) => { if (!(0, import_fs.existsSync)(path)) throw new Error(`File ${path} does not exist`); const isDirectory = (0, import_fs.lstatSync)(path).isDirectory(); if (!isDirectory) return path; return readdirRecursive(path, options); }).flat(), "flattenPaths"); // src/fs/helpers.ts var import_path2 = __toModule(require("path")); var posixify = /* @__PURE__ */ __name((path) => path.replace(/\\/g, "/"), "posixify"); var stripTrailingSlash = /* @__PURE__ */ __name((path) => path.replace(/\\+$/, "").replace(/\/+$/, ""), "stripTrailingSlash"); var fullNormalize = /* @__PURE__ */ __name((path) => stripTrailingSlash((0, import_path2.normalize)(path)), "fullNormalize"); var stripExt = /* @__PURE__ */ __name((path) => path.slice(0, -(0, import_path2.extname)(path).length || void 0), "stripExt"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { castToArray, flattenPaths, fullNormalize, posixify, readdirRecursive, removeKeys, stripExt, stripTrailingSlash });