slicejs-cli 3.6.2 → 3.6.4
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/LICENSE +21 -21
- package/README.md +226 -226
- package/client.js +744 -744
- package/commands/Print.js +163 -163
- package/commands/Validations.js +92 -92
- package/commands/build/build.js +40 -40
- package/commands/buildProduction/buildProduction.js +577 -579
- package/commands/bundle/bundle.js +234 -234
- package/commands/createComponent/VisualComponentTemplate.js +55 -55
- package/commands/createComponent/createComponent.js +128 -128
- package/commands/deleteComponent/deleteComponent.js +81 -81
- package/commands/doctor/doctor.js +440 -440
- package/commands/getComponent/getComponent.js +701 -692
- package/commands/init/init.js +467 -463
- package/commands/listComponents/listComponents.js +172 -172
- package/commands/startServer/startServer.js +261 -261
- package/commands/startServer/watchServer.js +66 -79
- package/commands/types/types.js +580 -580
- package/commands/utils/LocalCliDelegation.js +53 -53
- package/commands/utils/PackageManager.js +148 -148
- package/commands/utils/PathHelper.js +75 -75
- package/commands/utils/VersionChecker.js +169 -169
- package/commands/utils/bundling/BundleGenerator.js +2525 -2525
- package/commands/utils/bundling/DependencyAnalyzer.js +925 -925
- package/commands/utils/loadConfig.js +31 -31
- package/commands/utils/sliceScripts.js +23 -23
- package/commands/utils/updateManager.js +471 -471
- package/package.json +73 -73
- package/post.js +60 -60
|
@@ -1,440 +1,440 @@
|
|
|
1
|
-
import fs from 'fs-extra';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { createServer } from 'net';
|
|
4
|
-
import chalk from 'chalk';
|
|
5
|
-
import Table from 'cli-table3';
|
|
6
|
-
import Print from '../Print.js';
|
|
7
|
-
import inquirer from 'inquirer';
|
|
8
|
-
import { exec } from 'child_process';
|
|
9
|
-
import { promisify } from 'util';
|
|
10
|
-
import { getProjectRoot, getSrcPath, getApiPath, getConfigPath, getPath } from '../utils/PathHelper.js';
|
|
11
|
-
import { resolvePackageManager, installCommand } from '../utils/PackageManager.js';
|
|
12
|
-
import updateManager from '../utils/updateManager.js';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Checks the Node.js version
|
|
16
|
-
*/
|
|
17
|
-
async function checkNodeVersion() {
|
|
18
|
-
const currentVersion = process.version;
|
|
19
|
-
const majorVersion = parseInt(currentVersion.slice(1).split('.')[0]);
|
|
20
|
-
const required = 20;
|
|
21
|
-
|
|
22
|
-
if (majorVersion >= required) {
|
|
23
|
-
return {
|
|
24
|
-
pass: true,
|
|
25
|
-
message: `Node.js version: ${currentVersion} (required: >= v${required})`
|
|
26
|
-
};
|
|
27
|
-
} else {
|
|
28
|
-
return {
|
|
29
|
-
pass: false,
|
|
30
|
-
message: `Node.js version: ${currentVersion} (required: >= v${required})`,
|
|
31
|
-
suggestion: `Update Node.js to v${required} or higher`
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Checks the directory structure
|
|
38
|
-
*/
|
|
39
|
-
async function checkDirectoryStructure() {
|
|
40
|
-
const srcPath = getSrcPath(import.meta.url);
|
|
41
|
-
const apiPath = getApiPath(import.meta.url);
|
|
42
|
-
|
|
43
|
-
const srcExists = await fs.pathExists(srcPath);
|
|
44
|
-
const apiExists = await fs.pathExists(apiPath);
|
|
45
|
-
|
|
46
|
-
if (srcExists && apiExists) {
|
|
47
|
-
return {
|
|
48
|
-
pass: true,
|
|
49
|
-
message: 'Project structure (src/ and api/) exists'
|
|
50
|
-
};
|
|
51
|
-
} else {
|
|
52
|
-
const missing = [];
|
|
53
|
-
if (!srcExists) missing.push('src/');
|
|
54
|
-
if (!apiExists) missing.push('api/');
|
|
55
|
-
|
|
56
|
-
return {
|
|
57
|
-
pass: false,
|
|
58
|
-
message: `Missing directories: ${missing.join(', ')}`,
|
|
59
|
-
suggestion: 'Run "slice init" to initialize your project'
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Checks sliceConfig.json
|
|
66
|
-
*/
|
|
67
|
-
async function checkConfig() {
|
|
68
|
-
const configPath = getConfigPath(import.meta.url);
|
|
69
|
-
|
|
70
|
-
if (!await fs.pathExists(configPath)) {
|
|
71
|
-
return {
|
|
72
|
-
pass: false,
|
|
73
|
-
message: 'sliceConfig.json not found',
|
|
74
|
-
suggestion: 'Run "slice init" to create configuration'
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
const config = await fs.readJson(configPath);
|
|
80
|
-
|
|
81
|
-
if (!config.paths || !config.paths.components) {
|
|
82
|
-
return {
|
|
83
|
-
pass: false,
|
|
84
|
-
message: 'sliceConfig.json is invalid (missing paths.components)',
|
|
85
|
-
suggestion: 'Check your configuration file'
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return {
|
|
90
|
-
pass: true,
|
|
91
|
-
message: 'sliceConfig.json is valid'
|
|
92
|
-
};
|
|
93
|
-
} catch (error) {
|
|
94
|
-
return {
|
|
95
|
-
pass: false,
|
|
96
|
-
message: `sliceConfig.json is invalid JSON: ${error.message}`,
|
|
97
|
-
suggestion: 'Fix JSON syntax errors in sliceConfig.json'
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Checks port availability
|
|
104
|
-
*/
|
|
105
|
-
async function checkPort() {
|
|
106
|
-
const configPath = getConfigPath(import.meta.url);
|
|
107
|
-
let port = 3000;
|
|
108
|
-
|
|
109
|
-
try {
|
|
110
|
-
if (await fs.pathExists(configPath)) {
|
|
111
|
-
const config = await fs.readJson(configPath);
|
|
112
|
-
port = config.server?.port || 3000;
|
|
113
|
-
}
|
|
114
|
-
} catch { /* config missing or unreadable — use default port */ }
|
|
115
|
-
|
|
116
|
-
return new Promise((resolve) => {
|
|
117
|
-
const server = createServer();
|
|
118
|
-
|
|
119
|
-
server.once('error', (err) => {
|
|
120
|
-
if (err.code === 'EADDRINUSE') {
|
|
121
|
-
resolve({
|
|
122
|
-
warn: true,
|
|
123
|
-
message: `Port ${port} is already in use`,
|
|
124
|
-
suggestion: `Stop the process using port ${port} or use: slice dev -p <other-port>`
|
|
125
|
-
});
|
|
126
|
-
} else {
|
|
127
|
-
resolve({
|
|
128
|
-
pass: true,
|
|
129
|
-
message: `Port ${port} is available`
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
server.once('listening', () => {
|
|
135
|
-
server.close();
|
|
136
|
-
resolve({
|
|
137
|
-
pass: true,
|
|
138
|
-
message: `Port ${port} is available`
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
server.listen(port);
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Checks the package manager setup: mixed lockfiles, packageManager field
|
|
148
|
-
* consistency, and a project-local CLI install (required for local delegation).
|
|
149
|
-
* Exported for tests.
|
|
150
|
-
*/
|
|
151
|
-
export async function checkPackageManagerSetup(projectRoot = getProjectRoot(import.meta.url)) {
|
|
152
|
-
const issues = [];
|
|
153
|
-
const suggestions = [];
|
|
154
|
-
|
|
155
|
-
const LOCKFILE_PM = {
|
|
156
|
-
'package-lock.json': 'npm',
|
|
157
|
-
'pnpm-lock.yaml': 'pnpm'
|
|
158
|
-
};
|
|
159
|
-
const lockfiles = [];
|
|
160
|
-
for (const lockfile of Object.keys(LOCKFILE_PM)) {
|
|
161
|
-
if (await fs.pathExists(path.join(projectRoot, lockfile))) {
|
|
162
|
-
lockfiles.push(lockfile);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
let pkg = null;
|
|
167
|
-
const pkgPath = path.join(projectRoot, 'package.json');
|
|
168
|
-
if (await fs.pathExists(pkgPath)) {
|
|
169
|
-
try { pkg = await fs.readJson(pkgPath); } catch { /* reported by Dependencies check */ }
|
|
170
|
-
}
|
|
171
|
-
const pmField = typeof pkg?.packageManager === 'string' ? pkg.packageManager.split('@')[0] : null;
|
|
172
|
-
const pm = resolvePackageManager(projectRoot).name;
|
|
173
|
-
|
|
174
|
-
if (lockfiles.length > 1) {
|
|
175
|
-
issues.push(`Mixed lockfiles found: ${lockfiles.join(', ')}`);
|
|
176
|
-
const keep = pmField || pm;
|
|
177
|
-
const keepLockfile = Object.entries(LOCKFILE_PM).find(([, name]) => name === keep)?.[0];
|
|
178
|
-
suggestions.push(`Keep only ${keepLockfile ?? 'the lockfile of your package manager'} and delete the rest`);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (pkg && !pmField) {
|
|
182
|
-
issues.push('No "packageManager" field in package.json');
|
|
183
|
-
suggestions.push(`Pin it (e.g. "packageManager": "${pm}@<version>") so every tool resolves the same package manager`);
|
|
184
|
-
} else if (pmField && lockfiles.length === 1 && LOCKFILE_PM[lockfiles[0]] !== pmField) {
|
|
185
|
-
issues.push(`packageManager is "${pmField}" but the lockfile is ${lockfiles[0]}`);
|
|
186
|
-
suggestions.push(`Reinstall with ${pmField} (or update the packageManager field) so they agree`);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const localCliPath = path.join(projectRoot, 'node_modules', 'slicejs-cli', 'package.json');
|
|
190
|
-
if (pkg && !(await fs.pathExists(localCliPath))) {
|
|
191
|
-
issues.push('slicejs-cli is not installed locally');
|
|
192
|
-
suggestions.push(`Run "${installCommand(pm, 'slicejs-cli', { dev: true })}" so the launcher delegates to a version pinned per project`);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (issues.length === 0) {
|
|
196
|
-
return {
|
|
197
|
-
pass: true,
|
|
198
|
-
message: `Package manager setup is consistent (${pmField || pm})`
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
return {
|
|
202
|
-
warn: true,
|
|
203
|
-
message: issues.join(' | '),
|
|
204
|
-
suggestion: suggestions.join(' | ')
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Checks dependencies in package.json
|
|
210
|
-
*/
|
|
211
|
-
async function checkDependencies() {
|
|
212
|
-
const projectRoot = getProjectRoot(import.meta.url);
|
|
213
|
-
const packagePath = getPath(import.meta.url, 'package.json');
|
|
214
|
-
|
|
215
|
-
if (!await fs.pathExists(packagePath)) {
|
|
216
|
-
return {
|
|
217
|
-
warn: true,
|
|
218
|
-
message: 'package.json not found',
|
|
219
|
-
suggestion: 'Run "npm init" to create package.json'
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
try {
|
|
224
|
-
const pkg = await fs.readJson(packagePath);
|
|
225
|
-
const hasFrameworkDep = pkg.dependencies?.['slicejs-web-framework'] || pkg.devDependencies?.['slicejs-web-framework'];
|
|
226
|
-
const frameworkNodePath = getPath(import.meta.url, 'node_modules', 'slicejs-web-framework', 'package.json');
|
|
227
|
-
const hasFrameworkNode = await fs.pathExists(frameworkNodePath);
|
|
228
|
-
const hasFramework = !!(hasFrameworkDep || hasFrameworkNode);
|
|
229
|
-
|
|
230
|
-
if (hasFramework) {
|
|
231
|
-
return {
|
|
232
|
-
pass: true,
|
|
233
|
-
message: 'Required framework dependency is installed'
|
|
234
|
-
};
|
|
235
|
-
} else {
|
|
236
|
-
const missing = ['slicejs-web-framework'];
|
|
237
|
-
|
|
238
|
-
return {
|
|
239
|
-
warn: true,
|
|
240
|
-
message: `Missing dependencies: ${missing.join(', ')}`,
|
|
241
|
-
suggestion: missing.includes('slicejs-web-framework')
|
|
242
|
-
? `Run "${installCommand(resolvePackageManager(getProjectRoot(import.meta.url)).name, 'slicejs-web-framework@latest')}" in your project`
|
|
243
|
-
: `Run "${installCommand(resolvePackageManager(getProjectRoot(import.meta.url)).name, 'slicejs-cli@latest', { dev: true })}" in your project`,
|
|
244
|
-
missing
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
} catch (error) {
|
|
248
|
-
return {
|
|
249
|
-
pass: false,
|
|
250
|
-
message: `package.json is invalid: ${error.message}`,
|
|
251
|
-
suggestion: 'Fix JSON syntax errors in package.json'
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Checks component integrity
|
|
258
|
-
*/
|
|
259
|
-
async function checkComponents() {
|
|
260
|
-
const configPath = getConfigPath(import.meta.url);
|
|
261
|
-
const projectRoot = getProjectRoot(import.meta.url);
|
|
262
|
-
|
|
263
|
-
if (!await fs.pathExists(configPath)) {
|
|
264
|
-
return {
|
|
265
|
-
warn: true,
|
|
266
|
-
message: 'Cannot check components (no config)',
|
|
267
|
-
suggestion: 'Run "slice init" first'
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
try {
|
|
272
|
-
const config = await fs.readJson(configPath);
|
|
273
|
-
const componentPaths = config.paths?.components || {};
|
|
274
|
-
|
|
275
|
-
let totalComponents = 0;
|
|
276
|
-
let componentIssues = 0;
|
|
277
|
-
|
|
278
|
-
for (const [category, { path: compPath }] of Object.entries(componentPaths)) {
|
|
279
|
-
const fullPath = getSrcPath(import.meta.url, compPath);
|
|
280
|
-
|
|
281
|
-
if (await fs.pathExists(fullPath)) {
|
|
282
|
-
const items = await fs.readdir(fullPath);
|
|
283
|
-
|
|
284
|
-
for (const item of items) {
|
|
285
|
-
const itemPath = path.join(fullPath, item);
|
|
286
|
-
const stat = await fs.stat(itemPath);
|
|
287
|
-
|
|
288
|
-
if (stat.isDirectory()) {
|
|
289
|
-
totalComponents++;
|
|
290
|
-
|
|
291
|
-
// Check JS files
|
|
292
|
-
const jsFile = path.join(itemPath, `${item}.js`);
|
|
293
|
-
if (!await fs.pathExists(jsFile)) {
|
|
294
|
-
componentIssues++;
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
if (componentIssues === 0) {
|
|
302
|
-
return {
|
|
303
|
-
pass: true,
|
|
304
|
-
message: `${totalComponents} components checked, all OK`
|
|
305
|
-
};
|
|
306
|
-
} else {
|
|
307
|
-
return {
|
|
308
|
-
warn: true,
|
|
309
|
-
message: `${componentIssues} component(s) have missing files`,
|
|
310
|
-
suggestion: 'Check your component directories'
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
} catch (error) {
|
|
314
|
-
return {
|
|
315
|
-
warn: true,
|
|
316
|
-
message: `Cannot check components: ${error.message}`,
|
|
317
|
-
suggestion: 'Verify your project structure'
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
* Main diagnostic command
|
|
324
|
-
*/
|
|
325
|
-
export {
|
|
326
|
-
checkNodeVersion,
|
|
327
|
-
checkDirectoryStructure,
|
|
328
|
-
checkConfig,
|
|
329
|
-
checkPort,
|
|
330
|
-
checkDependencies,
|
|
331
|
-
checkComponents
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
export default async function runDiagnostics() {
|
|
335
|
-
Print.newLine();
|
|
336
|
-
Print.title('🔍 Running Slice.js Diagnostics...');
|
|
337
|
-
Print.newLine();
|
|
338
|
-
|
|
339
|
-
const checks = [
|
|
340
|
-
{ name: 'Node.js Version', fn: checkNodeVersion },
|
|
341
|
-
{ name: 'Project Structure', fn: checkDirectoryStructure },
|
|
342
|
-
{ name: 'Configuration', fn: checkConfig },
|
|
343
|
-
{ name: 'Port Availability', fn: checkPort },
|
|
344
|
-
{ name: 'Package Manager', fn: checkPackageManagerSetup },
|
|
345
|
-
{ name: 'Dependencies', fn: checkDependencies },
|
|
346
|
-
{ name: 'Components', fn: checkComponents }
|
|
347
|
-
];
|
|
348
|
-
|
|
349
|
-
const results = [];
|
|
350
|
-
|
|
351
|
-
for (const check of checks) {
|
|
352
|
-
const result = await check.fn();
|
|
353
|
-
results.push({ ...result, name: check.name });
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// Crear tabla de resultados
|
|
357
|
-
const table = new Table({
|
|
358
|
-
head: [chalk.cyan.bold('Check'), chalk.cyan.bold('Status'), chalk.cyan.bold('Details')],
|
|
359
|
-
colWidths: [25, 10, 55],
|
|
360
|
-
style: {
|
|
361
|
-
head: [],
|
|
362
|
-
border: ['gray']
|
|
363
|
-
},
|
|
364
|
-
wordWrap: true
|
|
365
|
-
});
|
|
366
|
-
|
|
367
|
-
results.forEach(result => {
|
|
368
|
-
let status;
|
|
369
|
-
if (result.pass) {
|
|
370
|
-
status = chalk.green('✅ PASS');
|
|
371
|
-
} else if (result.warn) {
|
|
372
|
-
status = chalk.yellow('⚠️ WARN');
|
|
373
|
-
} else {
|
|
374
|
-
status = chalk.red('❌ FAIL');
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
const details = result.suggestion
|
|
378
|
-
? `${result.message}\n${chalk.gray('→ ' + result.suggestion)}`
|
|
379
|
-
: result.message;
|
|
380
|
-
|
|
381
|
-
table.push([result.name, status, details]);
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
console.log(table.toString());
|
|
385
|
-
|
|
386
|
-
// Resumen
|
|
387
|
-
const issues = results.filter(r => !r.pass && !r.warn).length;
|
|
388
|
-
const warnings = results.filter(r => r.warn).length;
|
|
389
|
-
const passed = results.filter(r => r.pass).length;
|
|
390
|
-
|
|
391
|
-
Print.newLine();
|
|
392
|
-
Print.separator();
|
|
393
|
-
|
|
394
|
-
const depsResult = results.find(r => r.name === 'Dependencies');
|
|
395
|
-
if (depsResult && depsResult.warn && Array.isArray(depsResult.missing) && depsResult.missing.length > 0) {
|
|
396
|
-
const projectRoot = getProjectRoot(import.meta.url);
|
|
397
|
-
const execAsync = promisify(exec);
|
|
398
|
-
const { confirmInstall } = await inquirer.prompt([
|
|
399
|
-
{
|
|
400
|
-
type: 'confirm',
|
|
401
|
-
name: 'confirmInstall',
|
|
402
|
-
message: `Install missing dependencies in this project now? (${depsResult.missing.join(', ')})`,
|
|
403
|
-
default: true
|
|
404
|
-
}
|
|
405
|
-
]);
|
|
406
|
-
if (confirmInstall) {
|
|
407
|
-
const pm = resolvePackageManager(projectRoot).name;
|
|
408
|
-
for (const pkg of depsResult.missing) {
|
|
409
|
-
try {
|
|
410
|
-
const cmd = installCommand(pm, `${pkg}@latest`);
|
|
411
|
-
Print.info(`Installing ${pkg}...`);
|
|
412
|
-
await execAsync(cmd, { cwd: projectRoot });
|
|
413
|
-
Print.success(`${pkg} installed`);
|
|
414
|
-
} catch (e) {
|
|
415
|
-
Print.error(`Installing ${pkg}: ${e.message}`);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
if (issues === 0 && warnings === 0) {
|
|
422
|
-
Print.success('All checks passed! 🎉');
|
|
423
|
-
Print.info('Your Slice.js project is correctly configured');
|
|
424
|
-
} else {
|
|
425
|
-
console.log(chalk.bold('📊 Summary:'));
|
|
426
|
-
console.log(chalk.green(` ✅ Passed: ${passed}`));
|
|
427
|
-
if (warnings > 0) console.log(chalk.yellow(` ⚠️ Warnings: ${warnings}`));
|
|
428
|
-
if (issues > 0) console.log(chalk.red(` ❌ Issues: ${issues}`));
|
|
429
|
-
|
|
430
|
-
Print.newLine();
|
|
431
|
-
|
|
432
|
-
if (issues > 0) {
|
|
433
|
-
Print.warning('Fix the issues above to ensure proper functionality');
|
|
434
|
-
} else {
|
|
435
|
-
Print.info('Warnings are non-critical but should be addressed');
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
Print.separator();
|
|
440
|
-
}
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { createServer } from 'net';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import Table from 'cli-table3';
|
|
6
|
+
import Print from '../Print.js';
|
|
7
|
+
import inquirer from 'inquirer';
|
|
8
|
+
import { exec } from 'child_process';
|
|
9
|
+
import { promisify } from 'util';
|
|
10
|
+
import { getProjectRoot, getSrcPath, getApiPath, getConfigPath, getPath } from '../utils/PathHelper.js';
|
|
11
|
+
import { resolvePackageManager, installCommand } from '../utils/PackageManager.js';
|
|
12
|
+
import updateManager from '../utils/updateManager.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Checks the Node.js version
|
|
16
|
+
*/
|
|
17
|
+
async function checkNodeVersion() {
|
|
18
|
+
const currentVersion = process.version;
|
|
19
|
+
const majorVersion = parseInt(currentVersion.slice(1).split('.')[0]);
|
|
20
|
+
const required = 20;
|
|
21
|
+
|
|
22
|
+
if (majorVersion >= required) {
|
|
23
|
+
return {
|
|
24
|
+
pass: true,
|
|
25
|
+
message: `Node.js version: ${currentVersion} (required: >= v${required})`
|
|
26
|
+
};
|
|
27
|
+
} else {
|
|
28
|
+
return {
|
|
29
|
+
pass: false,
|
|
30
|
+
message: `Node.js version: ${currentVersion} (required: >= v${required})`,
|
|
31
|
+
suggestion: `Update Node.js to v${required} or higher`
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Checks the directory structure
|
|
38
|
+
*/
|
|
39
|
+
async function checkDirectoryStructure() {
|
|
40
|
+
const srcPath = getSrcPath(import.meta.url);
|
|
41
|
+
const apiPath = getApiPath(import.meta.url);
|
|
42
|
+
|
|
43
|
+
const srcExists = await fs.pathExists(srcPath);
|
|
44
|
+
const apiExists = await fs.pathExists(apiPath);
|
|
45
|
+
|
|
46
|
+
if (srcExists && apiExists) {
|
|
47
|
+
return {
|
|
48
|
+
pass: true,
|
|
49
|
+
message: 'Project structure (src/ and api/) exists'
|
|
50
|
+
};
|
|
51
|
+
} else {
|
|
52
|
+
const missing = [];
|
|
53
|
+
if (!srcExists) missing.push('src/');
|
|
54
|
+
if (!apiExists) missing.push('api/');
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
pass: false,
|
|
58
|
+
message: `Missing directories: ${missing.join(', ')}`,
|
|
59
|
+
suggestion: 'Run "slice init" to initialize your project'
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Checks sliceConfig.json
|
|
66
|
+
*/
|
|
67
|
+
async function checkConfig() {
|
|
68
|
+
const configPath = getConfigPath(import.meta.url);
|
|
69
|
+
|
|
70
|
+
if (!await fs.pathExists(configPath)) {
|
|
71
|
+
return {
|
|
72
|
+
pass: false,
|
|
73
|
+
message: 'sliceConfig.json not found',
|
|
74
|
+
suggestion: 'Run "slice init" to create configuration'
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
const config = await fs.readJson(configPath);
|
|
80
|
+
|
|
81
|
+
if (!config.paths || !config.paths.components) {
|
|
82
|
+
return {
|
|
83
|
+
pass: false,
|
|
84
|
+
message: 'sliceConfig.json is invalid (missing paths.components)',
|
|
85
|
+
suggestion: 'Check your configuration file'
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
pass: true,
|
|
91
|
+
message: 'sliceConfig.json is valid'
|
|
92
|
+
};
|
|
93
|
+
} catch (error) {
|
|
94
|
+
return {
|
|
95
|
+
pass: false,
|
|
96
|
+
message: `sliceConfig.json is invalid JSON: ${error.message}`,
|
|
97
|
+
suggestion: 'Fix JSON syntax errors in sliceConfig.json'
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Checks port availability
|
|
104
|
+
*/
|
|
105
|
+
async function checkPort() {
|
|
106
|
+
const configPath = getConfigPath(import.meta.url);
|
|
107
|
+
let port = 3000;
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
if (await fs.pathExists(configPath)) {
|
|
111
|
+
const config = await fs.readJson(configPath);
|
|
112
|
+
port = config.server?.port || 3000;
|
|
113
|
+
}
|
|
114
|
+
} catch { /* config missing or unreadable — use default port */ }
|
|
115
|
+
|
|
116
|
+
return new Promise((resolve) => {
|
|
117
|
+
const server = createServer();
|
|
118
|
+
|
|
119
|
+
server.once('error', (err) => {
|
|
120
|
+
if (err.code === 'EADDRINUSE') {
|
|
121
|
+
resolve({
|
|
122
|
+
warn: true,
|
|
123
|
+
message: `Port ${port} is already in use`,
|
|
124
|
+
suggestion: `Stop the process using port ${port} or use: slice dev -p <other-port>`
|
|
125
|
+
});
|
|
126
|
+
} else {
|
|
127
|
+
resolve({
|
|
128
|
+
pass: true,
|
|
129
|
+
message: `Port ${port} is available`
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
server.once('listening', () => {
|
|
135
|
+
server.close();
|
|
136
|
+
resolve({
|
|
137
|
+
pass: true,
|
|
138
|
+
message: `Port ${port} is available`
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
server.listen(port);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Checks the package manager setup: mixed lockfiles, packageManager field
|
|
148
|
+
* consistency, and a project-local CLI install (required for local delegation).
|
|
149
|
+
* Exported for tests.
|
|
150
|
+
*/
|
|
151
|
+
export async function checkPackageManagerSetup(projectRoot = getProjectRoot(import.meta.url)) {
|
|
152
|
+
const issues = [];
|
|
153
|
+
const suggestions = [];
|
|
154
|
+
|
|
155
|
+
const LOCKFILE_PM = {
|
|
156
|
+
'package-lock.json': 'npm',
|
|
157
|
+
'pnpm-lock.yaml': 'pnpm'
|
|
158
|
+
};
|
|
159
|
+
const lockfiles = [];
|
|
160
|
+
for (const lockfile of Object.keys(LOCKFILE_PM)) {
|
|
161
|
+
if (await fs.pathExists(path.join(projectRoot, lockfile))) {
|
|
162
|
+
lockfiles.push(lockfile);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
let pkg = null;
|
|
167
|
+
const pkgPath = path.join(projectRoot, 'package.json');
|
|
168
|
+
if (await fs.pathExists(pkgPath)) {
|
|
169
|
+
try { pkg = await fs.readJson(pkgPath); } catch { /* reported by Dependencies check */ }
|
|
170
|
+
}
|
|
171
|
+
const pmField = typeof pkg?.packageManager === 'string' ? pkg.packageManager.split('@')[0] : null;
|
|
172
|
+
const pm = resolvePackageManager(projectRoot).name;
|
|
173
|
+
|
|
174
|
+
if (lockfiles.length > 1) {
|
|
175
|
+
issues.push(`Mixed lockfiles found: ${lockfiles.join(', ')}`);
|
|
176
|
+
const keep = pmField || pm;
|
|
177
|
+
const keepLockfile = Object.entries(LOCKFILE_PM).find(([, name]) => name === keep)?.[0];
|
|
178
|
+
suggestions.push(`Keep only ${keepLockfile ?? 'the lockfile of your package manager'} and delete the rest`);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (pkg && !pmField) {
|
|
182
|
+
issues.push('No "packageManager" field in package.json');
|
|
183
|
+
suggestions.push(`Pin it (e.g. "packageManager": "${pm}@<version>") so every tool resolves the same package manager`);
|
|
184
|
+
} else if (pmField && lockfiles.length === 1 && LOCKFILE_PM[lockfiles[0]] !== pmField) {
|
|
185
|
+
issues.push(`packageManager is "${pmField}" but the lockfile is ${lockfiles[0]}`);
|
|
186
|
+
suggestions.push(`Reinstall with ${pmField} (or update the packageManager field) so they agree`);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const localCliPath = path.join(projectRoot, 'node_modules', 'slicejs-cli', 'package.json');
|
|
190
|
+
if (pkg && !(await fs.pathExists(localCliPath))) {
|
|
191
|
+
issues.push('slicejs-cli is not installed locally');
|
|
192
|
+
suggestions.push(`Run "${installCommand(pm, 'slicejs-cli', { dev: true })}" so the launcher delegates to a version pinned per project`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (issues.length === 0) {
|
|
196
|
+
return {
|
|
197
|
+
pass: true,
|
|
198
|
+
message: `Package manager setup is consistent (${pmField || pm})`
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
warn: true,
|
|
203
|
+
message: issues.join(' | '),
|
|
204
|
+
suggestion: suggestions.join(' | ')
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Checks dependencies in package.json
|
|
210
|
+
*/
|
|
211
|
+
async function checkDependencies() {
|
|
212
|
+
const projectRoot = getProjectRoot(import.meta.url);
|
|
213
|
+
const packagePath = getPath(import.meta.url, 'package.json');
|
|
214
|
+
|
|
215
|
+
if (!await fs.pathExists(packagePath)) {
|
|
216
|
+
return {
|
|
217
|
+
warn: true,
|
|
218
|
+
message: 'package.json not found',
|
|
219
|
+
suggestion: 'Run "npm init" to create package.json'
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
try {
|
|
224
|
+
const pkg = await fs.readJson(packagePath);
|
|
225
|
+
const hasFrameworkDep = pkg.dependencies?.['slicejs-web-framework'] || pkg.devDependencies?.['slicejs-web-framework'];
|
|
226
|
+
const frameworkNodePath = getPath(import.meta.url, 'node_modules', 'slicejs-web-framework', 'package.json');
|
|
227
|
+
const hasFrameworkNode = await fs.pathExists(frameworkNodePath);
|
|
228
|
+
const hasFramework = !!(hasFrameworkDep || hasFrameworkNode);
|
|
229
|
+
|
|
230
|
+
if (hasFramework) {
|
|
231
|
+
return {
|
|
232
|
+
pass: true,
|
|
233
|
+
message: 'Required framework dependency is installed'
|
|
234
|
+
};
|
|
235
|
+
} else {
|
|
236
|
+
const missing = ['slicejs-web-framework'];
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
warn: true,
|
|
240
|
+
message: `Missing dependencies: ${missing.join(', ')}`,
|
|
241
|
+
suggestion: missing.includes('slicejs-web-framework')
|
|
242
|
+
? `Run "${installCommand(resolvePackageManager(getProjectRoot(import.meta.url)).name, 'slicejs-web-framework@latest')}" in your project`
|
|
243
|
+
: `Run "${installCommand(resolvePackageManager(getProjectRoot(import.meta.url)).name, 'slicejs-cli@latest', { dev: true })}" in your project`,
|
|
244
|
+
missing
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
} catch (error) {
|
|
248
|
+
return {
|
|
249
|
+
pass: false,
|
|
250
|
+
message: `package.json is invalid: ${error.message}`,
|
|
251
|
+
suggestion: 'Fix JSON syntax errors in package.json'
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Checks component integrity
|
|
258
|
+
*/
|
|
259
|
+
async function checkComponents() {
|
|
260
|
+
const configPath = getConfigPath(import.meta.url);
|
|
261
|
+
const projectRoot = getProjectRoot(import.meta.url);
|
|
262
|
+
|
|
263
|
+
if (!await fs.pathExists(configPath)) {
|
|
264
|
+
return {
|
|
265
|
+
warn: true,
|
|
266
|
+
message: 'Cannot check components (no config)',
|
|
267
|
+
suggestion: 'Run "slice init" first'
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
try {
|
|
272
|
+
const config = await fs.readJson(configPath);
|
|
273
|
+
const componentPaths = config.paths?.components || {};
|
|
274
|
+
|
|
275
|
+
let totalComponents = 0;
|
|
276
|
+
let componentIssues = 0;
|
|
277
|
+
|
|
278
|
+
for (const [category, { path: compPath }] of Object.entries(componentPaths)) {
|
|
279
|
+
const fullPath = getSrcPath(import.meta.url, compPath);
|
|
280
|
+
|
|
281
|
+
if (await fs.pathExists(fullPath)) {
|
|
282
|
+
const items = await fs.readdir(fullPath);
|
|
283
|
+
|
|
284
|
+
for (const item of items) {
|
|
285
|
+
const itemPath = path.join(fullPath, item);
|
|
286
|
+
const stat = await fs.stat(itemPath);
|
|
287
|
+
|
|
288
|
+
if (stat.isDirectory()) {
|
|
289
|
+
totalComponents++;
|
|
290
|
+
|
|
291
|
+
// Check JS files
|
|
292
|
+
const jsFile = path.join(itemPath, `${item}.js`);
|
|
293
|
+
if (!await fs.pathExists(jsFile)) {
|
|
294
|
+
componentIssues++;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (componentIssues === 0) {
|
|
302
|
+
return {
|
|
303
|
+
pass: true,
|
|
304
|
+
message: `${totalComponents} components checked, all OK`
|
|
305
|
+
};
|
|
306
|
+
} else {
|
|
307
|
+
return {
|
|
308
|
+
warn: true,
|
|
309
|
+
message: `${componentIssues} component(s) have missing files`,
|
|
310
|
+
suggestion: 'Check your component directories'
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
} catch (error) {
|
|
314
|
+
return {
|
|
315
|
+
warn: true,
|
|
316
|
+
message: `Cannot check components: ${error.message}`,
|
|
317
|
+
suggestion: 'Verify your project structure'
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Main diagnostic command
|
|
324
|
+
*/
|
|
325
|
+
export {
|
|
326
|
+
checkNodeVersion,
|
|
327
|
+
checkDirectoryStructure,
|
|
328
|
+
checkConfig,
|
|
329
|
+
checkPort,
|
|
330
|
+
checkDependencies,
|
|
331
|
+
checkComponents
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
export default async function runDiagnostics() {
|
|
335
|
+
Print.newLine();
|
|
336
|
+
Print.title('🔍 Running Slice.js Diagnostics...');
|
|
337
|
+
Print.newLine();
|
|
338
|
+
|
|
339
|
+
const checks = [
|
|
340
|
+
{ name: 'Node.js Version', fn: checkNodeVersion },
|
|
341
|
+
{ name: 'Project Structure', fn: checkDirectoryStructure },
|
|
342
|
+
{ name: 'Configuration', fn: checkConfig },
|
|
343
|
+
{ name: 'Port Availability', fn: checkPort },
|
|
344
|
+
{ name: 'Package Manager', fn: checkPackageManagerSetup },
|
|
345
|
+
{ name: 'Dependencies', fn: checkDependencies },
|
|
346
|
+
{ name: 'Components', fn: checkComponents }
|
|
347
|
+
];
|
|
348
|
+
|
|
349
|
+
const results = [];
|
|
350
|
+
|
|
351
|
+
for (const check of checks) {
|
|
352
|
+
const result = await check.fn();
|
|
353
|
+
results.push({ ...result, name: check.name });
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Crear tabla de resultados
|
|
357
|
+
const table = new Table({
|
|
358
|
+
head: [chalk.cyan.bold('Check'), chalk.cyan.bold('Status'), chalk.cyan.bold('Details')],
|
|
359
|
+
colWidths: [25, 10, 55],
|
|
360
|
+
style: {
|
|
361
|
+
head: [],
|
|
362
|
+
border: ['gray']
|
|
363
|
+
},
|
|
364
|
+
wordWrap: true
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
results.forEach(result => {
|
|
368
|
+
let status;
|
|
369
|
+
if (result.pass) {
|
|
370
|
+
status = chalk.green('✅ PASS');
|
|
371
|
+
} else if (result.warn) {
|
|
372
|
+
status = chalk.yellow('⚠️ WARN');
|
|
373
|
+
} else {
|
|
374
|
+
status = chalk.red('❌ FAIL');
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const details = result.suggestion
|
|
378
|
+
? `${result.message}\n${chalk.gray('→ ' + result.suggestion)}`
|
|
379
|
+
: result.message;
|
|
380
|
+
|
|
381
|
+
table.push([result.name, status, details]);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
console.log(table.toString());
|
|
385
|
+
|
|
386
|
+
// Resumen
|
|
387
|
+
const issues = results.filter(r => !r.pass && !r.warn).length;
|
|
388
|
+
const warnings = results.filter(r => r.warn).length;
|
|
389
|
+
const passed = results.filter(r => r.pass).length;
|
|
390
|
+
|
|
391
|
+
Print.newLine();
|
|
392
|
+
Print.separator();
|
|
393
|
+
|
|
394
|
+
const depsResult = results.find(r => r.name === 'Dependencies');
|
|
395
|
+
if (depsResult && depsResult.warn && Array.isArray(depsResult.missing) && depsResult.missing.length > 0) {
|
|
396
|
+
const projectRoot = getProjectRoot(import.meta.url);
|
|
397
|
+
const execAsync = promisify(exec);
|
|
398
|
+
const { confirmInstall } = await inquirer.prompt([
|
|
399
|
+
{
|
|
400
|
+
type: 'confirm',
|
|
401
|
+
name: 'confirmInstall',
|
|
402
|
+
message: `Install missing dependencies in this project now? (${depsResult.missing.join(', ')})`,
|
|
403
|
+
default: true
|
|
404
|
+
}
|
|
405
|
+
]);
|
|
406
|
+
if (confirmInstall) {
|
|
407
|
+
const pm = resolvePackageManager(projectRoot).name;
|
|
408
|
+
for (const pkg of depsResult.missing) {
|
|
409
|
+
try {
|
|
410
|
+
const cmd = installCommand(pm, `${pkg}@latest`);
|
|
411
|
+
Print.info(`Installing ${pkg}...`);
|
|
412
|
+
await execAsync(cmd, { cwd: projectRoot });
|
|
413
|
+
Print.success(`${pkg} installed`);
|
|
414
|
+
} catch (e) {
|
|
415
|
+
Print.error(`Installing ${pkg}: ${e.message}`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (issues === 0 && warnings === 0) {
|
|
422
|
+
Print.success('All checks passed! 🎉');
|
|
423
|
+
Print.info('Your Slice.js project is correctly configured');
|
|
424
|
+
} else {
|
|
425
|
+
console.log(chalk.bold('📊 Summary:'));
|
|
426
|
+
console.log(chalk.green(` ✅ Passed: ${passed}`));
|
|
427
|
+
if (warnings > 0) console.log(chalk.yellow(` ⚠️ Warnings: ${warnings}`));
|
|
428
|
+
if (issues > 0) console.log(chalk.red(` ❌ Issues: ${issues}`));
|
|
429
|
+
|
|
430
|
+
Print.newLine();
|
|
431
|
+
|
|
432
|
+
if (issues > 0) {
|
|
433
|
+
Print.warning('Fix the issues above to ensure proper functionality');
|
|
434
|
+
} else {
|
|
435
|
+
Print.info('Warnings are non-critical but should be addressed');
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
Print.separator();
|
|
440
|
+
}
|