slicejs-cli 2.8.5 → 2.9.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,369 +1,369 @@
1
- import fs from 'fs-extra';
2
- import path from 'path';
3
- import { fileURLToPath } from 'url';
4
- import { createServer } from 'net';
5
- import chalk from 'chalk';
6
- import Table from 'cli-table3';
7
- import Print from '../Print.js';
8
- import inquirer from 'inquirer';
9
- import { exec } from 'child_process';
10
- import { promisify } from 'util';
11
- import { getProjectRoot, getSrcPath, getApiPath, getConfigPath, getPath } from '../utils/PathHelper.js';
12
- import updateManager from '../utils/updateManager.js';
13
-
14
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
15
-
16
- /**
17
- * Verifica la versión de Node.js
18
- */
19
- async function checkNodeVersion() {
20
- const currentVersion = process.version;
21
- const majorVersion = parseInt(currentVersion.slice(1).split('.')[0]);
22
- const required = 20;
23
-
24
- if (majorVersion >= required) {
25
- return {
26
- pass: true,
27
- message: `Node.js version: ${currentVersion} (required: >= v${required})`
28
- };
29
- } else {
30
- return {
31
- pass: false,
32
- message: `Node.js version: ${currentVersion} (required: >= v${required})`,
33
- suggestion: `Update Node.js to v${required} or higher`
34
- };
35
- }
36
- }
37
-
38
- /**
39
- * Verifica la estructura de directorios
40
- */
41
- async function checkDirectoryStructure() {
42
- const srcPath = getSrcPath(import.meta.url);
43
- const apiPath = getApiPath(import.meta.url);
44
-
45
- const srcExists = await fs.pathExists(srcPath);
46
- const apiExists = await fs.pathExists(apiPath);
47
-
48
- if (srcExists && apiExists) {
49
- return {
50
- pass: true,
51
- message: 'Project structure (src/ and api/) exists'
52
- };
53
- } else {
54
- const missing = [];
55
- if (!srcExists) missing.push('src/');
56
- if (!apiExists) missing.push('api/');
57
-
58
- return {
59
- pass: false,
60
- message: `Missing directories: ${missing.join(', ')}`,
61
- suggestion: 'Run "slice init" to initialize your project'
62
- };
63
- }
64
- }
65
-
66
- /**
67
- * Verifica sliceConfig.json
68
- */
69
- async function checkConfig() {
70
- const configPath = getConfigPath(import.meta.url);
71
-
72
- if (!await fs.pathExists(configPath)) {
73
- return {
74
- pass: false,
75
- message: 'sliceConfig.json not found',
76
- suggestion: 'Run "slice init" to create configuration'
77
- };
78
- }
79
-
80
- try {
81
- const config = await fs.readJson(configPath);
82
-
83
- if (!config.paths || !config.paths.components) {
84
- return {
85
- pass: false,
86
- message: 'sliceConfig.json is invalid (missing paths.components)',
87
- suggestion: 'Check your configuration file'
88
- };
89
- }
90
-
91
- return {
92
- pass: true,
93
- message: 'sliceConfig.json is valid'
94
- };
95
- } catch (error) {
96
- return {
97
- pass: false,
98
- message: `sliceConfig.json is invalid JSON: ${error.message}`,
99
- suggestion: 'Fix JSON syntax errors in sliceConfig.json'
100
- };
101
- }
102
- }
103
-
104
- /**
105
- * Verifica disponibilidad del puerto
106
- */
107
- async function checkPort() {
108
- const configPath = getConfigPath(import.meta.url);
109
- let port = 3000;
110
-
111
- try {
112
- if (await fs.pathExists(configPath)) {
113
- const config = await fs.readJson(configPath);
114
- port = config.server?.port || 3000;
115
- }
116
- } catch { }
117
-
118
- return new Promise((resolve) => {
119
- const server = createServer();
120
-
121
- server.once('error', (err) => {
122
- if (err.code === 'EADDRINUSE') {
123
- resolve({
124
- warn: true,
125
- message: `Port ${port} is already in use`,
126
- suggestion: `Stop the process using port ${port} or use: slice dev -p <other-port>`
127
- });
128
- } else {
129
- resolve({
130
- pass: true,
131
- message: `Port ${port} is available`
132
- });
133
- }
134
- });
135
-
136
- server.once('listening', () => {
137
- server.close();
138
- resolve({
139
- pass: true,
140
- message: `Port ${port} is available`
141
- });
142
- });
143
-
144
- server.listen(port);
145
- });
146
- }
147
-
148
- /**
149
- * Verifica dependencias en package.json
150
- */
151
- async function checkDependencies() {
152
- const projectRoot = getProjectRoot(import.meta.url);
153
- const packagePath = path.join(projectRoot, 'package.json');
154
-
155
- if (!await fs.pathExists(packagePath)) {
156
- return {
157
- warn: true,
158
- message: 'package.json not found',
159
- suggestion: 'Run "npm init" to create package.json'
160
- };
161
- }
162
-
163
- try {
164
- const pkg = await fs.readJson(packagePath);
165
- const hasFrameworkDep = pkg.dependencies?.['slicejs-web-framework'] || pkg.devDependencies?.['slicejs-web-framework'];
166
- const frameworkNodePath = path.join(projectRoot, 'node_modules', 'slicejs-web-framework', 'package.json');
167
- const hasFrameworkNode = await fs.pathExists(frameworkNodePath);
168
- const hasFramework = !!(hasFrameworkDep || hasFrameworkNode);
169
-
170
- if (hasFramework) {
171
- return {
172
- pass: true,
173
- message: 'Required framework dependency is installed'
174
- };
175
- } else {
176
- const missing = ['slicejs-web-framework'];
177
-
178
- return {
179
- warn: true,
180
- message: `Missing dependencies: ${missing.join(', ')}`,
181
- suggestion: missing.includes('slicejs-web-framework')
182
- ? 'Run "npm install slicejs-web-framework@latest" in your project'
183
- : 'Run "npm install -D slicejs-cli@latest" in your project',
184
- missing
185
- };
186
- }
187
- } catch (error) {
188
- return {
189
- pass: false,
190
- message: `package.json is invalid: ${error.message}`,
191
- suggestion: 'Fix JSON syntax errors in package.json'
192
- };
193
- }
194
- }
195
-
196
- /**
197
- * Verifica integridad de componentes
198
- */
199
- async function checkComponents() {
200
- const configPath = getConfigPath(import.meta.url);
201
- const projectRoot = getProjectRoot(import.meta.url);
202
-
203
- if (!await fs.pathExists(configPath)) {
204
- return {
205
- warn: true,
206
- message: 'Cannot check components (no config)',
207
- suggestion: 'Run "slice init" first'
208
- };
209
- }
210
-
211
- try {
212
- const config = await fs.readJson(configPath);
213
- const componentPaths = config.paths?.components || {};
214
-
215
- let totalComponents = 0;
216
- let componentIssues = 0;
217
-
218
- for (const [category, { path: compPath }] of Object.entries(componentPaths)) {
219
- const fullPath = getSrcPath(import.meta.url, compPath);
220
-
221
- if (await fs.pathExists(fullPath)) {
222
- const items = await fs.readdir(fullPath);
223
-
224
- for (const item of items) {
225
- const itemPath = path.join(fullPath, item);
226
- const stat = await fs.stat(itemPath);
227
-
228
- if (stat.isDirectory()) {
229
- totalComponents++;
230
-
231
- // Verificar archivos JS
232
- const jsFile = path.join(itemPath, `${item}.js`);
233
- if (!await fs.pathExists(jsFile)) {
234
- componentIssues++;
235
- }
236
- }
237
- }
238
- }
239
- }
240
-
241
- if (componentIssues === 0) {
242
- return {
243
- pass: true,
244
- message: `${totalComponents} components checked, all OK`
245
- };
246
- } else {
247
- return {
248
- warn: true,
249
- message: `${componentIssues} component(s) have missing files`,
250
- suggestion: 'Check your component directories'
251
- };
252
- }
253
- } catch (error) {
254
- return {
255
- warn: true,
256
- message: `Cannot check components: ${error.message}`,
257
- suggestion: 'Verify your project structure'
258
- };
259
- }
260
- }
261
-
262
- /**
263
- * Comando principal de diagnóstico
264
- */
265
- export default async function runDiagnostics() {
266
- Print.newLine();
267
- Print.title('🔍 Running Slice.js Diagnostics...');
268
- Print.newLine();
269
-
270
- const checks = [
271
- { name: 'Node.js Version', fn: checkNodeVersion },
272
- { name: 'Project Structure', fn: checkDirectoryStructure },
273
- { name: 'Configuration', fn: checkConfig },
274
- { name: 'Port Availability', fn: checkPort },
275
- { name: 'Dependencies', fn: checkDependencies },
276
- { name: 'Components', fn: checkComponents }
277
- ];
278
-
279
- const results = [];
280
-
281
- for (const check of checks) {
282
- const result = await check.fn();
283
- results.push({ ...result, name: check.name });
284
- }
285
-
286
- // Crear tabla de resultados
287
- const table = new Table({
288
- head: [chalk.cyan.bold('Check'), chalk.cyan.bold('Status'), chalk.cyan.bold('Details')],
289
- colWidths: [25, 10, 55],
290
- style: {
291
- head: [],
292
- border: ['gray']
293
- },
294
- wordWrap: true
295
- });
296
-
297
- results.forEach(result => {
298
- let status;
299
- if (result.pass) {
300
- status = chalk.green('✅ PASS');
301
- } else if (result.warn) {
302
- status = chalk.yellow('⚠️ WARN');
303
- } else {
304
- status = chalk.red('❌ FAIL');
305
- }
306
-
307
- const details = result.suggestion
308
- ? `${result.message}\n${chalk.gray('→ ' + result.suggestion)}`
309
- : result.message;
310
-
311
- table.push([result.name, status, details]);
312
- });
313
-
314
- console.log(table.toString());
315
-
316
- // Resumen
317
- const issues = results.filter(r => !r.pass && !r.warn).length;
318
- const warnings = results.filter(r => r.warn).length;
319
- const passed = results.filter(r => r.pass).length;
320
-
321
- Print.newLine();
322
- Print.separator();
323
-
324
- const depsResult = results.find(r => r.name === 'Dependencies');
325
- if (depsResult && depsResult.warn && Array.isArray(depsResult.missing) && depsResult.missing.length > 0) {
326
- const projectRoot = getProjectRoot(import.meta.url);
327
- const execAsync = promisify(exec);
328
- const { confirmInstall } = await inquirer.prompt([
329
- {
330
- type: 'confirm',
331
- name: 'confirmInstall',
332
- message: `Install missing dependencies in this project now? (${depsResult.missing.join(', ')})`,
333
- default: true
334
- }
335
- ]);
336
- if (confirmInstall) {
337
- for (const pkg of depsResult.missing) {
338
- try {
339
- const cmd = 'npm install slicejs-web-framework@latest';
340
- Print.info(`Installing ${pkg}...`);
341
- await execAsync(cmd, { cwd: projectRoot });
342
- Print.success(`${pkg} installed`);
343
- } catch (e) {
344
- Print.error(`Installing ${pkg}: ${e.message}`);
345
- }
346
- }
347
- }
348
- }
349
-
350
- if (issues === 0 && warnings === 0) {
351
- Print.success('All checks passed! 🎉');
352
- Print.info('Your Slice.js project is correctly configured');
353
- } else {
354
- console.log(chalk.bold('📊 Summary:'));
355
- console.log(chalk.green(` ✅ Passed: ${passed}`));
356
- if (warnings > 0) console.log(chalk.yellow(` ⚠️ Warnings: ${warnings}`));
357
- if (issues > 0) console.log(chalk.red(` ❌ Issues: ${issues}`));
358
-
359
- Print.newLine();
360
-
361
- if (issues > 0) {
362
- Print.warning('Fix the issues above to ensure proper functionality');
363
- } else {
364
- Print.info('Warnings are non-critical but should be addressed');
365
- }
366
- }
367
-
368
- Print.separator();
369
- }
1
+ import fs from 'fs-extra';
2
+ import path from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import { createServer } from 'net';
5
+ import chalk from 'chalk';
6
+ import Table from 'cli-table3';
7
+ import Print from '../Print.js';
8
+ import inquirer from 'inquirer';
9
+ import { exec } from 'child_process';
10
+ import { promisify } from 'util';
11
+ import { getProjectRoot, getSrcPath, getApiPath, getConfigPath, getPath } from '../utils/PathHelper.js';
12
+ import updateManager from '../utils/updateManager.js';
13
+
14
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
15
+
16
+ /**
17
+ * Verifica la versión de Node.js
18
+ */
19
+ async function checkNodeVersion() {
20
+ const currentVersion = process.version;
21
+ const majorVersion = parseInt(currentVersion.slice(1).split('.')[0]);
22
+ const required = 20;
23
+
24
+ if (majorVersion >= required) {
25
+ return {
26
+ pass: true,
27
+ message: `Node.js version: ${currentVersion} (required: >= v${required})`
28
+ };
29
+ } else {
30
+ return {
31
+ pass: false,
32
+ message: `Node.js version: ${currentVersion} (required: >= v${required})`,
33
+ suggestion: `Update Node.js to v${required} or higher`
34
+ };
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Verifica la estructura de directorios
40
+ */
41
+ async function checkDirectoryStructure() {
42
+ const srcPath = getSrcPath(import.meta.url);
43
+ const apiPath = getApiPath(import.meta.url);
44
+
45
+ const srcExists = await fs.pathExists(srcPath);
46
+ const apiExists = await fs.pathExists(apiPath);
47
+
48
+ if (srcExists && apiExists) {
49
+ return {
50
+ pass: true,
51
+ message: 'Project structure (src/ and api/) exists'
52
+ };
53
+ } else {
54
+ const missing = [];
55
+ if (!srcExists) missing.push('src/');
56
+ if (!apiExists) missing.push('api/');
57
+
58
+ return {
59
+ pass: false,
60
+ message: `Missing directories: ${missing.join(', ')}`,
61
+ suggestion: 'Run "slice init" to initialize your project'
62
+ };
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Verifica sliceConfig.json
68
+ */
69
+ async function checkConfig() {
70
+ const configPath = getConfigPath(import.meta.url);
71
+
72
+ if (!await fs.pathExists(configPath)) {
73
+ return {
74
+ pass: false,
75
+ message: 'sliceConfig.json not found',
76
+ suggestion: 'Run "slice init" to create configuration'
77
+ };
78
+ }
79
+
80
+ try {
81
+ const config = await fs.readJson(configPath);
82
+
83
+ if (!config.paths || !config.paths.components) {
84
+ return {
85
+ pass: false,
86
+ message: 'sliceConfig.json is invalid (missing paths.components)',
87
+ suggestion: 'Check your configuration file'
88
+ };
89
+ }
90
+
91
+ return {
92
+ pass: true,
93
+ message: 'sliceConfig.json is valid'
94
+ };
95
+ } catch (error) {
96
+ return {
97
+ pass: false,
98
+ message: `sliceConfig.json is invalid JSON: ${error.message}`,
99
+ suggestion: 'Fix JSON syntax errors in sliceConfig.json'
100
+ };
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Verifica disponibilidad del puerto
106
+ */
107
+ async function checkPort() {
108
+ const configPath = getConfigPath(import.meta.url);
109
+ let port = 3000;
110
+
111
+ try {
112
+ if (await fs.pathExists(configPath)) {
113
+ const config = await fs.readJson(configPath);
114
+ port = config.server?.port || 3000;
115
+ }
116
+ } catch { }
117
+
118
+ return new Promise((resolve) => {
119
+ const server = createServer();
120
+
121
+ server.once('error', (err) => {
122
+ if (err.code === 'EADDRINUSE') {
123
+ resolve({
124
+ warn: true,
125
+ message: `Port ${port} is already in use`,
126
+ suggestion: `Stop the process using port ${port} or use: slice dev -p <other-port>`
127
+ });
128
+ } else {
129
+ resolve({
130
+ pass: true,
131
+ message: `Port ${port} is available`
132
+ });
133
+ }
134
+ });
135
+
136
+ server.once('listening', () => {
137
+ server.close();
138
+ resolve({
139
+ pass: true,
140
+ message: `Port ${port} is available`
141
+ });
142
+ });
143
+
144
+ server.listen(port);
145
+ });
146
+ }
147
+
148
+ /**
149
+ * Verifica dependencias en package.json
150
+ */
151
+ async function checkDependencies() {
152
+ const projectRoot = getProjectRoot(import.meta.url);
153
+ const packagePath = path.join(projectRoot, 'package.json');
154
+
155
+ if (!await fs.pathExists(packagePath)) {
156
+ return {
157
+ warn: true,
158
+ message: 'package.json not found',
159
+ suggestion: 'Run "npm init" to create package.json'
160
+ };
161
+ }
162
+
163
+ try {
164
+ const pkg = await fs.readJson(packagePath);
165
+ const hasFrameworkDep = pkg.dependencies?.['slicejs-web-framework'] || pkg.devDependencies?.['slicejs-web-framework'];
166
+ const frameworkNodePath = path.join(projectRoot, 'node_modules', 'slicejs-web-framework', 'package.json');
167
+ const hasFrameworkNode = await fs.pathExists(frameworkNodePath);
168
+ const hasFramework = !!(hasFrameworkDep || hasFrameworkNode);
169
+
170
+ if (hasFramework) {
171
+ return {
172
+ pass: true,
173
+ message: 'Required framework dependency is installed'
174
+ };
175
+ } else {
176
+ const missing = ['slicejs-web-framework'];
177
+
178
+ return {
179
+ warn: true,
180
+ message: `Missing dependencies: ${missing.join(', ')}`,
181
+ suggestion: missing.includes('slicejs-web-framework')
182
+ ? 'Run "npm install slicejs-web-framework@latest" in your project'
183
+ : 'Run "npm install -D slicejs-cli@latest" in your project',
184
+ missing
185
+ };
186
+ }
187
+ } catch (error) {
188
+ return {
189
+ pass: false,
190
+ message: `package.json is invalid: ${error.message}`,
191
+ suggestion: 'Fix JSON syntax errors in package.json'
192
+ };
193
+ }
194
+ }
195
+
196
+ /**
197
+ * Verifica integridad de componentes
198
+ */
199
+ async function checkComponents() {
200
+ const configPath = getConfigPath(import.meta.url);
201
+ const projectRoot = getProjectRoot(import.meta.url);
202
+
203
+ if (!await fs.pathExists(configPath)) {
204
+ return {
205
+ warn: true,
206
+ message: 'Cannot check components (no config)',
207
+ suggestion: 'Run "slice init" first'
208
+ };
209
+ }
210
+
211
+ try {
212
+ const config = await fs.readJson(configPath);
213
+ const componentPaths = config.paths?.components || {};
214
+
215
+ let totalComponents = 0;
216
+ let componentIssues = 0;
217
+
218
+ for (const [category, { path: compPath }] of Object.entries(componentPaths)) {
219
+ const fullPath = getSrcPath(import.meta.url, compPath);
220
+
221
+ if (await fs.pathExists(fullPath)) {
222
+ const items = await fs.readdir(fullPath);
223
+
224
+ for (const item of items) {
225
+ const itemPath = path.join(fullPath, item);
226
+ const stat = await fs.stat(itemPath);
227
+
228
+ if (stat.isDirectory()) {
229
+ totalComponents++;
230
+
231
+ // Verificar archivos JS
232
+ const jsFile = path.join(itemPath, `${item}.js`);
233
+ if (!await fs.pathExists(jsFile)) {
234
+ componentIssues++;
235
+ }
236
+ }
237
+ }
238
+ }
239
+ }
240
+
241
+ if (componentIssues === 0) {
242
+ return {
243
+ pass: true,
244
+ message: `${totalComponents} components checked, all OK`
245
+ };
246
+ } else {
247
+ return {
248
+ warn: true,
249
+ message: `${componentIssues} component(s) have missing files`,
250
+ suggestion: 'Check your component directories'
251
+ };
252
+ }
253
+ } catch (error) {
254
+ return {
255
+ warn: true,
256
+ message: `Cannot check components: ${error.message}`,
257
+ suggestion: 'Verify your project structure'
258
+ };
259
+ }
260
+ }
261
+
262
+ /**
263
+ * Comando principal de diagnóstico
264
+ */
265
+ export default async function runDiagnostics() {
266
+ Print.newLine();
267
+ Print.title('🔍 Running Slice.js Diagnostics...');
268
+ Print.newLine();
269
+
270
+ const checks = [
271
+ { name: 'Node.js Version', fn: checkNodeVersion },
272
+ { name: 'Project Structure', fn: checkDirectoryStructure },
273
+ { name: 'Configuration', fn: checkConfig },
274
+ { name: 'Port Availability', fn: checkPort },
275
+ { name: 'Dependencies', fn: checkDependencies },
276
+ { name: 'Components', fn: checkComponents }
277
+ ];
278
+
279
+ const results = [];
280
+
281
+ for (const check of checks) {
282
+ const result = await check.fn();
283
+ results.push({ ...result, name: check.name });
284
+ }
285
+
286
+ // Crear tabla de resultados
287
+ const table = new Table({
288
+ head: [chalk.cyan.bold('Check'), chalk.cyan.bold('Status'), chalk.cyan.bold('Details')],
289
+ colWidths: [25, 10, 55],
290
+ style: {
291
+ head: [],
292
+ border: ['gray']
293
+ },
294
+ wordWrap: true
295
+ });
296
+
297
+ results.forEach(result => {
298
+ let status;
299
+ if (result.pass) {
300
+ status = chalk.green('✅ PASS');
301
+ } else if (result.warn) {
302
+ status = chalk.yellow('⚠️ WARN');
303
+ } else {
304
+ status = chalk.red('❌ FAIL');
305
+ }
306
+
307
+ const details = result.suggestion
308
+ ? `${result.message}\n${chalk.gray('→ ' + result.suggestion)}`
309
+ : result.message;
310
+
311
+ table.push([result.name, status, details]);
312
+ });
313
+
314
+ console.log(table.toString());
315
+
316
+ // Resumen
317
+ const issues = results.filter(r => !r.pass && !r.warn).length;
318
+ const warnings = results.filter(r => r.warn).length;
319
+ const passed = results.filter(r => r.pass).length;
320
+
321
+ Print.newLine();
322
+ Print.separator();
323
+
324
+ const depsResult = results.find(r => r.name === 'Dependencies');
325
+ if (depsResult && depsResult.warn && Array.isArray(depsResult.missing) && depsResult.missing.length > 0) {
326
+ const projectRoot = getProjectRoot(import.meta.url);
327
+ const execAsync = promisify(exec);
328
+ const { confirmInstall } = await inquirer.prompt([
329
+ {
330
+ type: 'confirm',
331
+ name: 'confirmInstall',
332
+ message: `Install missing dependencies in this project now? (${depsResult.missing.join(', ')})`,
333
+ default: true
334
+ }
335
+ ]);
336
+ if (confirmInstall) {
337
+ for (const pkg of depsResult.missing) {
338
+ try {
339
+ const cmd = 'npm install slicejs-web-framework@latest';
340
+ Print.info(`Installing ${pkg}...`);
341
+ await execAsync(cmd, { cwd: projectRoot });
342
+ Print.success(`${pkg} installed`);
343
+ } catch (e) {
344
+ Print.error(`Installing ${pkg}: ${e.message}`);
345
+ }
346
+ }
347
+ }
348
+ }
349
+
350
+ if (issues === 0 && warnings === 0) {
351
+ Print.success('All checks passed! 🎉');
352
+ Print.info('Your Slice.js project is correctly configured');
353
+ } else {
354
+ console.log(chalk.bold('📊 Summary:'));
355
+ console.log(chalk.green(` ✅ Passed: ${passed}`));
356
+ if (warnings > 0) console.log(chalk.yellow(` ⚠️ Warnings: ${warnings}`));
357
+ if (issues > 0) console.log(chalk.red(` ❌ Issues: ${issues}`));
358
+
359
+ Print.newLine();
360
+
361
+ if (issues > 0) {
362
+ Print.warning('Fix the issues above to ensure proper functionality');
363
+ } else {
364
+ Print.info('Warnings are non-critical but should be addressed');
365
+ }
366
+ }
367
+
368
+ Print.separator();
369
+ }