UNPKG

@plugjs/plug

Version:
114 lines (112 loc) 4.18 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // files.ts var files_exports = {}; __export(files_exports, { Files: () => Files }); module.exports = __toCommonJS(files_exports); var import_node_util = require("node:util"); var import_asserts = require("./asserts.cjs"); var import_fs = require("./fs.cjs"); var import_paths = require("./paths.cjs"); var Files = class _Files { _directory; _files; /** * Create a new {@link Files} instance rooted in the specified `directory` * relative to the specified {@link Run}'s directory. */ constructor(directory) { this._directory = directory || (0, import_paths.getCurrentWorkingDirectory)(); this._files = []; Object.defineProperty(this, import_node_util.inspect.custom, { value: () => ({ directory: this._directory, files: [...this._files] }) }); } /** Return the _directory_ where this {@link Files} is rooted */ get directory() { return this._directory; } /** Return the number of files tracked by this instance. */ get length() { return this._files.length; } /** Return an iterator over all _relative_ files of this instance */ *[Symbol.iterator]() { for (const file of this._files) yield file; } /** Return an iterator over all _absolute_ files of this instance */ *absolutePaths() { for (const file of this) yield (0, import_paths.resolveAbsolutePath)(this._directory, file); } /** Return an iterator over all _relative_ to _absolute_ mappings */ *pathMappings() { for (const file of this) yield [file, (0, import_paths.resolveAbsolutePath)(this._directory, file)]; } static builder(arg) { if (!arg) arg = (0, import_paths.getCurrentWorkingDirectory)(); const directory = typeof arg === "string" ? arg : arg.directory; const set = typeof arg === "string" ? /* @__PURE__ */ new Set() : new Set(arg._files); const instance = new _Files(directory); let built = false; return { directory: instance.directory, add(...files) { (0, import_asserts.assert)(!built, 'FileBuilder "build()" already called'); for (const file of files) { const relative = (0, import_paths.assertRelativeChildPath)(instance.directory, file); set.add(relative); } return this; }, merge(...args) { (0, import_asserts.assert)(!built, 'FileBuilder "build()" already called'); for (const files of args) { for (const file of files.absolutePaths()) { this.add(file); } } return this; }, async write(file, content) { const relative = (0, import_paths.assertRelativeChildPath)(instance.directory, file); const absolute = (0, import_paths.resolveAbsolutePath)(instance.directory, relative); const directory2 = (0, import_paths.getAbsoluteParent)(absolute); await (0, import_fs.mkdir)(directory2, { recursive: true }); await (0, import_fs.writeFile)(absolute, content); this.add(absolute); return absolute; }, build() { (0, import_asserts.assert)(!built, 'FileBuilder "build()" already called'); built = true; instance._files.push(...set); instance._files.sort(); return instance; } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Files }); //# sourceMappingURL=files.cjs.map