@friggframework/devtools 2.0.0--canary.395.495dc7d.0 → 2.0.0--canary.395.04851d8.0

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.
@@ -1,170 +0,0 @@
1
- const { Command } = require('commander');
2
-
3
- /**
4
- * CommandTester - Utility class for testing CLI commands
5
- * Provides a fluent interface for setting up mocks and executing commands
6
- */
7
- class CommandTester {
8
- constructor(commandDefinition) {
9
- this.commandDefinition = commandDefinition;
10
- this.mocks = new Map();
11
- this.originalEnv = process.env;
12
- this.capturedLogs = {
13
- info: [],
14
- error: [],
15
- debug: [],
16
- warn: []
17
- };
18
- }
19
-
20
- /**
21
- * Set up a mock for a module
22
- * @param {string} modulePath - Path to the module to mock
23
- * @param {object} implementation - Mock implementation
24
- * @returns {CommandTester} - Fluent interface
25
- */
26
- mock(modulePath, implementation) {
27
- this.mocks.set(modulePath, implementation);
28
- return this;
29
- }
30
-
31
- /**
32
- * Set environment variables for the test
33
- * @param {object} env - Environment variables to set
34
- * @returns {CommandTester} - Fluent interface
35
- */
36
- withEnv(env) {
37
- process.env = { ...process.env, ...env };
38
- return this;
39
- }
40
-
41
- /**
42
- * Capture console output during test execution
43
- * @returns {CommandTester} - Fluent interface
44
- */
45
- captureOutput() {
46
- const originalConsole = { ...console };
47
-
48
- console.log = (...args) => {
49
- this.capturedLogs.info.push(args.join(' '));
50
- originalConsole.log(...args);
51
- };
52
-
53
- console.error = (...args) => {
54
- this.capturedLogs.error.push(args.join(' '));
55
- originalConsole.error(...args);
56
- };
57
-
58
- console.warn = (...args) => {
59
- this.capturedLogs.warn.push(args.join(' '));
60
- originalConsole.warn(...args);
61
- };
62
-
63
- console.debug = (...args) => {
64
- this.capturedLogs.debug.push(args.join(' '));
65
- originalConsole.debug(...args);
66
- };
67
-
68
- return this;
69
- }
70
-
71
- /**
72
- * Execute the command with given arguments
73
- * @param {string[]} args - Command arguments
74
- * @param {object} options - Command options
75
- * @returns {Promise<object>} - Execution result
76
- */
77
- async execute(args = [], options = {}) {
78
- // Set up mocks
79
- for (const [path, impl] of this.mocks) {
80
- jest.mock(path, () => impl, { virtual: true });
81
- }
82
-
83
- try {
84
- const program = new Command();
85
-
86
- // Set up the command
87
- const cmd = program
88
- .command(this.commandDefinition.name)
89
- .description(this.commandDefinition.description);
90
-
91
- // Add options if defined
92
- if (this.commandDefinition.options) {
93
- this.commandDefinition.options.forEach(option => {
94
- cmd.option(option.flags, option.description, option.defaultValue);
95
- });
96
- }
97
-
98
- // Add action
99
- cmd.action(this.commandDefinition.action);
100
-
101
- // Mock process.exit to prevent actual exit
102
- const originalExit = process.exit;
103
- let exitCode = 0;
104
- process.exit = (code) => {
105
- exitCode = code;
106
- throw new Error(`Process exited with code ${code}`);
107
- };
108
-
109
- try {
110
- await program.parseAsync(['node', 'cli', ...args]);
111
-
112
- return {
113
- success: true,
114
- exitCode: 0,
115
- logs: this.capturedLogs,
116
- args,
117
- options
118
- };
119
- } catch (error) {
120
- if (error.message.includes('Process exited with code')) {
121
- return {
122
- success: false,
123
- exitCode,
124
- error: error.message,
125
- logs: this.capturedLogs,
126
- args,
127
- options
128
- };
129
- }
130
- throw error;
131
- } finally {
132
- process.exit = originalExit;
133
- }
134
- } finally {
135
- // Clean up mocks
136
- for (const [path] of this.mocks) {
137
- jest.unmock(path);
138
- }
139
-
140
- // Restore environment
141
- process.env = this.originalEnv;
142
- }
143
- }
144
-
145
- /**
146
- * Get captured logs
147
- * @returns {object} - Captured logs by type
148
- */
149
- getLogs() {
150
- return this.capturedLogs;
151
- }
152
-
153
- /**
154
- * Reset the tester state
155
- * @returns {CommandTester} - Fluent interface
156
- */
157
- reset() {
158
- this.mocks.clear();
159
- this.capturedLogs = {
160
- info: [],
161
- error: [],
162
- debug: [],
163
- warn: []
164
- };
165
- process.env = this.originalEnv;
166
- return this;
167
- }
168
- }
169
-
170
- module.exports = { CommandTester };