UNPKG

4.74 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 var desc = Object.getOwnPropertyDescriptor(m, k);
12 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
13 desc = { enumerable: true, get: function() { return m[k]; } };
14 }
15 Object.defineProperty(o, k2, desc);
16}) : (function(o, m, k, k2) {
17 if (k2 === undefined) k2 = k;
18 o[k2] = m[k];
19}));
20var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21 Object.defineProperty(o, "default", { enumerable: true, value: v });
22}) : function(o, v) {
23 o["default"] = v;
24});
25var __importStar = (this && this.__importStar) || function (mod) {
26 if (mod && mod.__esModule) return mod;
27 var result = {};
28 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29 __setModuleDefault(result, mod);
30 return result;
31};
32Object.defineProperty(exports, "__esModule", { value: true });
33exports.FileSystemEngineHost = void 0;
34const fs_1 = require("fs");
35const path_1 = require("path");
36const rxjs_1 = require("rxjs");
37const operators_1 = require("rxjs/operators");
38const src_1 = require("../src");
39const export_ref_1 = require("./export-ref");
40const file_system_engine_host_base_1 = require("./file-system-engine-host-base");
41/**
42 * A simple EngineHost that uses a root with one directory per collection inside of it. The
43 * collection declaration follows the same rules as the regular FileSystemEngineHostBase.
44 */
45class FileSystemEngineHost extends file_system_engine_host_base_1.FileSystemEngineHostBase {
46 constructor(_root) {
47 super();
48 this._root = _root;
49 }
50 _resolveCollectionPath(name) {
51 try {
52 // Allow `${_root}/${name}.json` as a collection.
53 const maybePath = require.resolve((0, path_1.join)(this._root, name + '.json'));
54 if ((0, fs_1.existsSync)(maybePath)) {
55 return maybePath;
56 }
57 }
58 catch (error) { }
59 try {
60 // Allow `${_root}/${name}/collection.json.
61 const maybePath = require.resolve((0, path_1.join)(this._root, name, 'collection.json'));
62 if ((0, fs_1.existsSync)(maybePath)) {
63 return maybePath;
64 }
65 }
66 catch (error) { }
67 throw new file_system_engine_host_base_1.CollectionCannotBeResolvedException(name);
68 }
69 _resolveReferenceString(refString, parentPath) {
70 // Use the same kind of export strings as NodeModule.
71 const ref = new export_ref_1.ExportStringRef(refString, parentPath);
72 if (!ref.ref) {
73 return null;
74 }
75 return { ref: ref.ref, path: ref.module };
76 }
77 _transformCollectionDescription(name, desc) {
78 if (!desc.schematics || typeof desc.schematics != 'object') {
79 throw new file_system_engine_host_base_1.CollectionMissingSchematicsMapException(name);
80 }
81 return {
82 ...desc,
83 name,
84 };
85 }
86 _transformSchematicDescription(name, _collection, desc) {
87 if (!desc.factoryFn || !desc.path || !desc.description) {
88 throw new file_system_engine_host_base_1.SchematicMissingFieldsException(name);
89 }
90 return desc;
91 }
92 hasTaskExecutor(name) {
93 if (super.hasTaskExecutor(name)) {
94 return true;
95 }
96 try {
97 const maybePath = require.resolve((0, path_1.join)(this._root, name));
98 if ((0, fs_1.existsSync)(maybePath)) {
99 return true;
100 }
101 }
102 catch { }
103 return false;
104 }
105 createTaskExecutor(name) {
106 if (!super.hasTaskExecutor(name)) {
107 try {
108 const path = require.resolve((0, path_1.join)(this._root, name));
109 // Default handling code is for old tasks that incorrectly export `default` with non-ESM module
110 return (0, rxjs_1.from)(Promise.resolve().then(() => __importStar(require(path))).then((mod) => { var _a; return (((_a = mod.default) === null || _a === void 0 ? void 0 : _a.default) || mod.default)(); })).pipe((0, operators_1.catchError)(() => (0, rxjs_1.throwError)(new src_1.UnregisteredTaskException(name))));
111 }
112 catch { }
113 }
114 return super.createTaskExecutor(name);
115 }
116}
117exports.FileSystemEngineHost = FileSystemEngineHost;