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
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
// `slice translator check` ā find the failures that only show up as a key on screen.
|
|
2
|
+
//
|
|
3
|
+
// The template rules are the framework's own (`Translator/dom.js`), so the CLI and the
|
|
4
|
+
// browser cannot disagree about which keys a `data-t` element needs.
|
|
5
|
+
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { createRequire } from 'node:module';
|
|
9
|
+
import { pathToFileURL } from 'node:url';
|
|
10
|
+
|
|
11
|
+
const SKIP_DIRS = new Set(['node_modules', 'dist', '.git', 'locales', 'public']);
|
|
12
|
+
const SKIP_FILES = /(\.spec|\.test|\.generated)\.js$/i;
|
|
13
|
+
|
|
14
|
+
const require = createRequire(import.meta.url);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Resolve the framework's `dom.js` from the project (the version the app runs) or from
|
|
18
|
+
* the CLI's own dependency. Returns null when neither has it (older framework).
|
|
19
|
+
*/
|
|
20
|
+
export async function loadTemplateRules(projectRoot) {
|
|
21
|
+
const candidates = [
|
|
22
|
+
path.join(projectRoot, 'node_modules', 'slicejs-web-framework', 'Slice', 'Components', 'Structural', 'Translator', 'dom.js')
|
|
23
|
+
];
|
|
24
|
+
try {
|
|
25
|
+
const entry = require.resolve('slicejs-web-framework');
|
|
26
|
+
candidates.push(path.resolve(path.dirname(entry), '..', 'Slice', 'Components', 'Structural', 'Translator', 'dom.js'));
|
|
27
|
+
} catch {
|
|
28
|
+
// not installed next to the CLI either
|
|
29
|
+
}
|
|
30
|
+
const file = candidates.find((candidate) => fs.existsSync(candidate));
|
|
31
|
+
if (!file) return null;
|
|
32
|
+
return import(pathToFileURL(file).href);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Resolve the framework's `content.js` (copy resolver + validator). Returns null when the
|
|
37
|
+
* installed framework is older and does not ship it, so content validation degrades to the
|
|
38
|
+
* file-existence check instead of failing the whole command.
|
|
39
|
+
*/
|
|
40
|
+
export async function loadContentRules(projectRoot) {
|
|
41
|
+
const candidates = [
|
|
42
|
+
path.join(projectRoot, 'node_modules', 'slicejs-web-framework', 'Slice', 'Components', 'Structural', 'Translator', 'content.js')
|
|
43
|
+
];
|
|
44
|
+
try {
|
|
45
|
+
const entry = require.resolve('slicejs-web-framework');
|
|
46
|
+
candidates.push(path.resolve(path.dirname(entry), '..', 'Slice', 'Components', 'Structural', 'Translator', 'content.js'));
|
|
47
|
+
} catch {
|
|
48
|
+
// not installed next to the CLI either
|
|
49
|
+
}
|
|
50
|
+
const file = candidates.find((candidate) => fs.existsSync(candidate));
|
|
51
|
+
if (!file) return null;
|
|
52
|
+
return import(pathToFileURL(file).href);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const ATTRIBUTE = /([^\s=/>]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
|
|
56
|
+
function parseAttributes(source) {
|
|
57
|
+
const attributes = {};
|
|
58
|
+
for (const match of String(source ?? '').matchAll(ATTRIBUTE)) {
|
|
59
|
+
const name = match[1].toLowerCase();
|
|
60
|
+
attributes[name] = match[2] ?? match[3] ?? match[4] ?? '';
|
|
61
|
+
}
|
|
62
|
+
return attributes;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Every `data-t` key an HTML string needs: the element's text, the attribute keys
|
|
67
|
+
* (`key.ariaLabel`) and the `data-t-*` params used to resolve plurals.
|
|
68
|
+
* `rules` is the framework's dom.js module.
|
|
69
|
+
*/
|
|
70
|
+
export function scanHtml(html, rules, where = '') {
|
|
71
|
+
const entries = [];
|
|
72
|
+
for (const match of String(html).matchAll(/<([a-zA-Z][\w-]*)((?:\s+[^<>]*?)?)\/?>/g)) {
|
|
73
|
+
const tagName = match[1].toUpperCase();
|
|
74
|
+
const attributes = parseAttributes(match[2]);
|
|
75
|
+
if (!attributes['data-t']) continue;
|
|
76
|
+
|
|
77
|
+
const element = {
|
|
78
|
+
tagName,
|
|
79
|
+
hasAttribute: (name) => Object.prototype.hasOwnProperty.call(attributes, name),
|
|
80
|
+
getAttribute: (name) => (Object.prototype.hasOwnProperty.call(attributes, name) ? attributes[name] : null)
|
|
81
|
+
};
|
|
82
|
+
const keys = rules.templateKeys(element);
|
|
83
|
+
if (!keys) continue;
|
|
84
|
+
|
|
85
|
+
const params = Object.keys(attributes)
|
|
86
|
+
.filter((name) => name.startsWith('data-t-'))
|
|
87
|
+
.map((name) => name.slice(7))
|
|
88
|
+
.filter((name) => name && !rules.RESERVED_PARAMS.includes(name))
|
|
89
|
+
.map((name) => rules.camelCase(name));
|
|
90
|
+
|
|
91
|
+
if (keys.text) entries.push({ key: keys.text, where, kind: 'text', params });
|
|
92
|
+
for (const [attribute, key] of Object.entries(keys.attributes)) {
|
|
93
|
+
entries.push({ key, where, kind: 'attribute', attribute, params });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return entries;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** `slice.t('key')`, `slice.t.ref('key')`, `slice.t.list('key')`, `slice.t.ref.list('key')`,
|
|
100
|
+
* `slice.translator.t('key')`, and local helpers named `tr()`/`translate()`. */
|
|
101
|
+
export function scanJs(source, where = '') {
|
|
102
|
+
const entries = [];
|
|
103
|
+
const pattern = /(?:slice\.t\.ref\.list|slice\.t\.list|slice\.t\.ref|(?:slice(?:\.translator)?|translator)\.(?:refList|t|ref|list)|(?<![.\w$])(?:\$t|tr|translate))\(\s*(['"])([^'"]+)\1/g;
|
|
104
|
+
for (const match of String(source).matchAll(pattern)) {
|
|
105
|
+
const call = match[0];
|
|
106
|
+
entries.push({
|
|
107
|
+
key: match[2],
|
|
108
|
+
where,
|
|
109
|
+
kind: /\.list\s*\(/.test(call) || /\.refList\s*\(/.test(call) ? 'list' : 'code'
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return entries;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** `slice.t.content('name')`: a content document, not a dictionary key. */
|
|
116
|
+
export function scanContentCalls(source, where = '') {
|
|
117
|
+
const calls = [];
|
|
118
|
+
const pattern = /(?:slice\.t|slice\.translator|translator)\.content\(\s*(['"])([^'"]+)\1/g;
|
|
119
|
+
for (const match of String(source).matchAll(pattern)) {
|
|
120
|
+
calls.push({ name: match[2], where });
|
|
121
|
+
}
|
|
122
|
+
return calls;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Raw `href="/path"` inside an anchor (they lose the locale with routing on). */
|
|
126
|
+
export function scanRawHrefs(html) {
|
|
127
|
+
const hrefs = [];
|
|
128
|
+
for (const tag of String(html).matchAll(/<a\b[^>]*>/gi)) {
|
|
129
|
+
const match = /\bhref\s*=\s*(['"])(\/[^'"]*)\1/i.exec(tag[0]);
|
|
130
|
+
if (!match) continue;
|
|
131
|
+
const href = match[2];
|
|
132
|
+
if (href.startsWith('//') || href.startsWith('{{')) continue;
|
|
133
|
+
hrefs.push(href);
|
|
134
|
+
}
|
|
135
|
+
return hrefs;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function walk(directory, files = []) {
|
|
139
|
+
if (!fs.existsSync(directory)) return files;
|
|
140
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
141
|
+
if (entry.isDirectory()) {
|
|
142
|
+
if (SKIP_DIRS.has(entry.name)) continue;
|
|
143
|
+
walk(path.join(directory, entry.name), files);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (SKIP_FILES.test(entry.name)) continue;
|
|
147
|
+
if (/\.(html|js)$/i.test(entry.name)) files.push(path.join(directory, entry.name));
|
|
148
|
+
}
|
|
149
|
+
return files;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Every `.html`/`.js` the app ships (never `locales/`, `public/` or `node_modules/`). */
|
|
153
|
+
export function sourceFiles(projectRoot) {
|
|
154
|
+
const src = path.join(projectRoot, 'src');
|
|
155
|
+
const files = new Set(walk(path.join(src, 'Components')));
|
|
156
|
+
for (const extra of [path.join(src, 'App', 'index.html'), path.join(src, 'App', 'index.js'), path.join(src, 'routes.js')]) {
|
|
157
|
+
if (fs.existsSync(extra)) files.add(extra);
|
|
158
|
+
}
|
|
159
|
+
return [...files].sort();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Read every source file and collect the keys, the raw hrefs and the dictionaries. */
|
|
163
|
+
export async function collectProject(projectRoot, options = {}) {
|
|
164
|
+
const rules = options.rules || await loadTemplateRules(projectRoot);
|
|
165
|
+
if (!rules) {
|
|
166
|
+
throw new Error('The installed slicejs-web-framework has no Translator/template rules: update the framework.');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const entries = [];
|
|
170
|
+
const rawHrefs = [];
|
|
171
|
+
const templates = [];
|
|
172
|
+
const contentCalls = [];
|
|
173
|
+
for (const file of sourceFiles(projectRoot)) {
|
|
174
|
+
const where = path.relative(projectRoot, file);
|
|
175
|
+
const content = fs.readFileSync(file, 'utf8');
|
|
176
|
+
if (file.endsWith('.html')) {
|
|
177
|
+
templates.push({ file, where, content });
|
|
178
|
+
entries.push(...scanHtml(content, rules, where));
|
|
179
|
+
for (const href of scanRawHrefs(content)) rawHrefs.push({ where, href });
|
|
180
|
+
} else {
|
|
181
|
+
entries.push(...scanJs(content, where));
|
|
182
|
+
contentCalls.push(...scanContentCalls(content, where));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const config = readTranslatorConfig(projectRoot);
|
|
187
|
+
const dictionaries = config?.enabled ? readDictionaries(projectRoot, config) : {};
|
|
188
|
+
const content = config?.enabled ? readContentDocuments(projectRoot, config) : { documents: {}, errors: [] };
|
|
189
|
+
const contentRules = options.contentRules || await loadContentRules(projectRoot);
|
|
190
|
+
|
|
191
|
+
return { rules, config, dictionaries, entries, rawHrefs, templates, contentCalls, content, contentRules };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function readTranslatorConfig(projectRoot) {
|
|
195
|
+
for (const folder of ['src', 'dist']) {
|
|
196
|
+
const file = path.join(projectRoot, folder, 'sliceConfig.json');
|
|
197
|
+
if (!fs.existsSync(file)) continue;
|
|
198
|
+
try {
|
|
199
|
+
return JSON.parse(fs.readFileSync(file, 'utf8')).translator || null;
|
|
200
|
+
} catch (error) {
|
|
201
|
+
throw new Error(`invalid ${folder}/sliceConfig.json: ${error.message}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function readDictionaries(projectRoot, config) {
|
|
208
|
+
const folder = String(config.path || '/locales').replace(/^\/+|\/+$/g, '') || 'locales';
|
|
209
|
+
const locales = [
|
|
210
|
+
config.defaultLocale,
|
|
211
|
+
...(Array.isArray(config.locales) ? config.locales : []),
|
|
212
|
+
config.fallbackLocale
|
|
213
|
+
].filter((value) => typeof value === 'string' && value);
|
|
214
|
+
|
|
215
|
+
const dictionaries = {};
|
|
216
|
+
for (const locale of [...new Set(locales)]) {
|
|
217
|
+
for (const base of ['src', 'dist']) {
|
|
218
|
+
const root = path.join(projectRoot, base, folder);
|
|
219
|
+
const file = path.join(root, `${locale}.json`);
|
|
220
|
+
const namespaceDir = path.join(root, locale);
|
|
221
|
+
if (!fs.existsSync(file) && !fs.existsSync(namespaceDir)) continue;
|
|
222
|
+
let messages = {};
|
|
223
|
+
if (fs.existsSync(file)) {
|
|
224
|
+
try {
|
|
225
|
+
messages = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
226
|
+
} catch (error) {
|
|
227
|
+
throw new Error(`invalid translator dictionary ${path.join(base, folder, `${locale}.json`)}: ${error.message}`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (fs.existsSync(namespaceDir) && fs.statSync(namespaceDir).isDirectory()) {
|
|
231
|
+
for (const name of fs.readdirSync(namespaceDir)) {
|
|
232
|
+
if (!name.endsWith('.json')) continue;
|
|
233
|
+
try {
|
|
234
|
+
messages[name.slice(0, -5)] = JSON.parse(fs.readFileSync(path.join(namespaceDir, name), 'utf8'));
|
|
235
|
+
} catch (error) {
|
|
236
|
+
throw new Error(`invalid translator dictionary ${path.join(base, folder, locale, name)}: ${error.message}`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
dictionaries[locale] = messages;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return dictionaries;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Every content document under `<path>/content/` (`src` wins over `dist`). Names are the
|
|
249
|
+
* file paths without `.json`, using `/` (`teaching`, `teaching/pr2`).
|
|
250
|
+
* @param {string} projectRoot
|
|
251
|
+
* @param {{ path?: string }} config
|
|
252
|
+
* @returns {{ documents: Record<string, any>, errors: string[] }}
|
|
253
|
+
*/
|
|
254
|
+
export function readContentDocuments(projectRoot, config) {
|
|
255
|
+
const folder = String(config.path || '/locales').replace(/^\/+|\/+$/g, '') || 'locales';
|
|
256
|
+
const documents = {};
|
|
257
|
+
const errors = [];
|
|
258
|
+
|
|
259
|
+
for (const base of ['src', 'dist']) {
|
|
260
|
+
const root = path.join(projectRoot, base, folder, 'content');
|
|
261
|
+
if (!fs.existsSync(root) || !fs.statSync(root).isDirectory()) continue;
|
|
262
|
+
|
|
263
|
+
const walk = (directory) => {
|
|
264
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
265
|
+
const full = path.join(directory, entry.name);
|
|
266
|
+
if (entry.isDirectory()) {
|
|
267
|
+
walk(full);
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (!entry.name.endsWith('.json')) continue;
|
|
271
|
+
const name = path.relative(root, full).split(path.sep).join('/').slice(0, -5);
|
|
272
|
+
try {
|
|
273
|
+
documents[name] = JSON.parse(fs.readFileSync(full, 'utf8'));
|
|
274
|
+
} catch (error) {
|
|
275
|
+
errors.push(`invalid content document ${path.relative(projectRoot, full)}: ${error.message}`);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
walk(root);
|
|
280
|
+
if (Object.keys(documents).length > 0 || errors.length > 0) break;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return { documents, errors };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function lookup(dictionary, key) { let node = dictionary;
|
|
287
|
+
for (const part of String(key).split('.')) {
|
|
288
|
+
if (!node || typeof node !== 'object' || !Object.prototype.hasOwnProperty.call(node, part)) return undefined;
|
|
289
|
+
node = node[part];
|
|
290
|
+
}
|
|
291
|
+
return node;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function flatten(dictionary, prefix = '', out = []) {
|
|
295
|
+
for (const [key, value] of Object.entries(dictionary || {})) {
|
|
296
|
+
const full = prefix ? `${prefix}.${key}` : key;
|
|
297
|
+
if (value && typeof value === 'object' && !Array.isArray(value) && typeof value.other !== 'string') {
|
|
298
|
+
flatten(value, full, out);
|
|
299
|
+
} else {
|
|
300
|
+
out.push(full);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return out;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const paramsOf = (text) => new Set([...String(text).matchAll(/\{([A-Za-z_$][\w$]*)\}/g)].map((match) => match[1]));
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* The rules of the feature: missing key per locale, plural without `other`, params that
|
|
310
|
+
* differ between locales, dead keys and raw links that lose the locale.
|
|
311
|
+
*/
|
|
312
|
+
export function checkTranslations({ entries, dictionaries, config, rawHrefs = [], reportUnused = false, content = { documents: {}, errors: [] }, contentCalls = [], contentRules = null }) {
|
|
313
|
+
const errors = [];
|
|
314
|
+
const warnings = [];
|
|
315
|
+
const locales = config?.enabled
|
|
316
|
+
? [...new Set([config.defaultLocale, ...(config.locales || [])].filter(Boolean))]
|
|
317
|
+
: [];
|
|
318
|
+
|
|
319
|
+
if (!config?.enabled) {
|
|
320
|
+
return { errors: ['translator is not enabled in sliceConfig.json: run `slice translator init`'], warnings };
|
|
321
|
+
}
|
|
322
|
+
if (typeof config.defaultLocale !== 'string' || !config.defaultLocale.trim()) {
|
|
323
|
+
return {
|
|
324
|
+
errors: ['translator.enabled is true but defaultLocale is missing: the runtime stays disabled (set "defaultLocale" or "enabled": false)'],
|
|
325
|
+
warnings
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
if (locales.length === 0) {
|
|
329
|
+
return { errors: ['translator has no locales configured'], warnings };
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const seen = new Set();
|
|
333
|
+
for (const entry of entries) {
|
|
334
|
+
// Library defaults (`slice.*`) are registered by the component itself in the
|
|
335
|
+
// translator's defaults layer; the app is not required to copy them.
|
|
336
|
+
if (entry.key.startsWith('slice.')) continue;
|
|
337
|
+
const id = `${entry.kind || 'code'}|${entry.key}|${entry.attribute || ''}`;
|
|
338
|
+
if (seen.has(id)) continue;
|
|
339
|
+
seen.add(id);
|
|
340
|
+
|
|
341
|
+
const where = `${entry.where}${entry.attribute ? ` (${entry.attribute})` : ''}`;
|
|
342
|
+
if (entry.kind === 'list') {
|
|
343
|
+
for (const locale of locales) {
|
|
344
|
+
const value = lookup(dictionaries[locale], entry.key)
|
|
345
|
+
?? lookup(dictionaries[config.fallbackLocale], entry.key);
|
|
346
|
+
if (value === undefined) errors.push(`missing list "${entry.key}" in "${locale}" (${where})`);
|
|
347
|
+
else if (!Array.isArray(value)) errors.push(`"${entry.key}" in "${locale}" is not a list (${where})`);
|
|
348
|
+
}
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const texts = {};
|
|
353
|
+
for (const locale of locales) {
|
|
354
|
+
const value = lookup(dictionaries[locale], entry.key)
|
|
355
|
+
?? lookup(dictionaries[config.fallbackLocale], entry.key);
|
|
356
|
+
if (value === undefined) {
|
|
357
|
+
errors.push(`missing "${entry.key}" in "${locale}" (${where})`);
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
if (value && typeof value === 'object' && typeof value.other !== 'string') {
|
|
361
|
+
errors.push(`"${entry.key}" in "${locale}" is a group or a plural without "other" (${where})`);
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
texts[locale] = typeof value === 'string' ? value : value.other;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const withText = Object.entries(texts);
|
|
368
|
+
if (withText.length > 1) {
|
|
369
|
+
const expected = paramsOf(withText[0][1]);
|
|
370
|
+
for (const [locale, text] of withText.slice(1)) {
|
|
371
|
+
const actual = paramsOf(text);
|
|
372
|
+
for (const param of expected) {
|
|
373
|
+
if (!actual.has(param)) warnings.push(`"${entry.key}" (${locale}) is missing {${param}} used by ${withText[0][0]} (${where})`);
|
|
374
|
+
}
|
|
375
|
+
for (const param of actual) {
|
|
376
|
+
if (!expected.has(param)) warnings.push(`"${entry.key}" (${locale}) uses {${param}} which ${withText[0][0]} does not (${where})`);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Content documents: a `slice.t.content('name')` needs `locales/content/name.json`,
|
|
383
|
+
// and every `copy` needs a branch for each locale with the same shape.
|
|
384
|
+
const contentLocales = [...new Set([...locales, config.fallbackLocale].filter(Boolean))];
|
|
385
|
+
const missingDocuments = new Set();
|
|
386
|
+
for (const { name, where } of contentCalls) {
|
|
387
|
+
if (content.documents[name] === undefined && !missingDocuments.has(name)) {
|
|
388
|
+
missingDocuments.add(name);
|
|
389
|
+
errors.push(`missing content document "${name}" (${where})`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
for (const error of content.errors || []) errors.push(error);
|
|
393
|
+
if (contentRules && typeof contentRules.validateContentDocument === 'function') {
|
|
394
|
+
for (const [name, document] of Object.entries(content.documents || {})) {
|
|
395
|
+
for (const issue of contentRules.validateContentDocument(document, contentLocales)) {
|
|
396
|
+
errors.push(`content/${name}.json${issue.path ? `: ${issue.path}` : ''}: ${issue.message}`);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// Keys nobody reads: cheap to spot, expensive to keep forever. Opt-in: keys reached
|
|
402
|
+
// through an app helper (`tr(key)`, a table of labels) are invisible to the scan, so
|
|
403
|
+
// this would otherwise report false positives.
|
|
404
|
+
if (reportUnused) {
|
|
405
|
+
const used = new Set(entries.map((entry) => entry.key));
|
|
406
|
+
const unused = [];
|
|
407
|
+
for (const locale of locales) {
|
|
408
|
+
for (const key of flatten(dictionaries[locale])) {
|
|
409
|
+
if (used.has(key) || key.startsWith('slice.')) continue;
|
|
410
|
+
if (!unused.includes(key)) unused.push(key);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
for (const key of unused.slice(0, 20)) warnings.push(`unused key "${key}"`);
|
|
414
|
+
if (unused.length > 20) warnings.push(`ā¦and ${unused.length - 20} more unused keys`);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (config.localePrefix) {
|
|
418
|
+
for (const { where, href } of rawHrefs) {
|
|
419
|
+
warnings.push(`raw href="${href}" in ${where} loses the locale: use slice.router.href()`);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
return { errors, warnings };
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** Everything `slice translator check` and the doctor row need. */
|
|
427
|
+
export async function checkProject(projectRoot, options = {}) {
|
|
428
|
+
const collected = await collectProject(projectRoot, options);
|
|
429
|
+
const result = checkTranslations({
|
|
430
|
+
...collected,
|
|
431
|
+
reportUnused: options.unused === true
|
|
432
|
+
});
|
|
433
|
+
const locales = collected.config?.enabled
|
|
434
|
+
? [...new Set([collected.config.defaultLocale, ...(collected.config.locales || [])].filter(Boolean))]
|
|
435
|
+
: [];
|
|
436
|
+
return {
|
|
437
|
+
...result,
|
|
438
|
+
config: collected.config,
|
|
439
|
+
entries: collected.entries,
|
|
440
|
+
locales,
|
|
441
|
+
dictionaryCount: Object.keys(collected.dictionaries).length,
|
|
442
|
+
contentCount: Object.keys(collected.content?.documents || {}).length
|
|
443
|
+
};
|
|
444
|
+
}
|
|
@@ -42,7 +42,7 @@ export default async function typecheckCommand(options = {}, projectRoot) {
|
|
|
42
42
|
const types = await runTypeRefresh(projectRoot, options);
|
|
43
43
|
if (types) reportTypesRefresh(types, projectRoot);
|
|
44
44
|
} catch (error) {
|
|
45
|
-
Print.warning(
|
|
45
|
+
Print.warning(`Could not refresh the generated types: ${error.message}`);
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -69,7 +69,7 @@ export default async function typecheckCommand(options = {}, projectRoot) {
|
|
|
69
69
|
});
|
|
70
70
|
|
|
71
71
|
if (result.error) {
|
|
72
|
-
Print.error(
|
|
72
|
+
Print.error(`Could not run tsc: ${result.error.message}`);
|
|
73
73
|
process.exitCode = 1;
|
|
74
74
|
return false;
|
|
75
75
|
}
|
package/commands/types/types.js
CHANGED
|
@@ -233,9 +233,27 @@ const mergeCatalog = (registry, catalog, prefix = '') => {
|
|
|
233
233
|
};
|
|
234
234
|
|
|
235
235
|
// Scan src/**/*.js for slice.events.register(<catalog>) calls and build the event registry.
|
|
236
|
+
// Source files that participate in the static event graph. Specs, tests and
|
|
237
|
+
// generated artifacts must never be documented as application emitters.
|
|
238
|
+
const isEventSourceFile = (filePath, srcDir) => {
|
|
239
|
+
const name = path.basename(filePath).toLowerCase();
|
|
240
|
+
if (name.endsWith('.spec.js') || name.endsWith('.test.js') || name.endsWith('.generated.js')) return false;
|
|
241
|
+
const relative = path.relative(srcDir, filePath).replace(/\\/g, '/');
|
|
242
|
+
if (relative.startsWith('public/') || relative.startsWith('bundles/')) return false;
|
|
243
|
+
return true;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const collectEventSourceFiles = async (srcDir) =>
|
|
247
|
+
(await collectJavaScriptFiles(srcDir)).filter((file) => isEventSourceFile(file, srcDir));
|
|
248
|
+
|
|
249
|
+
const categoryForEventSource = (relFile) => {
|
|
250
|
+
const match = relFile.match(/^src\/Components\/([^/]+)\//);
|
|
251
|
+
return match ? match[1] : null;
|
|
252
|
+
};
|
|
253
|
+
|
|
236
254
|
const collectEventRegistry = async ({ projectRoot }) => {
|
|
237
255
|
const srcDir = joinRoot(projectRoot, 'src');
|
|
238
|
-
const files = await
|
|
256
|
+
const files = await collectEventSourceFiles(srcDir);
|
|
239
257
|
const registry = {};
|
|
240
258
|
|
|
241
259
|
for (const file of files) {
|
|
@@ -342,7 +360,7 @@ const enclosingClassName = (pathRef) => {
|
|
|
342
360
|
// Returns { events: { name: { emitters:[], listeners:[] } }, dynamic: { emitters:[], listeners:[] } }.
|
|
343
361
|
const collectEventGraph = async ({ projectRoot }) => {
|
|
344
362
|
const srcDir = joinRoot(projectRoot, 'src');
|
|
345
|
-
const files = await
|
|
363
|
+
const files = await collectEventSourceFiles(srcDir);
|
|
346
364
|
const events = {};
|
|
347
365
|
const dynamic = { emitters: [], listeners: [] };
|
|
348
366
|
|
|
@@ -378,7 +396,8 @@ const collectEventGraph = async ({ projectRoot }) => {
|
|
|
378
396
|
const site = {
|
|
379
397
|
file: relFile,
|
|
380
398
|
line: pathRef.node.loc?.start?.line || 0,
|
|
381
|
-
component: enclosingClassName(pathRef)
|
|
399
|
+
component: enclosingClassName(pathRef),
|
|
400
|
+
category: categoryForEventSource(relFile)
|
|
382
401
|
};
|
|
383
402
|
|
|
384
403
|
const nameArg = pathRef.node.arguments[0];
|
|
@@ -113,19 +113,19 @@ class VersionChecker {
|
|
|
113
113
|
if (!silent && (cliStatus === 'outdated' || frameworkStatus === 'outdated')) {
|
|
114
114
|
const pm = resolvePackageManager(getProjectRoot(import.meta.url)).name;
|
|
115
115
|
console.log(''); // Line break
|
|
116
|
-
Print.
|
|
116
|
+
Print.subtitle('Available updates');
|
|
117
117
|
|
|
118
118
|
if (cliStatus === 'outdated') {
|
|
119
|
-
console.log(`
|
|
119
|
+
console.log(` CLI: ${current.cli} ā ${latest.cli}`);
|
|
120
120
|
console.log(` ${pm} update slicejs-cli`);
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
if (frameworkStatus === 'outdated') {
|
|
124
|
-
console.log(`
|
|
124
|
+
console.log(` Framework: ${current.framework} ā ${latest.framework}`);
|
|
125
125
|
console.log(` ${pm} update slicejs-web-framework`);
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
console.log('
|
|
128
|
+
console.log(' Changelog: https://github.com/VKneider/slice.js/releases');
|
|
129
129
|
console.log(''); // Line break
|
|
130
130
|
}
|
|
131
131
|
|
|
@@ -144,22 +144,22 @@ class VersionChecker {
|
|
|
144
144
|
const current = await this.getCurrentVersions();
|
|
145
145
|
const latest = await this.getLatestVersions();
|
|
146
146
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
147
|
+
Print.title('Version information');
|
|
148
|
+
Print.separator();
|
|
149
|
+
|
|
150
150
|
if (current?.cli) {
|
|
151
151
|
const cliStatus = this.compareVersions(current.cli, latest?.cli);
|
|
152
|
-
const
|
|
153
|
-
console.log(
|
|
152
|
+
const status = cliStatus === 'current' ? 'up to date' : cliStatus === 'outdated' ? 'update available' : 'new';
|
|
153
|
+
console.log(`CLI v${current.cli}${latest?.cli && latest.cli !== current.cli ? ` ā v${latest.cli}` : ''} (${status})`);
|
|
154
154
|
}
|
|
155
|
-
|
|
155
|
+
|
|
156
156
|
if (current?.framework) {
|
|
157
157
|
const frameworkStatus = this.compareVersions(current.framework, latest?.framework);
|
|
158
|
-
const
|
|
159
|
-
console.log(
|
|
158
|
+
const status = frameworkStatus === 'current' ? 'up to date' : frameworkStatus === 'outdated' ? 'update available' : 'new';
|
|
159
|
+
console.log(`Framework v${current.framework}${latest?.framework && latest.framework !== current.framework ? ` ā v${latest.framework}` : ''} (${status})`);
|
|
160
160
|
}
|
|
161
|
-
|
|
162
|
-
|
|
161
|
+
|
|
162
|
+
Print.separator();
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
165
|
|