slicejs-cli 4.4.0 → 4.6.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.
- package/client.js +192 -82
- package/commands/Print.js +158 -164
- package/commands/build/build.js +27 -1
- package/commands/buildProduction/buildProduction.js +42 -71
- package/commands/bundle/bundle.js +65 -60
- package/commands/docs/agents.js +260 -0
- package/commands/docs/events.js +365 -0
- package/commands/docs/index.js +123 -0
- package/commands/doctor/doctor.js +99 -12
- package/commands/export/checkExport.js +163 -0
- package/commands/export/clientRedirects.js +72 -0
- package/commands/export/copyDist.js +180 -0
- package/commands/export/export.js +236 -0
- package/commands/export/files.js +33 -0
- package/commands/export/hosts/cloudflare.js +48 -0
- package/commands/export/hosts/github.js +31 -0
- package/commands/export/hosts/index.js +69 -0
- package/commands/export/hosts/netlify.js +42 -0
- package/commands/export/hosts/nginx.js +73 -0
- package/commands/export/hosts/none.js +23 -0
- package/commands/export/hosts/s3.js +24 -0
- package/commands/export/hosts/shared.js +62 -0
- package/commands/export/hosts/vercel.js +69 -0
- package/commands/export/outDir.js +91 -0
- package/commands/export/prerender.js +121 -0
- package/commands/getComponent/getComponent.js +44 -40
- package/commands/init/init.js +32 -22
- package/commands/lint/lint.js +2 -2
- package/commands/lint/types.js +1 -1
- package/commands/listComponents/listComponents.js +205 -172
- package/commands/seo/check.js +7 -4
- package/commands/seo/images.js +77 -22
- package/commands/seo/sitemap.js +6 -3
- package/commands/seo/support.js +62 -0
- package/commands/setup/lint.js +1 -1
- package/commands/skill/skill.js +2 -2
- package/commands/startServer/startServer.js +60 -21
- package/commands/startServer/watchServer.js +3 -3
- package/commands/translator/check.js +32 -0
- package/commands/translator/init.js +125 -0
- package/commands/translator/scan.js +444 -0
- package/commands/typecheck/typecheck.js +2 -2
- package/commands/types/types.js +22 -3
- package/commands/utils/VersionChecker.js +14 -14
- package/commands/utils/bundling/BundleGenerator.js +45 -27
- package/commands/utils/bundling/DependencyAnalyzer.js +1007 -976
- package/commands/utils/componentCatalog.js +120 -0
- package/commands/utils/publicEnv.js +74 -0
- package/commands/utils/sliceConfigSchema.js +34 -0
- package/commands/utils/updateManager.js +12 -12
- package/package.json +22 -23
package/commands/seo/images.js
CHANGED
|
@@ -15,7 +15,7 @@ import { createHash } from 'node:crypto';
|
|
|
15
15
|
import { createRequire } from 'node:module';
|
|
16
16
|
import { pathToFileURL } from 'node:url';
|
|
17
17
|
import Print from '../Print.js';
|
|
18
|
-
import { loadManifest, writeFileConfirmed } from './support.js';
|
|
18
|
+
import { loadFrameworkSeo, loadManifest, loadSeoTranslator, writeFileConfirmed } from './support.js';
|
|
19
19
|
|
|
20
20
|
const CARD = { width: 1200, height: 630 };
|
|
21
21
|
const DEVICE_SCALE = 2;
|
|
@@ -88,6 +88,29 @@ export function selectRoutes(manifest, only = null) {
|
|
|
88
88
|
.sort((a, b) => a.route.localeCompare(b.route));
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* One card target per route — or per route and locale when the translator puts the locale
|
|
93
|
+
* in the URL. `url` is both what the browser captures and the key in the cards manifest,
|
|
94
|
+
* so the server can pick the card for the exact page it is serving.
|
|
95
|
+
*/
|
|
96
|
+
export function cardTargets(manifest, translator = null, only = null) {
|
|
97
|
+
const wanted = only ? new Set(String(only).split(',').map((item) => item.trim())) : null;
|
|
98
|
+
const locales = translator?.enabled
|
|
99
|
+
? (translator.localePrefix ? translator.locales : [translator.defaultLocale])
|
|
100
|
+
: [null];
|
|
101
|
+
const targets = [];
|
|
102
|
+
|
|
103
|
+
for (const { route, meta } of selectRoutes(manifest, null)) {
|
|
104
|
+
for (const locale of locales) {
|
|
105
|
+
const url = translator?.enabled ? translator.localize(route, locale) : route;
|
|
106
|
+
if (wanted && !wanted.has(route) && !wanted.has(url)) continue;
|
|
107
|
+
targets.push({ route, url, locale, meta });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return targets;
|
|
112
|
+
}
|
|
113
|
+
|
|
91
114
|
/** Neutral 1200×630 card. `template` replaces it entirely. */
|
|
92
115
|
export function buildCardHtml({ title, route, host, imageData, dark = false, template = null }) {
|
|
93
116
|
if (template) {
|
|
@@ -358,8 +381,22 @@ export async function generateImages(options = {}) {
|
|
|
358
381
|
...(options.selector ? { selector: options.selector } : {}),
|
|
359
382
|
...(options.outDir || options.out ? { outDir: options.outDir || options.out } : {})
|
|
360
383
|
};
|
|
361
|
-
|
|
362
|
-
|
|
384
|
+
|
|
385
|
+
// The translator is optional: an older framework, or a project that has not enabled it,
|
|
386
|
+
// yields one single-language target per route and the old manifest keys.
|
|
387
|
+
let frameworkSeo = options.framework || null;
|
|
388
|
+
let translator = options.translator ?? null;
|
|
389
|
+
if (translator === null) {
|
|
390
|
+
try {
|
|
391
|
+
frameworkSeo = frameworkSeo || await loadFrameworkSeo(projectRoot);
|
|
392
|
+
translator = await loadSeoTranslator(projectRoot, frameworkSeo);
|
|
393
|
+
} catch {
|
|
394
|
+
translator = null;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const targets = cardTargets(manifest, translator, only);
|
|
399
|
+
if (!targets.length) {
|
|
363
400
|
return { errors: [`no indexable route matched${only ? ` --only=${only}` : ''}`], files: [], patched: null };
|
|
364
401
|
}
|
|
365
402
|
|
|
@@ -409,15 +446,24 @@ export async function generateImages(options = {}) {
|
|
|
409
446
|
locale: config.locale || 'en-US'
|
|
410
447
|
});
|
|
411
448
|
|
|
412
|
-
for (const
|
|
449
|
+
for (const target of targets) {
|
|
413
450
|
const page = await captureContext.newPage();
|
|
414
451
|
const cardPage = await cardContext.newPage();
|
|
415
452
|
try {
|
|
416
453
|
await page.clock?.setFixedTime?.(FIXED_TIME).catch?.(() => {});
|
|
417
|
-
const capture = await captureRoute(page, {
|
|
454
|
+
const capture = await captureRoute(page, {
|
|
455
|
+
baseUrl,
|
|
456
|
+
route: target.url,
|
|
457
|
+
selector: config.selector,
|
|
458
|
+
runner: runnerModule
|
|
459
|
+
});
|
|
460
|
+
const rawTitle = translator?.enabled
|
|
461
|
+
? frameworkSeo?.resolveSeoText?.(target.meta.title, (key, params) => translator.t(target.locale || translator.defaultLocale, key, params))
|
|
462
|
+
: target.meta.title;
|
|
463
|
+
const title = typeof rawTitle === 'string' && rawTitle ? rawTitle : target.route;
|
|
418
464
|
const card = await encodeCard(cardPage, {
|
|
419
|
-
title
|
|
420
|
-
route,
|
|
465
|
+
title,
|
|
466
|
+
route: target.url,
|
|
421
467
|
host,
|
|
422
468
|
shot: capture.shot,
|
|
423
469
|
kind: capture.kind,
|
|
@@ -426,7 +472,8 @@ export async function generateImages(options = {}) {
|
|
|
426
472
|
});
|
|
427
473
|
|
|
428
474
|
const hash = createHash('sha256').update(card.buffer).digest('hex').slice(0, 10);
|
|
429
|
-
const
|
|
475
|
+
const prefix = target.locale ? `${target.locale}-` : '';
|
|
476
|
+
const fileName = `${prefix}${routeSlug(target.route)}-${hash}${card.ext}`;
|
|
430
477
|
const file = path.join(outDir, fileName);
|
|
431
478
|
|
|
432
479
|
if (card.buffer.length > budgetBytes) {
|
|
@@ -438,7 +485,7 @@ export async function generateImages(options = {}) {
|
|
|
438
485
|
if (!fs.existsSync(file)) await fsp.writeFile(file, card.buffer);
|
|
439
486
|
}
|
|
440
487
|
|
|
441
|
-
images[
|
|
488
|
+
images[target.url] = {
|
|
442
489
|
path: `/og/${fileName}`,
|
|
443
490
|
width: CARD.width,
|
|
444
491
|
height: CARD.height,
|
|
@@ -446,10 +493,10 @@ export async function generateImages(options = {}) {
|
|
|
446
493
|
bytes: card.buffer.length,
|
|
447
494
|
kind: capture.kind
|
|
448
495
|
};
|
|
449
|
-
files.push({ route, file, status: dryRun ? 'would-write' : 'written', bytes: card.buffer.length });
|
|
496
|
+
files.push({ route: target.url, file, status: dryRun ? 'would-write' : 'written', bytes: card.buffer.length });
|
|
450
497
|
} catch (error) {
|
|
451
498
|
failed += 1;
|
|
452
|
-
files.push({ route, status: 'failed', error: error.message });
|
|
499
|
+
files.push({ route: target.url, status: 'failed', error: error.message });
|
|
453
500
|
} finally {
|
|
454
501
|
await page.close();
|
|
455
502
|
await cardPage.close();
|
|
@@ -506,7 +553,7 @@ const readExistingManifest = (outDir) => {
|
|
|
506
553
|
};
|
|
507
554
|
|
|
508
555
|
/** `--check`: no browser, no server. Every indexable route must have a card that fits. */
|
|
509
|
-
export function checkImages({ projectRoot, only = null }) {
|
|
556
|
+
export function checkImages({ projectRoot, only = null, translator = null }) {
|
|
510
557
|
const { manifest } = loadManifest(projectRoot);
|
|
511
558
|
if (!manifest) return { errors: ['seo.json not found'], checks: [] };
|
|
512
559
|
|
|
@@ -515,24 +562,24 @@ export function checkImages({ projectRoot, only = null }) {
|
|
|
515
562
|
const outDir = path.isAbsolute(configuredOut) ? configuredOut : path.join(projectRoot, configuredOut);
|
|
516
563
|
const budgetBytes = Math.round((config.budgetKB || DEFAULT_BUDGET_KB) * 1024);
|
|
517
564
|
const cards = readExistingManifest(outDir)?.images || {};
|
|
518
|
-
const
|
|
565
|
+
const targets = cardTargets(manifest, translator, only);
|
|
519
566
|
const errors = [];
|
|
520
567
|
const checks = [];
|
|
521
568
|
|
|
522
|
-
for (const {
|
|
523
|
-
const card = cards[
|
|
524
|
-
if (!card) { errors.push(`${
|
|
569
|
+
for (const { url } of targets) {
|
|
570
|
+
const card = cards[url];
|
|
571
|
+
if (!card) { errors.push(`${url} has no card: run \`slice seo images\``); continue; }
|
|
525
572
|
const file = path.join(projectRoot, config.outDir || DEFAULT_OUT_DIR, path.basename(card.path));
|
|
526
|
-
if (!fs.existsSync(file)) { errors.push(`${
|
|
573
|
+
if (!fs.existsSync(file)) { errors.push(`${url}: missing ${card.path}`); continue; }
|
|
527
574
|
const buffer = fs.readFileSync(file);
|
|
528
575
|
const size = imageSize(buffer);
|
|
529
576
|
if (size.width !== CARD.width || size.height !== CARD.height) {
|
|
530
|
-
errors.push(`${
|
|
577
|
+
errors.push(`${url}: ${size.width}×${size.height}, expected ${CARD.width}×${CARD.height}`);
|
|
531
578
|
}
|
|
532
579
|
if (buffer.length > budgetBytes) {
|
|
533
|
-
errors.push(`${
|
|
580
|
+
errors.push(`${url}: ${(buffer.length / 1024).toFixed(1)} KB over the budget`);
|
|
534
581
|
}
|
|
535
|
-
checks.push(
|
|
582
|
+
checks.push(url);
|
|
536
583
|
}
|
|
537
584
|
|
|
538
585
|
return { errors, checks };
|
|
@@ -551,7 +598,15 @@ const STATUS = {
|
|
|
551
598
|
export default async function imagesCommand(options = {}, projectRoot) {
|
|
552
599
|
try {
|
|
553
600
|
if (options.check) {
|
|
554
|
-
|
|
601
|
+
let translator = options.translator ?? null;
|
|
602
|
+
if (translator === null) {
|
|
603
|
+
try {
|
|
604
|
+
translator = await loadSeoTranslator(projectRoot, await loadFrameworkSeo(projectRoot));
|
|
605
|
+
} catch {
|
|
606
|
+
translator = null;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
const result = checkImages({ projectRoot, only: options.only, translator });
|
|
555
610
|
if (result.errors.length) {
|
|
556
611
|
for (const error of result.errors) Print.error(error);
|
|
557
612
|
Print.error(`SEO images check failed: ${result.errors.length} problem(s).`);
|
|
@@ -580,7 +635,7 @@ export default async function imagesCommand(options = {}, projectRoot) {
|
|
|
580
635
|
process.exitCode = 1;
|
|
581
636
|
}
|
|
582
637
|
} catch (error) {
|
|
583
|
-
Print.error(
|
|
638
|
+
Print.error(`slice seo images failed: ${error.message}`);
|
|
584
639
|
process.exitCode = 1;
|
|
585
640
|
}
|
|
586
641
|
}
|
package/commands/seo/sitemap.js
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
buildRobotsTxt,
|
|
15
15
|
loadFrameworkSeo,
|
|
16
16
|
loadManifest,
|
|
17
|
+
loadSeoTranslator,
|
|
17
18
|
writeFileConfirmed
|
|
18
19
|
} from './support.js';
|
|
19
20
|
|
|
@@ -33,7 +34,8 @@ export async function generateSeoFiles(options = {}) {
|
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
const { errors, warnings } = framework.validateSeoManifest(manifest, {
|
|
36
|
-
publicDir: path.join(projectRoot, 'src', 'public')
|
|
37
|
+
publicDir: path.join(projectRoot, 'src', 'public'),
|
|
38
|
+
translator: await loadSeoTranslator(projectRoot, framework)
|
|
37
39
|
});
|
|
38
40
|
|
|
39
41
|
// A sitemap written with localhost URLs is a deploy bug, not a warning.
|
|
@@ -43,11 +45,12 @@ export async function generateSeoFiles(options = {}) {
|
|
|
43
45
|
if (errors.length) return { errors, warnings, files: [] };
|
|
44
46
|
|
|
45
47
|
const origin = framework.normalizeOrigin(manifest.origin);
|
|
48
|
+
const translator = await loadSeoTranslator(projectRoot, framework);
|
|
46
49
|
const files = [];
|
|
47
50
|
|
|
48
51
|
files.push(await writeFileConfirmed(
|
|
49
52
|
path.join(projectRoot, SITEMAP_FILE),
|
|
50
|
-
framework.buildSitemap({ manifest, origin }),
|
|
53
|
+
framework.buildSitemap({ manifest, origin, translator }),
|
|
51
54
|
{ yes, dryRun, confirm }
|
|
52
55
|
));
|
|
53
56
|
|
|
@@ -100,7 +103,7 @@ export default async function sitemapCommand(options = {}, projectRoot) {
|
|
|
100
103
|
process.exitCode = 1;
|
|
101
104
|
}
|
|
102
105
|
} catch (error) {
|
|
103
|
-
Print.error(
|
|
106
|
+
Print.error(`slice seo sitemap failed: ${error.message}`);
|
|
104
107
|
process.exitCode = 1;
|
|
105
108
|
}
|
|
106
109
|
}
|
package/commands/seo/support.js
CHANGED
|
@@ -12,6 +12,68 @@ export const MANIFEST_FILE = 'seo.json';
|
|
|
12
12
|
export const SITEMAP_FILE = path.join('src', 'public', 'sitemap.xml');
|
|
13
13
|
export const ROBOTS_FILE = path.join('src', 'public', 'robots.txt');
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* The app's translator (config + dictionaries) as the framework's SEO context, or null
|
|
17
|
+
* for a single-language app / an older framework. `src` wins over `dist`: the CLI reads
|
|
18
|
+
* the authoring copy.
|
|
19
|
+
*/
|
|
20
|
+
export async function loadSeoTranslator(projectRoot, framework) {
|
|
21
|
+
let translator = null;
|
|
22
|
+
for (const folder of ['src', 'dist']) {
|
|
23
|
+
const file = path.join(projectRoot, folder, 'sliceConfig.json');
|
|
24
|
+
if (!fs.existsSync(file)) continue;
|
|
25
|
+
try {
|
|
26
|
+
translator = JSON.parse(fs.readFileSync(file, 'utf8')).translator || null;
|
|
27
|
+
} catch {
|
|
28
|
+
translator = null;
|
|
29
|
+
}
|
|
30
|
+
if (translator) break;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (translator?.enabled !== true || typeof framework?.buildTranslatorContext !== 'function') {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const folder = String(translator.path || '/locales').replace(/^\/+|\/+$/g, '') || 'locales';
|
|
38
|
+
const locales = [
|
|
39
|
+
translator.defaultLocale,
|
|
40
|
+
...(Array.isArray(translator.locales) ? translator.locales : []),
|
|
41
|
+
translator.fallbackLocale
|
|
42
|
+
].filter((value) => typeof value === 'string' && value);
|
|
43
|
+
|
|
44
|
+
const dictionaries = {};
|
|
45
|
+
for (const locale of [...new Set(locales)]) {
|
|
46
|
+
for (const base of ['src', 'dist']) {
|
|
47
|
+
const root = path.join(projectRoot, base, folder);
|
|
48
|
+
const file = path.join(root, `${locale}.json`);
|
|
49
|
+
const namespaceDir = path.join(root, locale);
|
|
50
|
+
if (!fs.existsSync(file) && !fs.existsSync(namespaceDir)) continue;
|
|
51
|
+
let messages = {};
|
|
52
|
+
if (fs.existsSync(file)) {
|
|
53
|
+
try {
|
|
54
|
+
messages = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
55
|
+
} catch (error) {
|
|
56
|
+
throw new Error(`invalid translator dictionary ${path.join(base, folder, `${locale}.json`)}: ${error.message}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (fs.existsSync(namespaceDir) && fs.statSync(namespaceDir).isDirectory()) {
|
|
60
|
+
for (const name of fs.readdirSync(namespaceDir)) {
|
|
61
|
+
if (!name.endsWith('.json')) continue;
|
|
62
|
+
try {
|
|
63
|
+
messages[name.slice(0, -5)] = JSON.parse(fs.readFileSync(path.join(namespaceDir, name), 'utf8'));
|
|
64
|
+
} catch (error) {
|
|
65
|
+
throw new Error(`invalid translator dictionary ${path.join(base, folder, locale, name)}: ${error.message}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
dictionaries[locale] = messages;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return framework.buildTranslatorContext(translator, dictionaries);
|
|
75
|
+
}
|
|
76
|
+
|
|
15
77
|
export const resolveRoot = (moduleUrl) => getProjectRoot(moduleUrl);
|
|
16
78
|
|
|
17
79
|
/**
|
package/commands/setup/lint.js
CHANGED
|
@@ -120,7 +120,7 @@ export default async function setupLintCommand(options = {}, projectRoot) {
|
|
|
120
120
|
try {
|
|
121
121
|
result = await setupLint({ projectRoot, dryRun, withTypes: Boolean(options.withTypes) });
|
|
122
122
|
} catch (error) {
|
|
123
|
-
Print.error(
|
|
123
|
+
Print.error(`slice setup lint: ${error.message}`);
|
|
124
124
|
process.exitCode = 1;
|
|
125
125
|
return false;
|
|
126
126
|
}
|
package/commands/skill/skill.js
CHANGED
|
@@ -180,7 +180,7 @@ async function addSkill(options = {}) {
|
|
|
180
180
|
}
|
|
181
181
|
|
|
182
182
|
Print.success(`Skill '${manifest.name}' v${manifest.version} installed at ${manifest.installPath}/`);
|
|
183
|
-
Print.info("Reload
|
|
183
|
+
Print.info("Reload your agent session (Claude Code, OpenCode, …) to pick up the skill.");
|
|
184
184
|
return true;
|
|
185
185
|
}
|
|
186
186
|
|
|
@@ -213,7 +213,7 @@ async function statusSkill() {
|
|
|
213
213
|
const installDir = path.join(projectRoot, manifest.installPath);
|
|
214
214
|
const localVersion = readLocalVersion(installDir);
|
|
215
215
|
|
|
216
|
-
Print.title("Slice.js
|
|
216
|
+
Print.title("Slice.js developer skill");
|
|
217
217
|
console.log(` name: ${manifest.name}`);
|
|
218
218
|
console.log(` latest: v${manifest.version} (targets ${manifest.targets})`);
|
|
219
219
|
console.log(` installed: ${localVersion ? "v" + localVersion : "not installed"}`);
|
|
@@ -6,9 +6,11 @@ import { spawn } from 'child_process';
|
|
|
6
6
|
import { createServer } from 'net';
|
|
7
7
|
import setupWatcher, { stopWatcher } from './watchServer.js';
|
|
8
8
|
import Print from '../Print.js';
|
|
9
|
-
import { getApiPath, getSrcPath, getDistPath, getPath } from '../utils/PathHelper.js';
|
|
9
|
+
import { getApiPath, getSrcPath, getDistPath, getPath, getProjectRoot } from '../utils/PathHelper.js';
|
|
10
10
|
import { loadConfigSync } from '../utils/loadConfig.js';
|
|
11
11
|
import build from '../build/build.js';
|
|
12
|
+
import { generateDocs, hasDocumentationScaffold } from '../docs/index.js';
|
|
13
|
+
import { syncComponentsRegistry } from '../utils/componentCatalog.js';
|
|
12
14
|
|
|
13
15
|
/**
|
|
14
16
|
* Checks if a port is available
|
|
@@ -91,26 +93,46 @@ function startNodeServer(port, mode) {
|
|
|
91
93
|
});
|
|
92
94
|
|
|
93
95
|
let serverStarted = false;
|
|
94
|
-
let
|
|
96
|
+
let readyAnnounced = false;
|
|
97
|
+
let stdoutBuffer = '';
|
|
98
|
+
|
|
99
|
+
const isReadyLine = (line) =>
|
|
100
|
+
line.includes('Server running') ||
|
|
101
|
+
line.includes('listening on') ||
|
|
102
|
+
line.includes('Started on') ||
|
|
103
|
+
line.includes(`port ${port}`);
|
|
104
|
+
|
|
105
|
+
// The framework announces readiness on stdout. The CLI already prints the
|
|
106
|
+
// READY block, so the announcing line is consumed instead of forwarded;
|
|
107
|
+
// everything else the server logs still reaches the terminal.
|
|
108
|
+
const forwardLine = (line) => {
|
|
109
|
+
if (isReadyLine(line)) {
|
|
110
|
+
if (!readyAnnounced) {
|
|
111
|
+
readyAnnounced = true;
|
|
112
|
+
serverStarted = true;
|
|
113
|
+
Print.serverReady(port);
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
process.stdout.write(line);
|
|
118
|
+
};
|
|
95
119
|
|
|
96
|
-
// Capture output
|
|
120
|
+
// Capture output line by line so a readiness message split across chunks is
|
|
121
|
+
// still detected and suppressed exactly once.
|
|
97
122
|
serverProcess.stdout.on('data', (data) => {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
output.includes('Server running') ||
|
|
104
|
-
output.includes('listening on') ||
|
|
105
|
-
output.includes('Started on') ||
|
|
106
|
-
output.includes(`port ${port}`)
|
|
107
|
-
)) {
|
|
108
|
-
serverStarted = true;
|
|
109
|
-
Print.serverReady(port);
|
|
123
|
+
stdoutBuffer += data.toString();
|
|
124
|
+
let newline;
|
|
125
|
+
while ((newline = stdoutBuffer.indexOf('\n')) !== -1) {
|
|
126
|
+
forwardLine(stdoutBuffer.slice(0, newline + 1));
|
|
127
|
+
stdoutBuffer = stdoutBuffer.slice(newline + 1);
|
|
110
128
|
}
|
|
129
|
+
});
|
|
111
130
|
|
|
112
|
-
|
|
113
|
-
|
|
131
|
+
serverProcess.stdout.on('end', () => {
|
|
132
|
+
if (stdoutBuffer) {
|
|
133
|
+
forwardLine(stdoutBuffer);
|
|
134
|
+
stdoutBuffer = '';
|
|
135
|
+
}
|
|
114
136
|
});
|
|
115
137
|
|
|
116
138
|
serverProcess.stderr.on('data', (data) => {
|
|
@@ -135,6 +157,7 @@ function startNodeServer(port, mode) {
|
|
|
135
157
|
setTimeout(() => {
|
|
136
158
|
if (!serverStarted) {
|
|
137
159
|
serverStarted = true;
|
|
160
|
+
readyAnnounced = true;
|
|
138
161
|
Print.serverReady(port);
|
|
139
162
|
}
|
|
140
163
|
resolve(serverProcess);
|
|
@@ -166,7 +189,7 @@ export default async function startServer(options = {}) {
|
|
|
166
189
|
const { mode = 'development', port = defaultPort, watch = false } = options;
|
|
167
190
|
|
|
168
191
|
try {
|
|
169
|
-
Print.title(
|
|
192
|
+
Print.title(`Starting Slice.js ${mode} server`);
|
|
170
193
|
Print.newLine();
|
|
171
194
|
|
|
172
195
|
// Verify project structure
|
|
@@ -174,6 +197,22 @@ export default async function startServer(options = {}) {
|
|
|
174
197
|
throw new Error('Project structure not found. Run "slice init" first.');
|
|
175
198
|
}
|
|
176
199
|
|
|
200
|
+
// Keep the generated documentation current while developing. The build
|
|
201
|
+
// command checks it instead of writing, so CI catches a stale commit.
|
|
202
|
+
// Hand-written AGENTS.md files are left alone until the project opts in.
|
|
203
|
+
if (mode === 'development' && watch) {
|
|
204
|
+
const projectRoot = getProjectRoot(import.meta.url);
|
|
205
|
+
try {
|
|
206
|
+
// Keep components.js current without printing the component catalog.
|
|
207
|
+
await syncComponentsRegistry(projectRoot);
|
|
208
|
+
if (await hasDocumentationScaffold(projectRoot)) {
|
|
209
|
+
await generateDocs({ projectRoot, target: 'all' });
|
|
210
|
+
}
|
|
211
|
+
} catch (error) {
|
|
212
|
+
Print.warning(`Could not generate project docs: ${error.message}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
177
216
|
let actualPort = await isPortAvailable(port) ? port : port + 1; // Try one more port
|
|
178
217
|
if(actualPort !== port) {
|
|
179
218
|
// Check if the fallback is available
|
|
@@ -181,10 +220,10 @@ export default async function startServer(options = {}) {
|
|
|
181
220
|
if(!fallbackAvailable) {
|
|
182
221
|
throw new Error(`Ports ${port} and ${actualPort} are in use.`);
|
|
183
222
|
}
|
|
184
|
-
Print.info(
|
|
223
|
+
Print.info(`Port ${port} is busy; using ${actualPort}.`);
|
|
185
224
|
}
|
|
186
225
|
|
|
187
|
-
Print.serverStatus('checking', `Port ${actualPort} available
|
|
226
|
+
Print.serverStatus('checking', `Port ${actualPort} available`);
|
|
188
227
|
Print.newLine();
|
|
189
228
|
|
|
190
229
|
if (mode === 'production') {
|
|
@@ -218,7 +257,7 @@ export default async function startServer(options = {}) {
|
|
|
218
257
|
await new Promise(r => setTimeout(r, 500));
|
|
219
258
|
|
|
220
259
|
try {
|
|
221
|
-
Print.info('
|
|
260
|
+
Print.info('File changed. Restarting server...');
|
|
222
261
|
|
|
223
262
|
currentServerProcess = await startNodeServer(actualPort, mode);
|
|
224
263
|
} catch (e) {
|
|
@@ -8,7 +8,7 @@ import Print from '../Print.js';
|
|
|
8
8
|
* @returns {FSWatcher} - Chokidar watcher
|
|
9
9
|
*/
|
|
10
10
|
export default function setupWatcher(serverProcess, onRestart) {
|
|
11
|
-
Print.info('Watch mode enabled
|
|
11
|
+
Print.info('Watch mode enabled; monitoring changes');
|
|
12
12
|
Print.newLine();
|
|
13
13
|
|
|
14
14
|
const watcher = chokidar.watch(['api/**/*'], {
|
|
@@ -33,7 +33,7 @@ export default function setupWatcher(serverProcess, onRestart) {
|
|
|
33
33
|
clearTimeout(reloadTimeout);
|
|
34
34
|
reloadTimeout = setTimeout(() => {
|
|
35
35
|
if (onRestart) {
|
|
36
|
-
console.log(chalk.yellow('
|
|
36
|
+
console.log(chalk.yellow('API change detected. Restarting server...'));
|
|
37
37
|
onRestart(changedPath);
|
|
38
38
|
}
|
|
39
39
|
}, 500);
|
|
@@ -47,7 +47,7 @@ export default function setupWatcher(serverProcess, onRestart) {
|
|
|
47
47
|
Print.error(`Watcher error: ${error.message}`);
|
|
48
48
|
})
|
|
49
49
|
.on('ready', () => {
|
|
50
|
-
console.log(chalk.gray('
|
|
50
|
+
console.log(chalk.gray('Watching for changes...'));
|
|
51
51
|
Print.newLine();
|
|
52
52
|
});
|
|
53
53
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// `slice translator check` — the strict gate: every key a template or a `slice.t()` call
|
|
2
|
+
// needs must exist in every locale (a missing key ships as the key on screen).
|
|
3
|
+
//
|
|
4
|
+
// The scan reuses the framework's template rules and the dictionaries the app ships, so
|
|
5
|
+
// this command and the browser cannot disagree about what a `data-t` element needs.
|
|
6
|
+
|
|
7
|
+
import Print from '../Print.js';
|
|
8
|
+
import { checkProject } from './scan.js';
|
|
9
|
+
|
|
10
|
+
export default async function checkCommand(options = {}, projectRoot) {
|
|
11
|
+
try {
|
|
12
|
+
const result = await checkProject(projectRoot, options);
|
|
13
|
+
|
|
14
|
+
for (const warning of result.warnings) Print.warning(warning);
|
|
15
|
+
|
|
16
|
+
if (result.errors.length) {
|
|
17
|
+
for (const error of result.errors) Print.error(error);
|
|
18
|
+
Print.error(`Translator check failed: ${result.errors.length} problem(s).`);
|
|
19
|
+
process.exitCode = 1;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
Print.success(
|
|
24
|
+
`Translator OK: ${result.entries.length} key reference(s) across ${result.locales.length} locale(s)` +
|
|
25
|
+
` (${result.dictionaryCount} dictionaries` +
|
|
26
|
+
`${result.contentCount ? `, ${result.contentCount} content document(s)` : ''}).`
|
|
27
|
+
);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
Print.error(`slice translator check failed: ${error.message}`);
|
|
30
|
+
process.exitCode = 1;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// `slice translator init` — add the translator block to sliceConfig and create the
|
|
2
|
+
// dictionaries folder. Idempotent: existing values and files are never overwritten.
|
|
3
|
+
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import Print from '../Print.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @returns {{ errors: string[], files: Array<{file: string, status: string}>, configFile: string }}
|
|
10
|
+
*/
|
|
11
|
+
export function initTranslatorFiles(options = {}) {
|
|
12
|
+
const { projectRoot, dryRun = false, localePath = '/locales' } = options;
|
|
13
|
+
const locales = (Array.isArray(options.locales) ? options.locales : [options.locales || 'en'])
|
|
14
|
+
.map((value) => String(value).trim())
|
|
15
|
+
.filter(Boolean);
|
|
16
|
+
const defaultLocale = options.defaultLocale || locales[0] || 'en';
|
|
17
|
+
const errors = [];
|
|
18
|
+
const files = [];
|
|
19
|
+
|
|
20
|
+
const configFile = path.join(projectRoot, 'src', 'sliceConfig.json');
|
|
21
|
+
if (!fs.existsSync(configFile)) {
|
|
22
|
+
return { errors: ['src/sliceConfig.json not found: run `slice init` first'], files, configFile };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let config;
|
|
26
|
+
try {
|
|
27
|
+
config = JSON.parse(fs.readFileSync(configFile, 'utf8'));
|
|
28
|
+
} catch (error) {
|
|
29
|
+
return { errors: [`src/sliceConfig.json is not valid JSON: ${error.message}`], files, configFile };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const existing = config.translator && typeof config.translator === 'object' ? config.translator : null;
|
|
33
|
+
const declared = [...new Set([
|
|
34
|
+
existing?.defaultLocale || defaultLocale,
|
|
35
|
+
...(Array.isArray(existing?.locales) ? existing.locales : []),
|
|
36
|
+
...locales
|
|
37
|
+
])];
|
|
38
|
+
const translator = existing
|
|
39
|
+
? {
|
|
40
|
+
...existing,
|
|
41
|
+
// Re-running init must not turn a deliberately disabled translator back on.
|
|
42
|
+
enabled: existing.enabled !== undefined ? existing.enabled : true,
|
|
43
|
+
defaultLocale: existing.defaultLocale || defaultLocale,
|
|
44
|
+
locales: declared,
|
|
45
|
+
path: existing.path || localePath
|
|
46
|
+
}
|
|
47
|
+
: {
|
|
48
|
+
enabled: true,
|
|
49
|
+
defaultLocale,
|
|
50
|
+
locales: declared,
|
|
51
|
+
path: localePath,
|
|
52
|
+
saveLocally: true,
|
|
53
|
+
useBrowserLocale: true,
|
|
54
|
+
localePrefix: false
|
|
55
|
+
};
|
|
56
|
+
const next = { ...config, translator };
|
|
57
|
+
|
|
58
|
+
const before = fs.readFileSync(configFile, 'utf8');
|
|
59
|
+
const after = `${JSON.stringify(next, null, 2)}\n`;
|
|
60
|
+
const alreadyConfigured = existing?.enabled === true
|
|
61
|
+
&& existing.defaultLocale
|
|
62
|
+
&& existing.path
|
|
63
|
+
&& Array.isArray(existing.locales)
|
|
64
|
+
&& JSON.stringify(existing.locales) === JSON.stringify(declared);
|
|
65
|
+
if (alreadyConfigured) {
|
|
66
|
+
// Nothing to add: never reformat a config that is already set up.
|
|
67
|
+
files.push({ file: configFile, status: 'unchanged' });
|
|
68
|
+
} else if (before !== after) {
|
|
69
|
+
if (!dryRun) fs.writeFileSync(configFile, after);
|
|
70
|
+
files.push({ file: configFile, status: dryRun ? 'would-change' : 'updated' });
|
|
71
|
+
} else {
|
|
72
|
+
files.push({ file: configFile, status: 'unchanged' });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const folder = String(next.translator.path || '/locales').replace(/^\/+|\/+$/g, '') || 'locales';
|
|
76
|
+
for (const locale of new Set([next.translator.defaultLocale, ...(next.translator.locales || [])])) {
|
|
77
|
+
const file = path.join(projectRoot, 'src', folder, `${locale}.json`);
|
|
78
|
+
if (fs.existsSync(file)) {
|
|
79
|
+
files.push({ file, status: 'exists' });
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (!dryRun) {
|
|
83
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
84
|
+
fs.writeFileSync(file, '{}\n');
|
|
85
|
+
}
|
|
86
|
+
files.push({ file, status: dryRun ? 'would-create' : 'created' });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return { errors, files, configFile };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export default async function initCommand(options = {}, projectRoot) {
|
|
93
|
+
try {
|
|
94
|
+
const locales = String(options.locales || options.locale || 'en')
|
|
95
|
+
.split(',')
|
|
96
|
+
.map((value) => value.trim())
|
|
97
|
+
.filter(Boolean);
|
|
98
|
+
const result = initTranslatorFiles({
|
|
99
|
+
projectRoot,
|
|
100
|
+
locales,
|
|
101
|
+
defaultLocale: options.default,
|
|
102
|
+
localePath: options.path,
|
|
103
|
+
dryRun: options.dryRun === true
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
if (result.errors.length) {
|
|
107
|
+
for (const error of result.errors) Print.error(error);
|
|
108
|
+
process.exitCode = 1;
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (const entry of result.files) {
|
|
113
|
+
const name = path.relative(projectRoot, entry.file);
|
|
114
|
+
if (entry.status === 'created' || entry.status === 'updated') Print.success(`${name}: ${entry.status}`);
|
|
115
|
+
else if (entry.status === 'would-create' || entry.status === 'would-change') Print.info(`${name}: ${entry.status}`);
|
|
116
|
+
else Print.info(`${name}: ${entry.status}`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
Print.info('Dictionaries live in that folder; templates use data-t="key" and code uses slice.t("key").');
|
|
120
|
+
Print.info('Next: `slice translator check`.');
|
|
121
|
+
} catch (error) {
|
|
122
|
+
Print.error(`slice translator init failed: ${error.message}`);
|
|
123
|
+
process.exitCode = 1;
|
|
124
|
+
}
|
|
125
|
+
}
|