UNPKG

eyeglass

Version:
113 lines 4.57 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs = __importStar(require("fs")); const glob = __importStar(require("glob")); const path = __importStar(require("path")); const lodash_merge_1 = __importDefault(require("lodash.merge")); const URI_1 = require("../util/URI"); const json_stable_stringify_1 = __importDefault(require("json-stable-stringify")); /* class AssetsSource * * srcPath - directory where assets are sourced. * Option: name [Optional] - The logical name of this path entry. When specified, * the source url of an asset in this directory will be of the form * "<name>/path/relativeTo/srcPath.ext". * Option: httpPrefix [Optional] - The http prefix where the assets url should be anchored * "url(<httpPrefix>/path/relativeTo/srcPath.ext)". Defaults to "/<name>" or just "/" * when there is no name. * Option: pattern [Optional] - A glob pattern that controls what files can be in this asset path. * Defaults to "**\/*". * Option: globOpts [Optional] - Options to use for globbing. * See: https://github.com/isaacs/node-glob#options */ class AssetsSource { constructor(srcPath, options) { options = options || { directory: srcPath }; if (fs.existsSync(srcPath) && !fs.statSync(srcPath).isDirectory()) { throw new Error("Expected " + srcPath + " to be a directory."); } // TODO (test) - what is this for? needs a test this.name = options.name || null; this.httpPrefix = options.httpPrefix || this.name; this.srcPath = URI_1.URI.system(srcPath); this.pattern = options.pattern || "**/*"; this.globOpts = lodash_merge_1.default( // default glob options { follow: true, nodir: true }, // with the custom options options.globOpts, // but the following cannot be overridden by options.globOpts { cwd: this.srcPath, root: this.srcPath }); } /** * returns any assets found in the given source * @param {String} namespace - the namespace * @returns {Object} the object containing the namespace and array of discovered files */ getAssets(namespace) { namespace = this.name || namespace; let files = glob.sync(this.pattern, this.globOpts).map((file) => { file = URI_1.URI.preserve(file); let uri = URI_1.URI.join(this.httpPrefix, namespace, file); return { name: URI_1.URI.web(file), sourcePath: path.join(this.srcPath, file), uri: URI_1.URI.web(uri) }; }); return { namespace, files }; } /** * returns a string representation of the source pattern * @returns {string} the source pattern */ toString() { return this.srcPath + "/" + this.pattern; } /** * Build a string suitable for caching an instance of this * @returns {String} the cache key */ cacheKey(namespace) { return "[" + "httpPrefix=" + (this.httpPrefix ? this.httpPrefix : "") + ";name=" + (this.name ? this.name : (namespace ? namespace : "")) + ";srcPath=" + this.srcPath + ";pattern=" + this.pattern + // json-stable-stringify orders keys when stringifying (JSON.stringify does not) ";opts=" + json_stable_stringify_1.default(this.globOpts, { replacer: skipSomeKeys }) + "]"; } } // don't include these globOpts in the cacheKey function skipSomeKeys(key, value) { // these are set to this.srcPath, which is already included in the cacheKey if (key === "cwd" || key === "root") { return undefined; } // these are added inside glob and always set to true, which happens after this string // is created when glob.sync() is run in #getAssets() // (see #setopts() in glob/common.js) if (key === "nonegate" || key === "nocomment") { return undefined; } return value; } exports.default = AssetsSource; //# sourceMappingURL=AssetsSource.js.map