@merkur/cli 0.37.10 → 0.37.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/merkur.mjs CHANGED
@@ -6,8 +6,12 @@ import { start } from '../src/commands/start.mjs';
6
6
  import { test } from '../src/commands/test.mjs';
7
7
  import { custom, CUSTOM_PART } from '../src/commands/custom.mjs';
8
8
  import { COMMAND_NAME } from '../src/commands/constant.mjs';
9
+ import { userDefinedCommandsPaths } from '../src/commands/userDefined.mjs';
10
+ import { createCommandConfig } from '../src/commandConfig.mjs';
9
11
 
10
- // eslint-disable-next-line
12
+ import path from 'node:path';
13
+
14
+ // eslint-disable-next-line
11
15
  import packageFile from '../package.json' with { type: 'json' };
12
16
 
13
17
  const program = new Command();
@@ -138,4 +142,29 @@ program
138
142
  await custom({ args, commandArgs: cmd.args, command: COMMAND_NAME.CUSTOM });
139
143
  });
140
144
 
145
+ // Load user-defined commands
146
+ let userDefinedCommands = [];
147
+ for (const { command, dir } of userDefinedCommandsPaths) {
148
+ const programCustom = new Command();
149
+ let commandName = '';
150
+
151
+ try {
152
+ const commandModule = await import(path.join(dir, command));
153
+ commandName = commandModule.default(({ program: programCustom, createCommandConfig })).name();
154
+ } catch (error) {
155
+ console.error(`Error loading command from ${dir}/${command} package:`, error);
156
+ continue;
157
+ }
158
+
159
+ if (userDefinedCommands.includes(commandName)) {
160
+ console.warn(`Command "${commandName}" from ${dir} package cannot be used.\nCommand with the same name already exists.`);
161
+ continue;
162
+ }
163
+
164
+ userDefinedCommands.push(commandName);
165
+ programCustom.commands.forEach(cmd => {
166
+ program.addCommand(cmd);
167
+ });
168
+ };
169
+
141
170
  program.parse(process.argv);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@merkur/cli",
3
- "version": "0.37.10",
3
+ "version": "0.37.12",
4
4
  "description": "Merkur is tiny and extensible library for creating front-end microservices.",
5
5
  "bin": {
6
6
  "merkur": "./bin/merkur.mjs"
@@ -67,5 +67,5 @@
67
67
  "engines": {
68
68
  "node": ">=20"
69
69
  },
70
- "gitHead": "81bd0ca244514e284e2b6b547a657fa4a57d15e1"
70
+ "gitHead": "a7a363751ea00c47e770dae4e9ad13503175043f"
71
71
  }
@@ -0,0 +1,45 @@
1
+ import path from 'node:path';
2
+ import fs from 'node:fs';
3
+
4
+ let userDefinedCommandsPaths = [];
5
+
6
+ /**
7
+ * Extend the commands from a directory
8
+ * @param {string} commandsDir - The directory containing the commands
9
+ * @returns {Promise<void>}
10
+ */
11
+ const extendCommandsFromDir = async (commandsDir) => {
12
+ userDefinedCommandsPaths = userDefinedCommandsPaths.concat(
13
+ fs
14
+ .readdirSync(commandsDir)
15
+ .map((command) => ({ dir: commandsDir, command }))
16
+ .filter(({ command, dir }) => {
17
+ return (
18
+ fs.statSync(path.join(dir, command)).isFile() &&
19
+ (command.endsWith('.js') ||
20
+ command.endsWith('.mjs') ||
21
+ command.endsWith('.cjs'))
22
+ );
23
+ }),
24
+ );
25
+ };
26
+
27
+ const existsMerkurConfig = fs
28
+ .readdirSync(process.cwd())
29
+ .includes('merkur.config.mjs');
30
+
31
+ if (existsMerkurConfig) {
32
+ const merkurDir = path.resolve(process.cwd(), 'node_modules/@merkur');
33
+ if (fs.existsSync(merkurDir)) {
34
+ let dirs = fs.readdirSync(merkurDir);
35
+
36
+ for (const dir of dirs) {
37
+ const fullPath = path.join(merkurDir, `${dir}/commands`);
38
+ if (fs.existsSync(fullPath)) {
39
+ await extendCommandsFromDir(fullPath);
40
+ }
41
+ }
42
+ }
43
+ }
44
+
45
+ export { userDefinedCommandsPaths };
@@ -1,4 +1,5 @@
1
1
  import path from 'node:path';
2
+ import { pathToFileURL } from 'node:url';
2
3
 
3
4
  import { EMITTER_EVENTS, emitter, RESULT_KEY } from './emitter.mjs';
4
5
  import { updateCLIConfig } from './CLIConfig.mjs';
@@ -20,7 +21,7 @@ export async function createMerkurConfig({ cliConfig, context, args } = {}) {
20
21
  );
21
22
 
22
23
  const file = await import(
23
- path.resolve(`${projectFolder}/${MERKUR_CONFIG_FILE}`)
24
+ pathToFileURL(path.resolve(`${projectFolder}/${MERKUR_CONFIG_FILE}`))
24
25
  );
25
26
  merkurConfig = await file.default({
26
27
  cliConfig,