UNPKG

1.86 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.listModules = void 0;
4const glob = require("glob");
5const path = require("path");
6const utils_1 = require("./utils");
7// Regex to extract the module name.
8const nameExpr = /(.*)\..*/i;
9/**
10 * Internal method for globbing a single pattern.
11 *
12 * @param {String} globPattern
13 * The glob pattern.
14 *
15 * @param {String} opts.cwd
16 * Current working directory, used for resolving filepaths.
17 * Defaults to `process.cwd()`.
18 *
19 * @return {[{name, path, opts}]}
20 * The module names and paths.
21 *
22 * @api private
23 */
24function _listModules(globPattern, opts) {
25 opts = Object.assign({ cwd: process.cwd(), glob: glob.sync }, opts);
26 let patternOpts = null;
27 if (globPattern instanceof Array) {
28 patternOpts = globPattern[1];
29 globPattern = globPattern[0];
30 }
31 const result = opts.glob(globPattern, { cwd: opts.cwd });
32 const mapped = result.map((p) => ({
33 name: nameExpr.exec(path.basename(p))[1],
34 path: path.resolve(opts.cwd, p),
35 opts: patternOpts,
36 }));
37 return mapped;
38}
39/**
40 * Returns a list of {name, path} pairs,
41 * where the name is the module name, and path is the actual
42 * full path to the module.
43 *
44 * @param {String|Array<String>} globPatterns
45 * The glob pattern as a string or an array of strings.
46 *
47 * @param {String} opts.cwd
48 * Current working directory, used for resolving filepaths.
49 * Defaults to `process.cwd()`.
50 *
51 * @return {[{name, path}]}
52 * An array of objects with the module names and paths.
53 */
54function listModules(globPatterns, opts) {
55 if (Array.isArray(globPatterns)) {
56 return utils_1.flatten(globPatterns.map((p) => _listModules(p, opts)));
57 }
58 return _listModules(globPatterns, opts);
59}
60exports.listModules = listModules;
61//# sourceMappingURL=list-modules.js.map
\No newline at end of file