neex 0.6.45 → 0.6.47

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,399 +1,121 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
4
  };
28
5
  Object.defineProperty(exports, "__esModule", { value: true });
29
6
  exports.StartManager = void 0;
30
- // src/start-manager.ts - Production application start manager with monitoring
31
- const watcher_1 = require("./watcher");
32
- const runner_1 = require("./runner");
7
+ // src/start-manager.ts - Production application runner
8
+ const child_process_1 = require("child_process");
9
+ const logger_manager_js_1 = require("./logger-manager.js");
33
10
  const chalk_1 = __importDefault(require("chalk"));
34
11
  const figures_1 = __importDefault(require("figures"));
35
- const logger_process_1 = require("./logger-process");
36
- const os = __importStar(require("os"));
12
+ const fs_1 = __importDefault(require("fs"));
13
+ const path_1 = __importDefault(require("path"));
37
14
  class StartManager {
38
15
  constructor(options) {
39
- this.isRunning = false;
40
- this.restartCount = 0;
41
- this.crashCount = 0;
42
- this.startTime = new Date();
43
- this.processStats = {
44
- memoryUsage: 0,
45
- cpuUsage: 0,
46
- uptime: 0
47
- };
48
- const defaultOptions = {
49
- parallel: false,
50
- printOutput: true,
51
- color: true,
52
- showTiming: true,
53
- prefix: true,
54
- stopOnError: false,
55
- minimalOutput: false,
56
- groupOutput: false,
57
- isServerMode: true,
58
- watch: false,
59
- watchPaths: [],
60
- ignore: [
61
- 'node_modules/**',
62
- '.git/**',
63
- '*.log',
64
- 'src/**',
65
- 'test/**',
66
- 'tests/**',
67
- 'coverage/**',
68
- '.nyc_output/**',
69
- '*.tmp',
70
- '*.temp'
71
- ],
72
- ext: ['js', 'mjs', 'json'],
73
- delay: 1000,
74
- verbose: false,
75
- showInfo: false,
76
- runnerName: 'neex start',
77
- environment: 'production',
78
- maxRestarts: 10,
79
- restartDelay: 3000,
80
- signal: 'SIGTERM',
81
- cluster: false,
82
- clusterInstances: os.cpus().length
83
- };
84
- this.options = {
85
- ...defaultOptions,
86
- ...options
87
- };
88
- }
89
- setupFileWatcher() {
90
- var _a;
91
- if (!this.options.watch || !((_a = this.options.watchPaths) === null || _a === void 0 ? void 0 : _a.length)) {
92
- return;
93
- }
94
- const watchOptions = {
95
- watch: this.options.watchPaths,
96
- ignore: this.options.ignore,
97
- ext: this.options.ext,
98
- delay: this.options.delay,
99
- verbose: this.options.verbose && this.options.showInfo
100
- };
101
- this.fileWatcher = new watcher_1.FileWatcher(watchOptions);
102
- this.fileWatcher.on('change', (event) => {
103
- if (this.options.watch && this.isRunning) {
104
- this.handleFileChange(event);
105
- }
106
- });
107
- }
108
- async handleFileChange(event) {
109
- const prefix = chalk_1.default.cyan(`[${this.options.runnerName}]`);
110
- if (this.options.showInfo) {
111
- logger_process_1.logger.printLine(`${prefix} File changed: ${chalk_1.default.yellow(event.relativePath)}`, 'info');
112
- }
113
- await this.gracefulRestart();
114
- }
115
- generateClusterCommand() {
116
- if (!this.options.cluster) {
117
- return this.options.command;
118
- }
119
- const instances = this.options.clusterInstances || os.cpus().length;
120
- const baseCommand = this.options.command;
121
- // For cluster mode, we'll use a simple approach with PM2-like clustering
122
- // This is a simplified implementation - in real production, you'd use PM2 or similar
123
- return `${baseCommand} --cluster=${instances}`;
124
- }
125
- async startApplication() {
126
- const command = this.generateClusterCommand();
127
- // Set environment variables
128
- const env = {
129
- ...process.env,
130
- NODE_ENV: this.options.environment,
131
- NEEX_START_MODE: 'production',
132
- NEEX_PROCESS_NAME: this.options.processName
133
- };
134
- // Create a modified options object for the runner
135
- const runnerOptions = {
136
- ...this.options,
137
- env,
138
- customPrefix: () => this.options.processName,
139
- // Override some options for production
140
- stopOnError: false,
141
- restartOnFailure: true,
142
- maxRestarts: this.options.maxRestarts,
143
- restartDelay: this.options.restartDelay,
144
- keepAlive: true,
145
- onProcessExit: (code, signal) => {
146
- if (code === 0) {
147
- this.isRunning = false;
148
- return;
149
- }
150
- // Only restart if it's not a graceful shutdown
151
- if (signal !== this.options.signal) {
152
- this.crashCount++;
153
- if (this.shouldRestart()) {
154
- this.handleCrash().catch(console.error);
155
- }
156
- }
157
- }
158
- };
159
- this.runner = new runner_1.Runner(runnerOptions);
160
- try {
161
- const results = await this.runner.run([command]);
162
- this.isRunning = true;
163
- return results;
164
- }
165
- catch (error) {
166
- this.crashCount++;
167
- if (this.options.showInfo) {
168
- logger_process_1.logger.printLine(`Application crashed: ${error.message}`, 'error');
169
- }
170
- // Check if we should restart
171
- if (this.shouldRestart()) {
172
- await this.handleCrash();
173
- }
174
- return [];
175
- }
16
+ this.process = null;
17
+ this.isStopping = false;
18
+ this.options = options;
176
19
  }
177
- shouldRestart() {
178
- if (!this.options.maxRestarts) {
179
- return true;
180
- }
181
- return this.restartCount < this.options.maxRestarts;
182
- }
183
- async handleCrash() {
184
- const prefix = chalk_1.default.cyan(`[${this.options.runnerName}]`);
185
- if (this.options.showInfo) {
186
- logger_process_1.logger.printLine(`${prefix} ${chalk_1.default.red(`${figures_1.default.cross} Application crashed`)}`, 'error');
187
- logger_process_1.logger.printLine(`${prefix} ${chalk_1.default.yellow(`${figures_1.default.warning} Attempting restart in ${this.options.restartDelay}ms...`)}`, 'info');
188
- }
189
- // Wait before restarting
190
- await new Promise(resolve => setTimeout(resolve, this.options.restartDelay));
191
- await this.gracefulRestart();
192
- }
193
- collectProcessStats() {
194
- if (!this.options.showInfo) {
195
- return;
196
- }
197
- const memUsage = process.memoryUsage();
198
- this.processStats.memoryUsage = Math.round(memUsage.heapUsed / 1024 / 1024);
199
- this.processStats.uptime = Math.floor((Date.now() - this.startTime.getTime()) / 1000);
200
- // Simple CPU usage approximation
201
- const cpuUsage = process.cpuUsage();
202
- this.processStats.cpuUsage = Math.round(((cpuUsage.user + cpuUsage.system) / 1000000) * 100) / 100;
203
- }
204
- printStartBanner() {
20
+ async startProcess() {
205
21
  var _a, _b;
206
- if (!this.options.showInfo) {
22
+ if (this.process) {
207
23
  return;
208
24
  }
209
- const prefix = chalk_1.default.cyan(`[${this.options.runnerName}]`);
210
- const uptime = Math.floor((Date.now() - this.startTime.getTime()) / 1000);
211
- const uptimeStr = this.formatUptime(uptime);
212
- console.log('\n' + chalk_1.default.bgGreen.black(` ${(_a = this.options.runnerName) === null || _a === void 0 ? void 0 : _a.toUpperCase()} - ${(_b = this.options.environment) === null || _b === void 0 ? void 0 : _b.toUpperCase()} MODE `) + '\n');
213
- console.log(`${prefix} ${chalk_1.default.green(`${figures_1.default.tick} ${this.options.processName} started successfully`)}`);
214
- console.log(`${prefix} ${chalk_1.default.blue(`${figures_1.default.info} Runtime: ${this.options.runtime}`)}`);
215
- console.log(`${prefix} ${chalk_1.default.blue(`${figures_1.default.info} Environment: ${this.options.environment}`)}`);
216
- console.log(`${prefix} ${chalk_1.default.blue(`${figures_1.default.info} Uptime: ${uptimeStr}`)}`);
217
- if (this.options.cluster) {
218
- console.log(`${prefix} ${chalk_1.default.blue(`${figures_1.default.info} Cluster mode: ${this.options.clusterInstances} instances`)}`);
219
- }
220
- if (this.options.watch) {
221
- console.log(`${prefix} ${chalk_1.default.blue(`${figures_1.default.info} Hot reload: enabled`)}`);
222
- }
223
- if (this.restartCount > 0) {
224
- console.log(`${prefix} ${chalk_1.default.yellow(`${figures_1.default.warning} Restarts: ${this.restartCount}`)}`);
225
- }
226
- if (this.crashCount > 0) {
227
- console.log(`${prefix} ${chalk_1.default.red(`${figures_1.default.cross} Crashes: ${this.crashCount}`)}`);
228
- }
229
- // Show process stats
230
- this.collectProcessStats();
231
- console.log(`${prefix} ${chalk_1.default.blue(`${figures_1.default.info} Memory: ${this.processStats.memoryUsage}MB`)}`);
232
- if (this.options.memoryLimit) {
233
- const memoryPercent = Math.round((this.processStats.memoryUsage / this.options.memoryLimit) * 100);
234
- console.log(`${prefix} ${chalk_1.default.blue(`${figures_1.default.info} Memory usage: ${memoryPercent}%`)}`);
235
- }
236
- console.log('');
237
- }
238
- formatUptime(seconds) {
239
- if (seconds < 60) {
240
- return `${seconds}s`;
241
- }
242
- else if (seconds < 3600) {
243
- const minutes = Math.floor(seconds / 60);
244
- const remainingSeconds = seconds % 60;
245
- return `${minutes}m ${remainingSeconds}s`;
246
- }
247
- else {
248
- const hours = Math.floor(seconds / 3600);
249
- const minutes = Math.floor((seconds % 3600) / 60);
250
- return `${hours}h ${minutes}m`;
251
- }
252
- }
253
- monitorResources() {
254
- if (!this.options.showInfo) {
255
- return;
25
+ const nodeArgs = this.options.nodeArgs || [];
26
+ const args = [...nodeArgs, this.options.entry];
27
+ if (this.options.verbose) {
28
+ logger_manager_js_1.loggerManager.printLine(`Executing: node ${args.join(' ')}`, 'info');
29
+ }
30
+ this.process = (0, child_process_1.spawn)('node', args, {
31
+ stdio: ['ignore', 'pipe', 'pipe'],
32
+ shell: false,
33
+ env: {
34
+ ...process.env,
35
+ NODE_ENV: 'production',
36
+ FORCE_COLOR: this.options.color ? '1' : '0'
37
+ },
38
+ detached: true
39
+ });
40
+ const appName = this.options.name || path_1.default.basename(this.options.entry);
41
+ if (!this.options.quiet) {
42
+ logger_manager_js_1.loggerManager.printLine(`${chalk_1.default.green(figures_1.default.play)} Starting ${chalk_1.default.cyan(appName)} in production mode...`, 'info');
256
43
  }
257
- const checkInterval = setInterval(() => {
258
- if (!this.isRunning) {
259
- clearInterval(checkInterval);
260
- return;
44
+ (_a = this.process.stdout) === null || _a === void 0 ? void 0 : _a.on('data', (data) => {
45
+ if (!this.options.quiet) {
46
+ process.stdout.write(data);
261
47
  }
262
- this.collectProcessStats();
263
- // Check memory limit
264
- if (this.options.memoryLimit && this.processStats.memoryUsage > this.options.memoryLimit) {
265
- const prefix = chalk_1.default.cyan(`[${this.options.runnerName}]`);
266
- logger_process_1.logger.printLine(`${prefix} ${chalk_1.default.red(`${figures_1.default.warning} Memory limit exceeded: ${this.processStats.memoryUsage}MB > ${this.options.memoryLimit}MB`)}`, 'warn');
48
+ });
49
+ (_b = this.process.stderr) === null || _b === void 0 ? void 0 : _b.on('data', (data) => {
50
+ if (!this.options.quiet) {
51
+ process.stderr.write(data);
267
52
  }
268
- // Log stats every 30 seconds in verbose mode
269
- if (this.options.verbose && this.processStats.uptime % 30 === 0) {
270
- const prefix = chalk_1.default.cyan(`[${this.options.runnerName}]`);
271
- logger_process_1.logger.printLine(`${prefix} ${chalk_1.default.gray(`Stats: ${this.processStats.memoryUsage}MB memory, ${this.formatUptime(this.processStats.uptime)} uptime`)}`, 'info');
53
+ });
54
+ this.process.on('error', (error) => {
55
+ logger_manager_js_1.loggerManager.printLine(`Application error: ${error.message}`, 'error');
56
+ });
57
+ this.process.on('exit', (code) => {
58
+ this.process = null;
59
+ if (!this.isStopping) {
60
+ logger_manager_js_1.loggerManager.printLine(`${chalk_1.default.red(figures_1.default.cross)} Application ${appName} exited with code ${code}`, 'error');
61
+ if (this.options.watch) {
62
+ logger_manager_js_1.loggerManager.printLine(`${chalk_1.default.yellow(figures_1.default.arrowRight)} Restarting application...`, 'info');
63
+ this.startProcess();
64
+ }
272
65
  }
273
- }, 1000);
66
+ });
274
67
  }
275
68
  async start() {
276
- this.isRunning = true;
277
- this.startTime = new Date();
278
- // Setup file watcher if enabled
279
- if (this.options.watch) {
280
- this.setupFileWatcher();
281
- }
282
- // Print start banner
283
- this.printStartBanner();
284
- // Start file watcher if enabled
285
- if (this.fileWatcher) {
286
- await this.fileWatcher.start();
287
- }
288
- // Start resource monitoring
289
- this.monitorResources();
290
- // Start application
291
- const prefix = chalk_1.default.cyan(`[${this.options.runnerName}]`);
292
- if (this.options.showInfo) {
293
- logger_process_1.logger.printLine(`${prefix} Starting ${this.options.processName}...`, 'info');
294
- }
295
- await this.startApplication();
296
- // Set up graceful shutdown
297
- this.setupGracefulShutdown();
298
- if (this.options.showInfo) {
299
- logger_process_1.logger.printLine(`${prefix} ${this.options.processName} is running in ${this.options.environment} mode`, 'info');
300
- if (this.options.watch) {
301
- logger_process_1.logger.printLine(`${prefix} Watching for changes...`, 'info');
302
- }
303
- logger_process_1.logger.printLine(`${prefix} Press ${chalk_1.default.cyan('Ctrl+C')} to stop`, 'info');
304
- }
305
- }
306
- async gracefulRestart() {
307
- const prefix = chalk_1.default.cyan(`[${this.options.runnerName}]`);
308
- if (!this.isRunning) {
309
- return;
310
- }
311
- if (!this.shouldRestart()) {
312
- if (this.options.showInfo) {
313
- logger_process_1.logger.printLine(`${prefix} ${chalk_1.default.red(`${figures_1.default.cross} Max restarts reached (${this.options.maxRestarts}). Stopping application.`)}`, 'error');
314
- }
315
- await this.stop();
316
- return;
317
- }
318
- if (this.options.showInfo) {
319
- logger_process_1.logger.printLine(`${prefix} ${chalk_1.default.yellow(`${figures_1.default.warning} Gracefully restarting ${this.options.processName}...`)}`, 'info');
320
- }
321
- this.restartCount++;
322
- this.lastRestartTime = new Date();
323
- // Stop current processes gracefully
324
- if (this.runner) {
325
- this.runner.cleanup(this.options.signal);
69
+ if (!fs_1.default.existsSync(this.options.entry)) {
70
+ throw new Error(`Entry file not found: ${this.options.entry}`);
326
71
  }
327
- // Wait for graceful shutdown
328
- await new Promise(resolve => setTimeout(resolve, 1000));
329
- // Print restart banner
330
- this.printStartBanner();
331
- // Start application again
332
- await this.startApplication();
333
- if (this.options.showInfo) {
334
- logger_process_1.logger.printLine(`${prefix} ${chalk_1.default.green(`${figures_1.default.tick} ${this.options.processName} restarted successfully`)}`, 'info');
72
+ await this.startProcess();
73
+ if (!this.options.quiet) {
74
+ logger_manager_js_1.loggerManager.printLine(`${chalk_1.default.green(figures_1.default.tick)} Application is running.`, 'info');
335
75
  }
336
76
  }
337
77
  async stop() {
338
- const prefix = chalk_1.default.cyan(`[${this.options.runnerName}]`);
339
- if (!this.isRunning) {
78
+ this.isStopping = true;
79
+ const proc = this.process;
80
+ if (!proc) {
340
81
  return;
341
82
  }
342
- if (this.options.showInfo) {
343
- logger_process_1.logger.printLine(`${prefix} ${chalk_1.default.yellow(`${figures_1.default.warning} Stopping ${this.options.processName}...`)}`, 'info');
344
- }
345
- this.isRunning = false;
346
- // Stop file watcher
347
- if (this.fileWatcher) {
348
- this.fileWatcher.stop();
349
- }
350
- // Stop application gracefully
351
- if (this.runner) {
352
- this.runner.cleanup(this.options.signal);
353
- }
354
- // Calculate final stats
355
- const uptime = Math.floor((Date.now() - this.startTime.getTime()) / 1000);
356
- const uptimeStr = this.formatUptime(uptime);
357
- if (this.options.showInfo) {
358
- logger_process_1.logger.printLine(`${prefix} ${chalk_1.default.green(`${figures_1.default.tick} ${this.options.processName} stopped successfully`)}`, 'info');
359
- logger_process_1.logger.printLine(`${prefix} Final stats: ${uptimeStr} uptime, ${this.restartCount} restarts, ${this.crashCount} crashes`, 'info');
360
- }
361
- }
362
- setupGracefulShutdown() {
363
- const handleSignal = (signal) => {
364
- if (this.options.showInfo) {
365
- console.log(`\n${chalk_1.default.yellow(`${figures_1.default.warning} Received ${signal}. Gracefully shutting down ${this.options.processName}...`)}`);
366
- }
367
- this.stop().then(() => {
368
- process.exit(0);
83
+ this.process = null;
84
+ return new Promise((resolve) => {
85
+ const appName = this.options.name || path_1.default.basename(this.options.entry);
86
+ logger_manager_js_1.loggerManager.printLine(`${chalk_1.default.yellow(figures_1.default.warning)} Stopping application ${appName}...`, 'info');
87
+ proc.on('exit', () => {
88
+ logger_manager_js_1.loggerManager.printLine(`${chalk_1.default.yellow(figures_1.default.square)} Application stopped.`, 'info');
89
+ resolve();
369
90
  });
370
- };
371
- process.on('SIGINT', () => handleSignal('SIGINT'));
372
- process.on('SIGTERM', () => handleSignal('SIGTERM'));
373
- process.on('SIGQUIT', () => handleSignal('SIGQUIT'));
374
- }
375
- isActive() {
376
- return this.isRunning;
377
- }
378
- getUptime() {
379
- return Math.floor((Date.now() - this.startTime.getTime()) / 1000);
380
- }
381
- getRestartCount() {
382
- return this.restartCount;
383
- }
384
- getCrashCount() {
385
- return this.crashCount;
386
- }
387
- getProcessStats() {
388
- this.collectProcessStats();
389
- return { ...this.processStats };
390
- }
391
- getLastRestartTime() {
392
- return this.lastRestartTime;
393
- }
394
- getWatchedFiles() {
395
- var _a;
396
- return ((_a = this.fileWatcher) === null || _a === void 0 ? void 0 : _a.getWatchedFiles()) || [];
91
+ proc.on('error', () => {
92
+ // Handle errors during shutdown, e.g., if the process is already gone
93
+ logger_manager_js_1.loggerManager.printLine(`${chalk_1.default.yellow(figures_1.default.square)} Application stopped.`, 'info');
94
+ resolve();
95
+ });
96
+ try {
97
+ if (proc.pid) {
98
+ const pid = proc.pid;
99
+ // Kill the entire process group
100
+ process.kill(-pid, 'SIGTERM');
101
+ // Set a timeout to force kill if it doesn't terminate gracefully
102
+ setTimeout(() => {
103
+ if (!proc.killed) {
104
+ try {
105
+ process.kill(-pid, 'SIGKILL');
106
+ }
107
+ catch (e) {
108
+ // Ignore errors if the process is already gone
109
+ }
110
+ }
111
+ }, 5000).unref(); // .unref() allows the main process to exit if this is the only thing running
112
+ }
113
+ }
114
+ catch (e) {
115
+ // This can happen if the process is already dead
116
+ resolve();
117
+ }
118
+ });
397
119
  }
398
120
  }
399
121
  exports.StartManager = StartManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neex",
3
- "version": "0.6.45",
3
+ "version": "0.6.47",
4
4
  "description": "The Modern Build System for Polyrepo-in-Monorepo Architecture",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",
@@ -31,10 +31,12 @@
31
31
  "license": "MIT",
32
32
  "dependencies": {
33
33
  "chalk": "^4.1.2",
34
+ "chokidar": "^4.0.3",
34
35
  "commander": "^9.4.0",
35
36
  "figlet": "^1.8.1",
36
37
  "figures": "^3.2.0",
37
38
  "gradient-string": "^3.0.0",
39
+ "lodash": "^4.17.21",
38
40
  "npm-run-path": "^4.0.1",
39
41
  "p-map": "^4.0.0",
40
42
  "string-width": "^4.2.3"
@@ -42,6 +44,7 @@
42
44
  "devDependencies": {
43
45
  "@types/figlet": "^1.7.0",
44
46
  "@types/jest": "^29.2.3",
47
+ "@types/lodash": "^4.17.20",
45
48
  "@types/node": "^18.11.9",
46
49
  "jest": "^29.3.1",
47
50
  "ts-jest": "^29.0.3",