UNPKG

@grammyjs/i18n

Version:

Internationalization plugin for grammY based on Fluent.

188 lines (187 loc) 6.91 kB
"use strict"; 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.WalkError = void 0; exports.walk = walk; exports.walkSync = walkSync; // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Documentation and interface for walk were adapted from Go // https://golang.org/pkg/path/filepath/#Walk // Copyright 2009 The Go Authors. All rights reserved. BSD license. const dntShim = __importStar(require("../../../../_dnt.shims.js")); const asserts_js_1 = require("../_util/asserts.js"); const mod_js_1 = require("../path/mod.js"); const _util_js_1 = require("./_util.js"); class WalkError extends Error { constructor(cause, path) { super(`${cause instanceof Error ? cause.message : cause} for path "${path}"`); Object.defineProperty(this, "cause", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "name", { enumerable: true, configurable: true, writable: true, value: "WalkError" }); Object.defineProperty(this, "path", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.path = path; this.cause = cause; } } exports.WalkError = WalkError; function include(path, exts, match, skip) { if (exts && !exts.some((ext) => path.endsWith(ext))) { return false; } if (match && !match.some((pattern) => !!path.match(pattern))) { return false; } if (skip && skip.some((pattern) => !!path.match(pattern))) { return false; } return true; } function wrapErrorWithPath(err, root) { if (err instanceof WalkError) return err; return new WalkError(err, root); } /** * Walks the file tree rooted at root, yielding each file or directory in the * tree filtered according to the given options. * * @example * ```ts * import { walk } from "https://deno.land/std@$STD_VERSION/fs/walk.ts"; * import { assert } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * for await (const entry of walk(".")) { * console.log(entry.path); * assert(entry.isFile); * } * ``` */ async function* walk(root, { maxDepth = Infinity, includeFiles = true, includeDirs = true, followSymlinks = false, exts = undefined, match = undefined, skip = undefined, } = {}) { if (maxDepth < 0) { return; } root = (0, _util_js_1.toPathString)(root); if (includeDirs && include(root, exts, match, skip)) { yield await (0, _util_js_1.createWalkEntry)(root); } if (maxDepth < 1 || !include(root, undefined, undefined, skip)) { return; } try { for await (const entry of dntShim.Deno.readDir(root)) { (0, asserts_js_1.assert)(entry.name != null); let path = (0, mod_js_1.join)(root, entry.name); let { isSymlink, isDirectory } = entry; if (isSymlink) { if (!followSymlinks) continue; path = await dntShim.Deno.realPath(path); // Caveat emptor: don't assume |path| is not a symlink. realpath() // resolves symlinks but another process can replace the file system // entity with a different type of entity before we call lstat(). ({ isSymlink, isDirectory } = await dntShim.Deno.lstat(path)); } if (isSymlink || isDirectory) { yield* walk(path, { maxDepth: maxDepth - 1, includeFiles, includeDirs, followSymlinks, exts, match, skip, }); } else if (includeFiles && include(path, exts, match, skip)) { yield { path, ...entry }; } } } catch (err) { throw wrapErrorWithPath(err, (0, mod_js_1.normalize)(root)); } } /** Same as walk() but uses synchronous ops */ function* walkSync(root, { maxDepth = Infinity, includeFiles = true, includeDirs = true, followSymlinks = false, exts = undefined, match = undefined, skip = undefined, } = {}) { root = (0, _util_js_1.toPathString)(root); if (maxDepth < 0) { return; } if (includeDirs && include(root, exts, match, skip)) { yield (0, _util_js_1.createWalkEntrySync)(root); } if (maxDepth < 1 || !include(root, undefined, undefined, skip)) { return; } let entries; try { entries = dntShim.Deno.readDirSync(root); } catch (err) { throw wrapErrorWithPath(err, (0, mod_js_1.normalize)(root)); } for (const entry of entries) { (0, asserts_js_1.assert)(entry.name != null); let path = (0, mod_js_1.join)(root, entry.name); let { isSymlink, isDirectory } = entry; if (isSymlink) { if (!followSymlinks) continue; path = dntShim.Deno.realPathSync(path); // Caveat emptor: don't assume |path| is not a symlink. realpath() // resolves symlinks but another process can replace the file system // entity with a different type of entity before we call lstat(). ({ isSymlink, isDirectory } = dntShim.Deno.lstatSync(path)); } if (isSymlink || isDirectory) { yield* walkSync(path, { maxDepth: maxDepth - 1, includeFiles, includeDirs, followSymlinks, exts, match, skip, }); } else if (includeFiles && include(path, exts, match, skip)) { yield { path, ...entry }; } } }