1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.EntryResolver = void 0;
|
7 |
|
8 | var _path = _interopRequireDefault(require("path"));
|
9 |
|
10 | var _utils = require("@parcel/utils");
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 |
|
14 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
15 |
|
16 | class EntryResolver {
|
17 | constructor(options) {
|
18 | _defineProperty(this, "fs", void 0);
|
19 |
|
20 | this.fs = options.inputFS;
|
21 | }
|
22 |
|
23 | async resolveEntry(entry) {
|
24 | if ((0, _utils.isGlob)(entry)) {
|
25 | let files = await (0, _utils.glob)(entry, this.fs, {
|
26 | absolute: true,
|
27 | onlyFiles: false
|
28 | });
|
29 | let results = await Promise.all(files.map(f => this.resolveEntry(f)));
|
30 | return results.reduce((p, res) => ({
|
31 | entries: p.entries.concat(res.entries),
|
32 | files: p.files.concat(res.files)
|
33 | }), {
|
34 | entries: [],
|
35 | files: []
|
36 | });
|
37 | }
|
38 |
|
39 | let stat;
|
40 |
|
41 | try {
|
42 | stat = await this.fs.stat(entry);
|
43 | } catch (err) {
|
44 | throw new Error(`Entry ${entry} does not exist`);
|
45 | }
|
46 |
|
47 | if (stat.isDirectory()) {
|
48 | let pkg = await this.readPackage(entry);
|
49 |
|
50 | if (pkg && typeof pkg.source === 'string') {
|
51 | let source = _path.default.join(_path.default.dirname(pkg.filePath), pkg.source);
|
52 |
|
53 | try {
|
54 | stat = await this.fs.stat(source);
|
55 | } catch (err) {
|
56 | throw new Error(`${pkg.source} in ${_path.default.relative(this.fs.cwd(), pkg.filePath)}#source does not exist`);
|
57 | }
|
58 |
|
59 | if (!stat.isFile()) {
|
60 | throw new Error(`${pkg.source} in ${_path.default.relative(this.fs.cwd(), pkg.filePath)}#source is not a file`);
|
61 | }
|
62 |
|
63 | return {
|
64 | entries: [{
|
65 | filePath: source,
|
66 | packagePath: entry
|
67 | }],
|
68 | files: [{
|
69 | filePath: pkg.filePath
|
70 | }]
|
71 | };
|
72 | }
|
73 |
|
74 | throw new Error(`Could not find entry: ${entry}`);
|
75 | } else if (stat.isFile()) {
|
76 | return {
|
77 | entries: [{
|
78 | filePath: entry
|
79 | }],
|
80 | files: []
|
81 | };
|
82 | }
|
83 |
|
84 | throw new Error(`Unknown entry ${entry}`);
|
85 | }
|
86 |
|
87 | async readPackage(entry) {
|
88 | let content, pkg;
|
89 |
|
90 | let pkgFile = _path.default.join(entry, 'package.json');
|
91 |
|
92 | try {
|
93 | content = await this.fs.readFile(pkgFile, 'utf8');
|
94 | } catch (err) {
|
95 | return null;
|
96 | }
|
97 |
|
98 | try {
|
99 | pkg = JSON.parse(content);
|
100 | } catch (err) {
|
101 | throw new Error(`Error parsing ${_path.default.relative(this.fs.cwd(), pkgFile)}: ${err.message}`);
|
102 | }
|
103 |
|
104 | pkg.filePath = pkgFile;
|
105 | return pkg;
|
106 | }
|
107 |
|
108 | }
|
109 |
|
110 | exports.EntryResolver = EntryResolver; |
\ | No newline at end of file |