1 | ;
|
2 | // Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
|
3 | // Node module: @loopback/boot
|
4 | // This file is licensed under the MIT License.
|
5 | // License text available at https://opensource.org/licenses/MIT
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.BaseArtifactBooter = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
10 | const path_1 = tslib_1.__importDefault(require("path"));
|
11 | const booter_utils_1 = require("./booter-utils");
|
12 | const debug = (0, debug_1.default)('loopback:boot:base-artifact-booter');
|
13 | /**
|
14 | * This class serves as a base class for Booters which follow a pattern of
|
15 | * configure, discover files in a folder(s) using explicit folder / extensions
|
16 | * or a glob pattern and lastly identifying exported classes from such files and
|
17 | * performing an action on such files such as binding them.
|
18 | *
|
19 | * Any Booter extending this base class is expected to
|
20 | *
|
21 | * 1. Set the 'options' property to a object of ArtifactOptions type. (Each extending
|
22 | * class should provide defaults for the ArtifactOptions and use Object.assign to merge
|
23 | * the properties with user provided Options).
|
24 | * 2. Provide it's own logic for 'load' after calling 'await super.load()' to
|
25 | * actually boot the Artifact classes.
|
26 | *
|
27 | * Currently supports the following boot phases: configure, discover, load.
|
28 | *
|
29 | */
|
30 | class BaseArtifactBooter {
|
31 | constructor(projectRoot, options) {
|
32 | this.projectRoot = projectRoot;
|
33 | this.options = options;
|
34 | }
|
35 | /**
|
36 | * Get the name of the artifact loaded by this booter, e.g. "Controller".
|
37 | * Subclasses can override the default logic based on the class name.
|
38 | */
|
39 | get artifactName() {
|
40 | return this.constructor.name.replace(/Booter$/, '');
|
41 | }
|
42 | /**
|
43 | * Configure the Booter by initializing the 'dirs', 'extensions' and 'glob'
|
44 | * properties.
|
45 | *
|
46 | * NOTE: All properties are configured even if all aren't used.
|
47 | */
|
48 | async configure() {
|
49 | this.dirs = this.options.dirs
|
50 | ? Array.isArray(this.options.dirs)
|
51 | ? this.options.dirs
|
52 | : [this.options.dirs]
|
53 | : [];
|
54 | this.extensions = this.options.extensions
|
55 | ? Array.isArray(this.options.extensions)
|
56 | ? this.options.extensions
|
57 | : [this.options.extensions]
|
58 | : [];
|
59 | let joinedDirs = this.dirs.join(',');
|
60 | if (this.dirs.length > 1)
|
61 | joinedDirs = `{${joinedDirs}}`;
|
62 | const joinedExts = `@(${this.extensions.join('|')})`;
|
63 | this.glob = this.options.glob
|
64 | ? this.options.glob
|
65 | : `/${joinedDirs}/${this.options.nested ? '**/*' : '*'}${joinedExts}`;
|
66 | }
|
67 | /**
|
68 | * Discover files based on the 'glob' property relative to the 'projectRoot'.
|
69 | * Discovered artifact files matching the pattern are saved to the
|
70 | * 'discovered' property.
|
71 | */
|
72 | async discover() {
|
73 | debug('Discovering %s artifacts in %j using glob %j', this.artifactName, this.projectRoot, this.glob);
|
74 | this.discovered = await (0, booter_utils_1.discoverFiles)(this.glob, this.projectRoot);
|
75 | if (debug.enabled) {
|
76 | debug('Artifact files found: %s', JSON.stringify(this.discovered.map(f => path_1.default.relative(this.projectRoot, f)), null, 2));
|
77 | }
|
78 | }
|
79 | /**
|
80 | * Filters the exports of 'discovered' files to only be Classes (in case
|
81 | * function / types are exported) as an artifact is a Class. The filtered
|
82 | * artifact Classes are saved in the 'classes' property.
|
83 | *
|
84 | * NOTE: Booters extending this class should call this method (await super.load())
|
85 | * and then process the artifact classes as appropriate.
|
86 | */
|
87 | async load() {
|
88 | this.classes = (0, booter_utils_1.loadClassesFromFiles)(this.discovered, this.projectRoot);
|
89 | }
|
90 | }
|
91 | exports.BaseArtifactBooter = BaseArtifactBooter;
|
92 | //# sourceMappingURL=base-artifact.booter.js.map |
\ | No newline at end of file |