UNPKG

3.9 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/ace
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.ManifestLoader = void 0;
12const fs_extra_1 = require("fs-extra");
13const utils_1 = require("@poppinss/utils");
14const helpers_1 = require("@poppinss/utils/build/helpers");
15const validateCommand_1 = require("../utils/validateCommand");
16/**
17 * The manifest loader exposes the API to load ace commands from one
18 * or more manifest files.
19 */
20class ManifestLoader {
21 constructor(files) {
22 this.files = files;
23 /**
24 * An array of defined manifest files
25 */
26 this.manifestFiles = [];
27 this.booted = false;
28 }
29 /**
30 * Loads the manifest file from the disk
31 */
32 async loadManifestFile(file) {
33 const manifestCommands = await (0, fs_extra_1.readJSON)(file.manifestAbsPath);
34 /**
35 * Find if we are dealing with an old or the new manifest file
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 * Returns the command manifest node for a give command
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 * Boot manifest loader to read all manifest files from the disk
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 * Returns base path for a given command
63 */
64 getCommandBasePath(commandName) {
65 return this.getCommandManifest(commandName)?.basePath;
66 }
67 /**
68 * Returns manifest command node. One must load the command
69 * in order to use it
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 * Find if a command exists or not
85 */
86 hasCommand(commandName) {
87 return !!this.getCommandBasePath(commandName);
88 }
89 /**
90 * Load command from the disk. Make sure to use [[hasCommand]] before
91 * calling this method
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 * Returns an array of manifest commands
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}
112exports.ManifestLoader = ManifestLoader;