1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | exports.ManifestLoader = void 0;
|
12 | const fs_extra_1 = require("fs-extra");
|
13 | const utils_1 = require("@poppinss/utils");
|
14 | const helpers_1 = require("@poppinss/utils/build/helpers");
|
15 | const validateCommand_1 = require("../utils/validateCommand");
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | class ManifestLoader {
|
21 | constructor(files) {
|
22 | this.files = files;
|
23 | |
24 |
|
25 |
|
26 | this.manifestFiles = [];
|
27 | this.booted = false;
|
28 | }
|
29 | |
30 |
|
31 |
|
32 | async loadManifestFile(file) {
|
33 | const manifestCommands = await (0, fs_extra_1.readJSON)(file.manifestAbsPath);
|
34 | |
35 |
|
36 |
|
37 | const isNewManifestFile = manifestCommands['commands'] && manifestCommands['aliases'];
|
38 | const commands = isNewManifestFile ? manifestCommands['commands'] : manifestCommands;
|
39 | const aliases = isNewManifestFile ? manifestCommands['aliases'] : {};
|
40 | return { basePath: file.basePath, commands, aliases };
|
41 | }
|
42 | |
43 |
|
44 |
|
45 | getCommandManifest(commandName) {
|
46 | return this.manifestFiles.find(({ commands, aliases }) => {
|
47 | const aliasCommandName = aliases[commandName];
|
48 | return commands[commandName] || commands[aliasCommandName];
|
49 | });
|
50 | }
|
51 | |
52 |
|
53 |
|
54 | async boot() {
|
55 | if (this.booted) {
|
56 | return;
|
57 | }
|
58 | this.booted = true;
|
59 | this.manifestFiles = await Promise.all(this.files.map((file) => this.loadManifestFile(file)));
|
60 | }
|
61 | |
62 |
|
63 |
|
64 | getCommandBasePath(commandName) {
|
65 | return this.getCommandManifest(commandName)?.basePath;
|
66 | }
|
67 | |
68 |
|
69 |
|
70 |
|
71 | getCommand(commandName) {
|
72 | const manifestCommands = this.getCommandManifest(commandName);
|
73 | if (!manifestCommands) {
|
74 | return;
|
75 | }
|
76 | const aliasCommandName = manifestCommands.aliases[commandName];
|
77 | const command = manifestCommands.commands[commandName] || manifestCommands.commands[aliasCommandName];
|
78 | return {
|
79 | basePath: manifestCommands.basePath,
|
80 | command: command,
|
81 | };
|
82 | }
|
83 | |
84 |
|
85 |
|
86 | hasCommand(commandName) {
|
87 | return !!this.getCommandBasePath(commandName);
|
88 | }
|
89 | |
90 |
|
91 |
|
92 |
|
93 | async loadCommand(commandName) {
|
94 | const { basePath, command } = this.getCommand(commandName);
|
95 | const commandConstructor = (0, utils_1.esmRequire)((0, helpers_1.resolveFrom)(basePath, command.commandPath));
|
96 | (0, validateCommand_1.validateCommand)(commandConstructor);
|
97 | return commandConstructor;
|
98 | }
|
99 | |
100 |
|
101 |
|
102 | getCommands() {
|
103 | return this.manifestFiles.reduce((result, { commands, aliases }) => {
|
104 | Object.keys(commands).forEach((commandName) => {
|
105 | result.commands = result.commands.concat(commands[commandName]);
|
106 | });
|
107 | Object.assign(result.aliases, aliases);
|
108 | return result;
|
109 | }, { commands: [], aliases: {} });
|
110 | }
|
111 | }
|
112 | exports.ManifestLoader = ManifestLoader;
|