UNPKG

1.98 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const path_1 = __importDefault(require("path"));
7const assert_1 = __importDefault(require("assert"));
8const glob_1 = __importDefault(require("glob"));
9const util_1 = require("util");
10const fs_extra_1 = require("fs-extra");
11const file_fs_ref_1 = __importDefault(require("../file-fs-ref"));
12const vanillaGlob = util_1.promisify(glob_1.default);
13async function glob(pattern, opts, mountpoint) {
14 let options;
15 if (typeof opts === 'string') {
16 options = { cwd: opts };
17 }
18 else {
19 options = opts;
20 }
21 if (!options.cwd) {
22 throw new Error('Second argument (basePath) must be specified for names of resulting files');
23 }
24 if (!path_1.default.isAbsolute(options.cwd)) {
25 throw new Error(`basePath/cwd must be an absolute path (${options.cwd})`);
26 }
27 const results = {};
28 options.symlinks = {};
29 options.statCache = {};
30 options.stat = true;
31 options.dot = true;
32 const files = await vanillaGlob(pattern, options);
33 for (const relativePath of files) {
34 const fsPath = path_1.default.join(options.cwd, relativePath).replace(/\\/g, '/');
35 let stat = options.statCache[fsPath];
36 assert_1.default(stat, `statCache does not contain value for ${relativePath} (resolved to ${fsPath})`);
37 if (stat.isFile()) {
38 const isSymlink = options.symlinks[fsPath];
39 if (isSymlink) {
40 stat = await fs_extra_1.lstat(fsPath);
41 }
42 let finalPath = relativePath;
43 if (mountpoint) {
44 finalPath = path_1.default.join(mountpoint, finalPath);
45 }
46 results[finalPath] = new file_fs_ref_1.default({ mode: stat.mode, fsPath });
47 }
48 }
49 return results;
50}
51exports.default = glob;