@flowscripter/dynamic-cli-framework 1.0.8 → 1.0.10

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/README.md CHANGED
@@ -1441,8 +1441,8 @@ classDiagram
1441
1441
 
1442
1442
  ### Debug Logging
1443
1443
 
1444
- Internal framework logging can be enabled by setting the `CLI_DEBUG` environment
1445
- variable.
1444
+ Internal framework logging can be enabled by setting the
1445
+ `DYNAMIC_CLI_FRAMEWORK_DEBUG` environment variable.
1446
1446
 
1447
1447
  The logging implementation will look for an object conforming to the `Logger`
1448
1448
  interface and use it if found. If not found, a simple logging implementation
@@ -1456,7 +1456,8 @@ validation that takes place is for commands or services provided by plugins
1456
1456
  BEFORE they are installed.
1457
1457
 
1458
1458
  When using `launcher.ts` runtime validation of all commands and services can be
1459
- forced by defining the `CLI_VALIDATE_ALL` environment variable.
1459
+ forced by defining the `DYNAMIC_CLI_FRAMEWORK_VALIDATE_ALL` environment
1460
+ variable.
1460
1461
 
1461
1462
  Command validation includes:
1462
1463
 
package/package.json CHANGED
@@ -1,8 +1,24 @@
1
1
  {
2
2
  "name": "@flowscripter/dynamic-cli-framework",
3
+ "description": "A framework for developing CLI applications which supports dynamic discovery and installation of new commands",
4
+ "homepage": "https://github.com/flowscripter/dynamic-cli-framework#readme",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/flowscripter/dynamic-cli-framework.git"
8
+ },
9
+ "license": "MIT",
10
+ "keywords": [
11
+ "bun",
12
+ "plugin",
13
+ "framework",
14
+ "dynamic",
15
+ "import",
16
+ "executable",
17
+ "cli"
18
+ ],
3
19
  "module": "index.ts",
4
20
  "type": "module",
5
- "version": "1.0.8",
21
+ "version": "1.0.10",
6
22
  "publishConfig": {
7
23
  "access": "public"
8
24
  },
package/src/launcher.ts CHANGED
@@ -43,7 +43,8 @@ export async function launchSingleCommandCLI(
43
43
  "configEnabled must be true if keyValueServiceEnabled is true",
44
44
  );
45
45
  }
46
- const validateAllCommands = process.env.CLI_VALIDATE_ALL !== undefined;
46
+ const validateAllCommands =
47
+ process.env.DYNAMIC_CLI_FRAMEWORK_VALIDATE_ALL !== undefined;
47
48
  const cli = new DefaultRuntimeCLI(
48
49
  cliConfig,
49
50
  envVarsEnabled,
@@ -94,7 +95,8 @@ export async function launchMultiCommandCLI(
94
95
  "configEnabled must be true if keyValueServiceEnabled is true",
95
96
  );
96
97
  }
97
- const validateAllCommands = process.env.CLI_VALIDATE_ALL !== undefined;
98
+ const validateAllCommands =
99
+ process.env.DYNAMIC_CLI_FRAMEWORK_VALIDATE_ALL !== undefined;
98
100
  const cli = new DefaultRuntimeCLI(
99
101
  cliConfig,
100
102
  envVarsEnabled,
@@ -1,4 +1,5 @@
1
1
  import process from "node:process";
2
+
2
3
  type LoggerFunction = (
3
4
  message: object | (() => string) | string,
4
5
  ...optionalParams: unknown[]
@@ -12,7 +13,19 @@ export interface Logger {
12
13
  error: LoggerFunction;
13
14
  }
14
15
 
15
- const debugEnabled = process.env["CLI_DEBUG"] !== undefined;
16
+ const LEVEL_PADDINGS: Record<string, string> = {
17
+ TRACE: " ",
18
+ DEBUG: " ",
19
+ INFO: " ",
20
+ WARN: " ",
21
+ ERROR: " ",
22
+ };
23
+
24
+ const debugEnabled = process.env["DYNAMIC_CLI_FRAMEWORK_DEBUG"] !== undefined;
25
+
26
+ let maxLoggerNameLength = 0;
27
+
28
+ const LOGGER_NAME_PADDINGS: Record<string, string> = {};
16
29
 
17
30
  function getDefaultLogger(): Logger {
18
31
  if (debugEnabled) {
@@ -45,33 +58,90 @@ function getDefaultLogger(): Logger {
45
58
 
46
59
  function wrapDefaultLogger(
47
60
  loggerName: string,
61
+ levelPadding: string,
48
62
  loggerFunction: LoggerFunction,
49
63
  ): LoggerFunction {
50
64
  return (message, ...optionalParams) => {
51
65
  if (message instanceof Function) {
52
- loggerFunction(`${loggerName} : ${message()}`, ...optionalParams);
66
+ loggerFunction(
67
+ `${levelPadding} [${loggerName}]${
68
+ LOGGER_NAME_PADDINGS[loggerName]
69
+ } ${message()}`,
70
+ ...optionalParams,
71
+ );
53
72
  return;
54
73
  }
55
74
  if (message instanceof Object) {
56
- (message as { loggerName?: string }).loggerName = loggerName;
57
- loggerFunction(message, ...optionalParams);
75
+ loggerFunction(
76
+ `${levelPadding} [${loggerName}]${LOGGER_NAME_PADDINGS[loggerName]} ${
77
+ JSON.stringify(message)
78
+ }`,
79
+ ...optionalParams,
80
+ );
58
81
  return;
59
82
  }
60
- loggerFunction(`${loggerName} : ${message}`, ...optionalParams);
83
+ loggerFunction(
84
+ `${levelPadding} [${loggerName}]${
85
+ LOGGER_NAME_PADDINGS[loggerName]
86
+ } ${message}`,
87
+ ...optionalParams,
88
+ );
61
89
  };
62
90
  }
63
91
 
92
+ /**
93
+ * Retrieves a logger instance with the specified name. If the name length exceeds the current maximum logger name length,
94
+ * it updates the padding for all existing logger names to ensure consistent formatting.
95
+ *
96
+ * @param loggerName The name of the logger to retrieve.
97
+ * @returns A logger instance with the specified name.
98
+ */
64
99
  export default function getLogger(loggerName: string): Logger {
65
100
  if (globalThis.defaultLogger === undefined) {
66
101
  globalThis.defaultLogger = getDefaultLogger();
67
102
  }
103
+
104
+ if (loggerName.length > maxLoggerNameLength) {
105
+ maxLoggerNameLength = loggerName.length;
106
+
107
+ for (const key of Object.keys(LOGGER_NAME_PADDINGS)) {
108
+ LOGGER_NAME_PADDINGS[key] = " ".repeat(maxLoggerNameLength - key.length);
109
+ }
110
+
111
+ LOGGER_NAME_PADDINGS[loggerName] = "";
112
+ } else if (LOGGER_NAME_PADDINGS[loggerName] === undefined) {
113
+ LOGGER_NAME_PADDINGS[loggerName] = " ".repeat(
114
+ maxLoggerNameLength - loggerName.length,
115
+ );
116
+ }
117
+
68
118
  if (debugEnabled) {
69
119
  return {
70
- trace: () => {},
71
- debug: wrapDefaultLogger(loggerName, globalThis.defaultLogger.debug),
72
- info: wrapDefaultLogger(loggerName, globalThis.defaultLogger.info),
73
- warn: wrapDefaultLogger(loggerName, globalThis.defaultLogger.warn),
74
- error: wrapDefaultLogger(loggerName, globalThis.defaultLogger.error),
120
+ trace: wrapDefaultLogger(
121
+ loggerName,
122
+ LEVEL_PADDINGS.TRACE,
123
+ globalThis.defaultLogger.trace,
124
+ ),
125
+ debug: wrapDefaultLogger(
126
+ loggerName,
127
+ LEVEL_PADDINGS.DEBUG,
128
+ globalThis.defaultLogger.debug,
129
+ ),
130
+ info: wrapDefaultLogger(
131
+ loggerName,
132
+ LEVEL_PADDINGS.INFO,
133
+ globalThis.defaultLogger.info,
134
+ ),
135
+ warn: wrapDefaultLogger(
136
+ loggerName,
137
+ LEVEL_PADDINGS.WARN,
138
+ globalThis.defaultLogger.warn,
139
+ ),
140
+ error: wrapDefaultLogger(
141
+ loggerName,
142
+ LEVEL_PADDINGS.ERROR,
143
+ globalThis.defaultLogger.error,
144
+ ),
75
145
  };
76
146
  }
77
147
  return {
@@ -79,6 +149,10 @@ export default function getLogger(loggerName: string): Logger {
79
149
  debug: () => {},
80
150
  info: () => {},
81
151
  warn: () => {},
82
- error: wrapDefaultLogger(loggerName, globalThis.defaultLogger.error),
152
+ error: wrapDefaultLogger(
153
+ loggerName,
154
+ LEVEL_PADDINGS.ERROR,
155
+ globalThis.defaultLogger.error,
156
+ ),
83
157
  };
84
158
  }