@proveanything/smartlinks 2.1.18 → 2.1.20
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/dist/adminViews.d.ts +19 -0
- package/dist/adminViews.js +16 -0
- package/dist/api/ai.d.ts +15 -5
- package/dist/api/ai.js +15 -9
- package/dist/api/aiGuide.d.ts +110 -0
- package/dist/api/aiGuide.js +162 -0
- package/dist/api/appObjects.d.ts +21 -0
- package/dist/api/appObjects.js +33 -1
- package/dist/api/authKit.d.ts +37 -1
- package/dist/api/authKit.js +51 -0
- package/dist/docs/API_SUMMARY.md +510 -40
- package/dist/docs/admin-lists-and-pickers.md +140 -0
- package/dist/docs/admin-views.md +22 -0
- package/dist/docs/app-data-storage.md +277 -276
- package/dist/docs/auth-kit.md +872 -741
- package/dist/docs/comms.md +686 -644
- package/dist/docs/customer-guide.md +70 -0
- package/dist/docs/headless-providers.md +112 -112
- package/dist/docs/server-functions.md +69 -1
- package/dist/docs/ui-utils.md +123 -121
- package/dist/docs/user-groups.md +175 -168
- package/dist/headless.js +38 -2
- package/dist/http.js +4 -0
- package/dist/index.d.ts +2 -2
- package/dist/live.d.ts +88 -0
- package/dist/live.js +357 -0
- package/dist/openapi.yaml +888 -34
- package/dist/testing/index.d.ts +22 -0
- package/dist/testing/index.js +31 -0
- package/dist/types/appManifest.d.ts +214 -1
- package/dist/types/appOptions.d.ts +32 -0
- package/dist/types/appOptions.js +3 -0
- package/dist/types/authKit.d.ts +193 -4
- package/dist/types/comms.d.ts +41 -2
- package/dist/types/headless.d.ts +49 -1
- package/dist/types/iframeResponder.d.ts +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/docs/API_SUMMARY.md +510 -40
- package/docs/admin-lists-and-pickers.md +140 -0
- package/docs/admin-views.md +22 -0
- package/docs/app-data-storage.md +277 -276
- package/docs/auth-kit.md +872 -741
- package/docs/comms.md +686 -644
- package/docs/customer-guide.md +70 -0
- package/docs/headless-providers.md +112 -112
- package/docs/server-functions.md +69 -1
- package/docs/ui-utils.md +123 -121
- package/docs/user-groups.md +175 -168
- package/openapi.yaml +888 -34
- package/package.json +1 -1
- package/scripts/doctor.mjs +454 -428
- package/scripts/lib/keyed-app-data.mjs +21 -0
package/scripts/doctor.mjs
CHANGED
|
@@ -1,428 +1,454 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// =============================================================================
|
|
3
|
-
// smartlinks doctor — verify an app's ESM bundles against the shared-dependency
|
|
4
|
-
// contract, so a modern (esm/dual) app can't ship a bare import the host won't
|
|
5
|
-
// resolve.
|
|
6
|
-
//
|
|
7
|
-
// A correctly-built externalized ESM bundle inlines everything EXCEPT the host's
|
|
8
|
-
// shared singletons. So every *bare* import left in the output must be a contract
|
|
9
|
-
// entry — anything else will either fail to resolve through the import map at
|
|
10
|
-
// runtime, or silently pull in a second copy (the duplicate-React class of bug).
|
|
11
|
-
//
|
|
12
|
-
// Usage:
|
|
13
|
-
// smartlinks-doctor [appDir] # defaults to cwd
|
|
14
|
-
// npx @proveanything/smartlinks doctor (once a unified `smartlinks` bin exists)
|
|
15
|
-
//
|
|
16
|
-
// Exit code: 0 = clean, 1 = violations (CI-friendly).
|
|
17
|
-
// =============================================================================
|
|
18
|
-
|
|
19
|
-
import { readFileSync, existsSync, readdirSync } from 'node:fs';
|
|
20
|
-
import { resolve, dirname, join } from 'node:path';
|
|
21
|
-
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
22
|
-
import { bareImportsOf } from './lib/bare-imports.mjs';
|
|
23
|
-
|
|
24
|
-
const here = dirname(fileURLToPath(import.meta.url));
|
|
25
|
-
|
|
26
|
-
// Read the contract from THIS SDK build — one source of truth with the runtime.
|
|
27
|
-
const contractUrl = pathToFileURL(resolve(here, '../dist/shared-dependencies.js')).href;
|
|
28
|
-
const { SHARED_DEPENDENCY_SPECIFIERS, SHARED_DEPENDENCY_CONTRACT_VERSION } = await import(contractUrl);
|
|
29
|
-
const CONTRACT = new Set(SHARED_DEPENDENCY_SPECIFIERS);
|
|
30
|
-
|
|
31
|
-
const RED = '\x1b[31m';
|
|
32
|
-
const GREEN = '\x1b[32m';
|
|
33
|
-
const YELLOW = '\x1b[33m';
|
|
34
|
-
const DIM = '\x1b[2m';
|
|
35
|
-
const BOLD = '\x1b[1m';
|
|
36
|
-
const RESET = '\x1b[0m';
|
|
37
|
-
|
|
38
|
-
function die(msg) {
|
|
39
|
-
console.error(`${RED}smartlinks doctor: ${msg}${RESET}`);
|
|
40
|
-
process.exit(1);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const appDir = resolve(process.argv[2] || process.cwd());
|
|
44
|
-
if (!existsSync(appDir)) die(`app directory not found: ${appDir}`);
|
|
45
|
-
|
|
46
|
-
// Locate the manifest. The BUILT one (dist/) first: it's what ships, and the build's hash step renames
|
|
47
|
-
// bundles (mobile-admin.esm.js → mobile-admin-<hash>.esm.js) and rewrites only dist/app.manifest.json —
|
|
48
|
-
// checking the source manifest after a build looked for files that no longer exist under that name.
|
|
49
|
-
// The source manifest is the fallback for a doctor run before any build.
|
|
50
|
-
const manifestPath = ['dist/app.manifest.json', 'public/app.manifest.json', 'app.manifest.json']
|
|
51
|
-
.map((p) => join(appDir, p))
|
|
52
|
-
.find(existsSync);
|
|
53
|
-
if (!manifestPath) die(`no app.manifest.json found under ${appDir} (looked in dist/, public/, .)`);
|
|
54
|
-
|
|
55
|
-
let manifest;
|
|
56
|
-
try {
|
|
57
|
-
manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
58
|
-
} catch (e) {
|
|
59
|
-
die(`could not parse ${manifestPath}: ${e.message}`);
|
|
60
|
-
}
|
|
61
|
-
const manifestDir = dirname(manifestPath);
|
|
62
|
-
const meta = manifest.meta || {};
|
|
63
|
-
|
|
64
|
-
console.log(`${BOLD}SmartLinks doctor${RESET} ${DIM}— ${appDir}${RESET}`);
|
|
65
|
-
console.log(`${DIM}manifest:${RESET} ${manifestPath.replace(appDir + '\\', '').replace(appDir + '/', '')}`);
|
|
66
|
-
console.log(`${DIM}contract:${RESET} ${SHARED_DEPENDENCY_CONTRACT_VERSION} (${CONTRACT.size} shared deps)`);
|
|
67
|
-
console.log(`${DIM}moduleFormat:${RESET} ${meta.moduleFormat || '(absent → umd)'} · ${DIM}sharedDependencies:${RESET} ${meta.sharedDependencies || '(none)'}\n`);
|
|
68
|
-
|
|
69
|
-
const problems = [];
|
|
70
|
-
const warnings = [];
|
|
71
|
-
|
|
72
|
-
// Manifest-level sanity.
|
|
73
|
-
const format = meta.moduleFormat || 'umd';
|
|
74
|
-
const checkEsm = format === 'esm' || format === 'dual';
|
|
75
|
-
if (checkEsm && !meta.sharedDependencies) {
|
|
76
|
-
warnings.push(`meta.moduleFormat is "${format}" but meta.sharedDependencies is not declared — set it to "${SHARED_DEPENDENCY_CONTRACT_VERSION}".`);
|
|
77
|
-
}
|
|
78
|
-
if (meta.sharedDependencies && meta.sharedDependencies !== SHARED_DEPENDENCY_CONTRACT_VERSION) {
|
|
79
|
-
warnings.push(`built against contract ${meta.sharedDependencies}, but this SDK ships ${SHARED_DEPENDENCY_CONTRACT_VERSION} — host may not serve a matching import map.`);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// bareImportsOf (scripts/lib/bare-imports.mjs) LEXES the bundle for real import
|
|
83
|
-
// statements — so bundled data containing the word `from` (e.g. tailwind-merge's class
|
|
84
|
-
// map) is not mistaken for an import.
|
|
85
|
-
|
|
86
|
-
// Resolve a manifest-relative bundle path against the likely roots (dist paths
|
|
87
|
-
// are sometimes relative to the app root, sometimes to the manifest dir).
|
|
88
|
-
function resolveBundle(rel) {
|
|
89
|
-
const bases = [manifestDir, appDir, join(appDir, 'public'), join(appDir, 'dist')];
|
|
90
|
-
for (const b of bases) {
|
|
91
|
-
const p = resolve(b, rel);
|
|
92
|
-
if (existsSync(p)) return p;
|
|
93
|
-
}
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Check each ESM surface declared in the manifest.
|
|
98
|
-
const surfaces = [
|
|
99
|
-
['widgets', manifest.widgets],
|
|
100
|
-
['containers', manifest.containers],
|
|
101
|
-
['mobileAdmin', manifest.mobileAdmin],
|
|
102
|
-
];
|
|
103
|
-
|
|
104
|
-
let checkedAny = false;
|
|
105
|
-
for (const [name, block] of surfaces) {
|
|
106
|
-
const esm = block?.files?.js?.esm;
|
|
107
|
-
if (!esm) continue;
|
|
108
|
-
|
|
109
|
-
// UMD-format app that still declares an ESM bundle: the host won't load it, so
|
|
110
|
-
// it's dead weight and a common scaffold-confusion source — warn, don't fail.
|
|
111
|
-
if (!checkEsm) {
|
|
112
|
-
warnings.push(`${name}: ESM bundle "${esm}" is declared but meta.moduleFormat is "${format}", so the host never loads it. Set moduleFormat to "dual" to use it, or drop the esm entry.`);
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
checkedAny = true;
|
|
117
|
-
const bundlePath = resolveBundle(esm);
|
|
118
|
-
if (!bundlePath) {
|
|
119
|
-
console.log(`${RED}✗${RESET} ${name} ${DIM}(${esm})${RESET} — declared ESM bundle not found on disk`);
|
|
120
|
-
problems.push({ surface: name, spec: null, msg: `declared ESM bundle not found: ${esm}` });
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
const code = readFileSync(bundlePath, 'utf8');
|
|
124
|
-
const imports = bareImportsOf(code);
|
|
125
|
-
const offenders = imports.filter((s) => !CONTRACT.has(s));
|
|
126
|
-
const ok = imports.length - offenders.length;
|
|
127
|
-
|
|
128
|
-
if (offenders.length === 0) {
|
|
129
|
-
console.log(`${GREEN}✓${RESET} ${name} ${DIM}(${esm})${RESET} — ${ok} bare import${ok === 1 ? '' : 's'}, all in contract`);
|
|
130
|
-
} else {
|
|
131
|
-
console.log(`${RED}✗${RESET} ${name} ${DIM}(${esm})${RESET} — ${offenders.length} outside the contract:`);
|
|
132
|
-
for (const spec of offenders) {
|
|
133
|
-
const hint = spec.startsWith('node:')
|
|
134
|
-
? 'node built-in — must not appear in a browser bundle'
|
|
135
|
-
: 'not host-provided — bundle it (don\'t externalize) or it will fail to resolve / double-load';
|
|
136
|
-
console.log(` ${RED}${spec}${RESET} ${DIM}— ${hint}${RESET}`);
|
|
137
|
-
problems.push({ surface: name, spec, msg: hint });
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (checkEsm && !checkedAny) {
|
|
143
|
-
warnings.push(`moduleFormat is "${format}" but no surface declares files.js.esm — nothing to check.`);
|
|
144
|
-
}
|
|
145
|
-
if (!checkEsm && problems.length === 0) {
|
|
146
|
-
console.log(`${DIM}moduleFormat "${format}" — ESM path not in use; UMD bundles resolve shared deps from window globals.${RESET}`);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// ---- CSS baseline (sl-baseline) --------------------------------------------
|
|
150
|
-
// If the app declares meta.cssBaseline, warn on any `sl-*` class it uses that isn't in
|
|
151
|
-
// the declared baseline version — those render nothing once the host's baseline is the
|
|
152
|
-
// only source. Heuristic (scans built bundle text for the distinctive `sl-` namespace)
|
|
153
|
-
// and WARN-only: it never fails the build.
|
|
154
|
-
if (meta.cssBaseline) {
|
|
155
|
-
let baseline = null;
|
|
156
|
-
try {
|
|
157
|
-
baseline = JSON.parse(readFileSync(resolve(here, '../dist/baseline.classes.json'), 'utf8'));
|
|
158
|
-
} catch {
|
|
159
|
-
warnings.push(`meta.cssBaseline is "${meta.cssBaseline}" but this SDK build has no baseline class list to check against.`);
|
|
160
|
-
}
|
|
161
|
-
if (baseline) {
|
|
162
|
-
if (baseline.version !== meta.cssBaseline) {
|
|
163
|
-
warnings.push(`meta.cssBaseline is "${meta.cssBaseline}" but this SDK ships baseline "${baseline.version}" — the host may serve a different set.`);
|
|
164
|
-
}
|
|
165
|
-
const baseSet = new Set(baseline.classes || []);
|
|
166
|
-
// SDK-defined names that aren't baseline helpers but are legitimately in bundles: the Sidekick
|
|
167
|
-
// screen's scope class (mobile-admin-container.md → Styling).
|
|
168
|
-
for (const c of ['sl-mobile-admin']) baseSet.add(c);
|
|
169
|
-
// Class usage lives in compiled markup strings — present in every bundle regardless
|
|
170
|
-
// of module format, so scan both umd + esm across all surfaces.
|
|
171
|
-
// `sl-…` NOT preceded by a word char or hyphen: a class name, never a CSS custom property
|
|
172
|
-
// (`var(--sl-color-accent)`, `--sl-widget-accent`) — those are theme tokens, not classes.
|
|
173
|
-
// Classes the app defines itself (`.sl-foo {…}` in its own CSS) aren't missing either.
|
|
174
|
-
const used = new Set();
|
|
175
|
-
const defined = new Set();
|
|
176
|
-
for (const [, block] of surfaces) {
|
|
177
|
-
for (const p of [block?.files?.js?.umd, block?.files?.js?.esm, block?.files?.css]) {
|
|
178
|
-
if (!p || typeof p !== 'string') continue;
|
|
179
|
-
const bp = resolveBundle(p);
|
|
180
|
-
if (!bp) continue;
|
|
181
|
-
const text = readFileSync(bp, 'utf8');
|
|
182
|
-
for (const m of text.matchAll(/(?<![\w-])sl-[a-z0-9-]+/g)) used.add(m[0]);
|
|
183
|
-
for (const m of text.matchAll(/\.(sl-[a-z0-9-]+)/g)) defined.add(m[1]);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
const ownSl = [...defined].filter((c) => !baseSet.has(c)).sort();
|
|
187
|
-
if (ownSl.length) {
|
|
188
|
-
warnings.push(`the app's own classes use the platform's sl- prefix: ${ownSl.join(', ')} — rename them (e.g. drop the "sl-") so they can't collide with a future baseline class.`);
|
|
189
|
-
}
|
|
190
|
-
const unknown = [...used].filter((c) => !baseSet.has(c) && !defined.has(c)).sort();
|
|
191
|
-
if (used.size === 0) {
|
|
192
|
-
console.log(`${DIM}cssBaseline (${baseline.version}) — declared; no sl-* classes found in the bundles.${RESET}`);
|
|
193
|
-
} else if (unknown.length === 0) {
|
|
194
|
-
console.log(`${GREEN}✓${RESET} cssBaseline ${DIM}(${baseline.version})${RESET} — all ${used.size} sl-* classes used are in the baseline`);
|
|
195
|
-
} else {
|
|
196
|
-
console.log(`${YELLOW}⚠${RESET} cssBaseline ${DIM}(${baseline.version})${RESET} — ${unknown.length} sl-* class${unknown.length === 1 ? '' : 'es'} used but not in the baseline (they will not render):`);
|
|
197
|
-
for (const c of unknown) console.log(` ${YELLOW}${c}${RESET}`);
|
|
198
|
-
warnings.push(`sl-* classes used but not in cssBaseline "${baseline.version}": ${unknown.join(', ')} — define them in your own CSS, fix the typo, or drop them.`);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// ---- Host theming (theme tokens) -------------------------------------------
|
|
204
|
-
// If the app declares meta.respectsHostTheme, warn on hardcoded Tailwind PALETTE utilities in
|
|
205
|
-
// component SOURCE (e.g. bg-blue-600, text-zinc-900) — those pin a colour instead of following the
|
|
206
|
-
// host brand via the semantic tokens (bg-primary, text-foreground, border-border). Scans source,
|
|
207
|
-
// not compiled bundles (bundles are full of legitimate hex). Heuristic + WARN-only. See
|
|
208
|
-
// docs/theme-tokens.md.
|
|
209
|
-
if (meta.respectsHostTheme) {
|
|
210
|
-
const THEME_TOKENS_VERSION = 'v1';
|
|
211
|
-
if (!meta.themeTokens) {
|
|
212
|
-
warnings.push(`meta.respectsHostTheme is true but meta.themeTokens is not declared — set it to "${THEME_TOKENS_VERSION}".`);
|
|
213
|
-
} else if (meta.themeTokens !== THEME_TOKENS_VERSION) {
|
|
214
|
-
warnings.push(`meta.themeTokens is "${meta.themeTokens}" but this SDK ships theme tokens "${THEME_TOKENS_VERSION}".`);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const PALETTE = 'slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose';
|
|
218
|
-
const UTIL = 'bg|text|border|ring|divide|from|via|to|fill|stroke|outline|decoration|shadow|accent|caret';
|
|
219
|
-
const paletteRe = new RegExp(`\\b(?:${UTIL})-(?:${PALETTE})-(?:50|100|200|300|400|500|600|700|800|900|950)\\b`, 'g');
|
|
220
|
-
|
|
221
|
-
const SRC_EXT = /\.(tsx|ts|jsx|js|vue|html|svelte)$/;
|
|
222
|
-
const SKIP_DIR = new Set(['node_modules', 'dist', '.nuxt', '.output', '.git', 'public']);
|
|
223
|
-
function walk(dir, out = []) {
|
|
224
|
-
let entries = [];
|
|
225
|
-
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return out; }
|
|
226
|
-
for (const e of entries) {
|
|
227
|
-
if (e.isDirectory()) { if (!SKIP_DIR.has(e.name)) walk(join(dir, e.name), out); }
|
|
228
|
-
else if (SRC_EXT.test(e.name)) out.push(join(dir, e.name));
|
|
229
|
-
}
|
|
230
|
-
return out;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const srcRoot = existsSync(join(appDir, 'src')) ? join(appDir, 'src') : appDir;
|
|
234
|
-
const offenders = [];
|
|
235
|
-
let totalHits = 0;
|
|
236
|
-
for (const f of walk(srcRoot)) {
|
|
237
|
-
const hits = [...new Set((readFileSync(f, 'utf8').match(paletteRe) || []))];
|
|
238
|
-
if (hits.length) { offenders.push({ file: f.replace(appDir + '/', '').replace(appDir + '\\', ''), hits }); totalHits += hits.length; }
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (offenders.length === 0) {
|
|
242
|
-
console.log(`${GREEN}✓${RESET} theme ${DIM}(respectsHostTheme)${RESET} — no hardcoded palette utilities in source`);
|
|
243
|
-
} else {
|
|
244
|
-
console.log(`${YELLOW}⚠${RESET} theme ${DIM}(respectsHostTheme)${RESET} — hardcoded palette utilities in ${offenders.length} file${offenders.length === 1 ? '' : 's'} (use bg-primary / text-foreground / border-border instead):`);
|
|
245
|
-
for (const o of offenders.slice(0, 15)) console.log(` ${DIM}${o.file}${RESET} ${YELLOW}${o.hits.slice(0, 6).join(' ')}${o.hits.length > 6 ? ' …' : ''}${RESET}`);
|
|
246
|
-
if (offenders.length > 15) console.log(` ${DIM}…and ${offenders.length - 15} more file(s)${RESET}`);
|
|
247
|
-
warnings.push(`respectsHostTheme is true but ${totalHits} hardcoded palette utilit${totalHits === 1 ? 'y' : 'ies'} found in source — replace with semantic tokens, or drop respectsHostTheme if the app intentionally brings its own look.`);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// ---- containers: a router of their own must be isolated from the host's ------------------------
|
|
252
|
-
// Since R5, react-router-dom is SHARED with the host, so a container that wraps itself in a
|
|
253
|
-
// MemoryRouter/HashRouter/BrowserRouter inside the host's router throws "You cannot render a <Router>
|
|
254
|
-
// inside another <Router>" — unless it resets the parent's router context first (docs/containers.md →
|
|
255
|
-
// "Pages that read their context from the URL"). Heuristic on the built bundle names; WARN-only.
|
|
256
|
-
{
|
|
257
|
-
const block = manifest.containers;
|
|
258
|
-
for (const p of [block?.files?.js?.esm, block?.files?.js?.umd]) {
|
|
259
|
-
if (!p) continue;
|
|
260
|
-
const bp = resolveBundle(p);
|
|
261
|
-
if (!bp) continue;
|
|
262
|
-
const text = readFileSync(bp, 'utf8');
|
|
263
|
-
const routers = ['MemoryRouter', 'HashRouter', 'BrowserRouter'].filter((r) => new RegExp(`\\b${r}\\b`).test(text));
|
|
264
|
-
if (!routers.length) { console.log(`${GREEN}✓${RESET} containers ${DIM}— no router of their own (the host's is used)${RESET}`); break; }
|
|
265
|
-
if (/\bUNSAFE_LocationContext\b/.test(text)) { console.log(`${GREEN}✓${RESET} containers ${DIM}— own ${routers.join('/')} isolated from the host's router${RESET}`); break; }
|
|
266
|
-
console.log(`${YELLOW}⚠${RESET} containers — ${routers.join('/')} without isolating it from the host's router`);
|
|
267
|
-
warnings.push(`containers bundle renders its own ${routers.join('/')} but never resets the host's router context — inside an R5 host that throws "You cannot render a <Router> inside another <Router>". Wrap it in UNSAFE_LocationContext.Provider + UNSAFE_NavigationContext.Provider value={null} (docs/containers.md), or drop the router and use the host's.`);
|
|
268
|
-
break;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// ---- showing another app's widget: use the host's EmbeddedWidget, for widgets it allows -----------
|
|
273
|
-
// Hosts (Portal, Hub) pass every app an `EmbeddedWidget` prop (docs/widgets.md → "Using another app's
|
|
274
|
-
// widget"). A hand-rolled loader (getWidgets → evaluate the bundle) drifts and breaks; and the host only
|
|
275
|
-
// renders first-party widgets and the same publisher's apps, so anything else silently shows nothing.
|
|
276
|
-
// Static scan of the source; WARN-only.
|
|
277
|
-
{
|
|
278
|
-
const ownId = String(manifest.meta?.appId || '');
|
|
279
|
-
const handleOf = (id) => { const i = id.indexOf('--'); return i > 0 ? id.slice(0, i) : null; };
|
|
280
|
-
const EXT = /\.(tsx|ts|jsx|js)$/;
|
|
281
|
-
const SKIP = new Set(['node_modules', 'dist', '.git', 'public']);
|
|
282
|
-
const files = [];
|
|
283
|
-
(function walkSrc(dir) {
|
|
284
|
-
let entries = [];
|
|
285
|
-
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return; }
|
|
286
|
-
for (const e of entries) {
|
|
287
|
-
if (e.isDirectory()) { if (!SKIP.has(e.name)) walkSrc(join(dir, e.name)); }
|
|
288
|
-
else if (EXT.test(e.name)) files.push(join(dir, e.name));
|
|
289
|
-
}
|
|
290
|
-
})(existsSync(join(appDir, 'src')) ? join(appDir, 'src') : appDir);
|
|
291
|
-
const rel = (f) => (f.startsWith(appDir) ? f.slice(appDir.length + 1) : f);
|
|
292
|
-
const handRolled = [];
|
|
293
|
-
const blocked = new Set();
|
|
294
|
-
let embeds = 0;
|
|
295
|
-
for (const f of files) {
|
|
296
|
-
const text = readFileSync(f, 'utf8');
|
|
297
|
-
if (/\bgetWidgets\s*\(/.test(text) && /(new\s+Blob\s*\(|new\s+Function\s*\(|createRequireShim|import\s*\(\s*\/\*\s*@vite-ignore)/.test(text)) handRolled.push(rel(f));
|
|
298
|
-
for (const m of text.matchAll(/<EmbeddedWidget\b[^>]*?\bappId\s*=\s*\{?\s*["'`]([A-Za-z0-9._-]+)["'`]/g)) {
|
|
299
|
-
embeds++;
|
|
300
|
-
const id = m[1];
|
|
301
|
-
const owner = handleOf(id);
|
|
302
|
-
if (id === ownId || (owner !== null && owner !== handleOf(ownId))) blocked.add(id);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
if (handRolled.length) {
|
|
306
|
-
console.log(`${YELLOW}⚠${RESET} embedded widgets — a hand-rolled widget loader in ${handRolled.slice(0, 3).join(', ')}`);
|
|
307
|
-
warnings.push(`${handRolled.join(', ')} load${handRolled.length === 1 ? 's' : ''} another app's widget bundle by hand (getWidgets + evaluating it). Use the host's EmbeddedWidget prop instead: <EmbeddedWidget appId="…" /> (docs/widgets.md → "Using another app's widget").`);
|
|
308
|
-
}
|
|
309
|
-
if (blocked.size) {
|
|
310
|
-
console.log(`${YELLOW}⚠${RESET} embedded widgets — the host won't show: ${[...blocked].join(', ')}`);
|
|
311
|
-
warnings.push(`EmbeddedWidget for ${[...blocked].join(', ')} will render nothing: hosts only show first-party widgets and the same publisher's apps${ownId ? ` (this app is "${ownId}")` : ''}, and never the app itself.`);
|
|
312
|
-
}
|
|
313
|
-
if (embeds && !handRolled.length && !blocked.size) console.log(`${GREEN}✓${RESET} embedded widgets ${DIM}— ${embeds} via the host's EmbeddedWidget${RESET}`);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
// ----
|
|
317
|
-
//
|
|
318
|
-
//
|
|
319
|
-
{
|
|
320
|
-
const
|
|
321
|
-
const
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
//
|
|
343
|
-
//
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
const
|
|
347
|
-
const
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// =============================================================================
|
|
3
|
+
// smartlinks doctor — verify an app's ESM bundles against the shared-dependency
|
|
4
|
+
// contract, so a modern (esm/dual) app can't ship a bare import the host won't
|
|
5
|
+
// resolve.
|
|
6
|
+
//
|
|
7
|
+
// A correctly-built externalized ESM bundle inlines everything EXCEPT the host's
|
|
8
|
+
// shared singletons. So every *bare* import left in the output must be a contract
|
|
9
|
+
// entry — anything else will either fail to resolve through the import map at
|
|
10
|
+
// runtime, or silently pull in a second copy (the duplicate-React class of bug).
|
|
11
|
+
//
|
|
12
|
+
// Usage:
|
|
13
|
+
// smartlinks-doctor [appDir] # defaults to cwd
|
|
14
|
+
// npx @proveanything/smartlinks doctor (once a unified `smartlinks` bin exists)
|
|
15
|
+
//
|
|
16
|
+
// Exit code: 0 = clean, 1 = violations (CI-friendly).
|
|
17
|
+
// =============================================================================
|
|
18
|
+
|
|
19
|
+
import { readFileSync, existsSync, readdirSync } from 'node:fs';
|
|
20
|
+
import { resolve, dirname, join } from 'node:path';
|
|
21
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
22
|
+
import { bareImportsOf } from './lib/bare-imports.mjs';
|
|
23
|
+
|
|
24
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
25
|
+
|
|
26
|
+
// Read the contract from THIS SDK build — one source of truth with the runtime.
|
|
27
|
+
const contractUrl = pathToFileURL(resolve(here, '../dist/shared-dependencies.js')).href;
|
|
28
|
+
const { SHARED_DEPENDENCY_SPECIFIERS, SHARED_DEPENDENCY_CONTRACT_VERSION } = await import(contractUrl);
|
|
29
|
+
const CONTRACT = new Set(SHARED_DEPENDENCY_SPECIFIERS);
|
|
30
|
+
|
|
31
|
+
const RED = '\x1b[31m';
|
|
32
|
+
const GREEN = '\x1b[32m';
|
|
33
|
+
const YELLOW = '\x1b[33m';
|
|
34
|
+
const DIM = '\x1b[2m';
|
|
35
|
+
const BOLD = '\x1b[1m';
|
|
36
|
+
const RESET = '\x1b[0m';
|
|
37
|
+
|
|
38
|
+
function die(msg) {
|
|
39
|
+
console.error(`${RED}smartlinks doctor: ${msg}${RESET}`);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const appDir = resolve(process.argv[2] || process.cwd());
|
|
44
|
+
if (!existsSync(appDir)) die(`app directory not found: ${appDir}`);
|
|
45
|
+
|
|
46
|
+
// Locate the manifest. The BUILT one (dist/) first: it's what ships, and the build's hash step renames
|
|
47
|
+
// bundles (mobile-admin.esm.js → mobile-admin-<hash>.esm.js) and rewrites only dist/app.manifest.json —
|
|
48
|
+
// checking the source manifest after a build looked for files that no longer exist under that name.
|
|
49
|
+
// The source manifest is the fallback for a doctor run before any build.
|
|
50
|
+
const manifestPath = ['dist/app.manifest.json', 'public/app.manifest.json', 'app.manifest.json']
|
|
51
|
+
.map((p) => join(appDir, p))
|
|
52
|
+
.find(existsSync);
|
|
53
|
+
if (!manifestPath) die(`no app.manifest.json found under ${appDir} (looked in dist/, public/, .)`);
|
|
54
|
+
|
|
55
|
+
let manifest;
|
|
56
|
+
try {
|
|
57
|
+
manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
58
|
+
} catch (e) {
|
|
59
|
+
die(`could not parse ${manifestPath}: ${e.message}`);
|
|
60
|
+
}
|
|
61
|
+
const manifestDir = dirname(manifestPath);
|
|
62
|
+
const meta = manifest.meta || {};
|
|
63
|
+
|
|
64
|
+
console.log(`${BOLD}SmartLinks doctor${RESET} ${DIM}— ${appDir}${RESET}`);
|
|
65
|
+
console.log(`${DIM}manifest:${RESET} ${manifestPath.replace(appDir + '\\', '').replace(appDir + '/', '')}`);
|
|
66
|
+
console.log(`${DIM}contract:${RESET} ${SHARED_DEPENDENCY_CONTRACT_VERSION} (${CONTRACT.size} shared deps)`);
|
|
67
|
+
console.log(`${DIM}moduleFormat:${RESET} ${meta.moduleFormat || '(absent → umd)'} · ${DIM}sharedDependencies:${RESET} ${meta.sharedDependencies || '(none)'}\n`);
|
|
68
|
+
|
|
69
|
+
const problems = [];
|
|
70
|
+
const warnings = [];
|
|
71
|
+
|
|
72
|
+
// Manifest-level sanity.
|
|
73
|
+
const format = meta.moduleFormat || 'umd';
|
|
74
|
+
const checkEsm = format === 'esm' || format === 'dual';
|
|
75
|
+
if (checkEsm && !meta.sharedDependencies) {
|
|
76
|
+
warnings.push(`meta.moduleFormat is "${format}" but meta.sharedDependencies is not declared — set it to "${SHARED_DEPENDENCY_CONTRACT_VERSION}".`);
|
|
77
|
+
}
|
|
78
|
+
if (meta.sharedDependencies && meta.sharedDependencies !== SHARED_DEPENDENCY_CONTRACT_VERSION) {
|
|
79
|
+
warnings.push(`built against contract ${meta.sharedDependencies}, but this SDK ships ${SHARED_DEPENDENCY_CONTRACT_VERSION} — host may not serve a matching import map.`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// bareImportsOf (scripts/lib/bare-imports.mjs) LEXES the bundle for real import
|
|
83
|
+
// statements — so bundled data containing the word `from` (e.g. tailwind-merge's class
|
|
84
|
+
// map) is not mistaken for an import.
|
|
85
|
+
|
|
86
|
+
// Resolve a manifest-relative bundle path against the likely roots (dist paths
|
|
87
|
+
// are sometimes relative to the app root, sometimes to the manifest dir).
|
|
88
|
+
function resolveBundle(rel) {
|
|
89
|
+
const bases = [manifestDir, appDir, join(appDir, 'public'), join(appDir, 'dist')];
|
|
90
|
+
for (const b of bases) {
|
|
91
|
+
const p = resolve(b, rel);
|
|
92
|
+
if (existsSync(p)) return p;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Check each ESM surface declared in the manifest.
|
|
98
|
+
const surfaces = [
|
|
99
|
+
['widgets', manifest.widgets],
|
|
100
|
+
['containers', manifest.containers],
|
|
101
|
+
['mobileAdmin', manifest.mobileAdmin],
|
|
102
|
+
];
|
|
103
|
+
|
|
104
|
+
let checkedAny = false;
|
|
105
|
+
for (const [name, block] of surfaces) {
|
|
106
|
+
const esm = block?.files?.js?.esm;
|
|
107
|
+
if (!esm) continue;
|
|
108
|
+
|
|
109
|
+
// UMD-format app that still declares an ESM bundle: the host won't load it, so
|
|
110
|
+
// it's dead weight and a common scaffold-confusion source — warn, don't fail.
|
|
111
|
+
if (!checkEsm) {
|
|
112
|
+
warnings.push(`${name}: ESM bundle "${esm}" is declared but meta.moduleFormat is "${format}", so the host never loads it. Set moduleFormat to "dual" to use it, or drop the esm entry.`);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
checkedAny = true;
|
|
117
|
+
const bundlePath = resolveBundle(esm);
|
|
118
|
+
if (!bundlePath) {
|
|
119
|
+
console.log(`${RED}✗${RESET} ${name} ${DIM}(${esm})${RESET} — declared ESM bundle not found on disk`);
|
|
120
|
+
problems.push({ surface: name, spec: null, msg: `declared ESM bundle not found: ${esm}` });
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const code = readFileSync(bundlePath, 'utf8');
|
|
124
|
+
const imports = bareImportsOf(code);
|
|
125
|
+
const offenders = imports.filter((s) => !CONTRACT.has(s));
|
|
126
|
+
const ok = imports.length - offenders.length;
|
|
127
|
+
|
|
128
|
+
if (offenders.length === 0) {
|
|
129
|
+
console.log(`${GREEN}✓${RESET} ${name} ${DIM}(${esm})${RESET} — ${ok} bare import${ok === 1 ? '' : 's'}, all in contract`);
|
|
130
|
+
} else {
|
|
131
|
+
console.log(`${RED}✗${RESET} ${name} ${DIM}(${esm})${RESET} — ${offenders.length} outside the contract:`);
|
|
132
|
+
for (const spec of offenders) {
|
|
133
|
+
const hint = spec.startsWith('node:')
|
|
134
|
+
? 'node built-in — must not appear in a browser bundle'
|
|
135
|
+
: 'not host-provided — bundle it (don\'t externalize) or it will fail to resolve / double-load';
|
|
136
|
+
console.log(` ${RED}${spec}${RESET} ${DIM}— ${hint}${RESET}`);
|
|
137
|
+
problems.push({ surface: name, spec, msg: hint });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (checkEsm && !checkedAny) {
|
|
143
|
+
warnings.push(`moduleFormat is "${format}" but no surface declares files.js.esm — nothing to check.`);
|
|
144
|
+
}
|
|
145
|
+
if (!checkEsm && problems.length === 0) {
|
|
146
|
+
console.log(`${DIM}moduleFormat "${format}" — ESM path not in use; UMD bundles resolve shared deps from window globals.${RESET}`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ---- CSS baseline (sl-baseline) --------------------------------------------
|
|
150
|
+
// If the app declares meta.cssBaseline, warn on any `sl-*` class it uses that isn't in
|
|
151
|
+
// the declared baseline version — those render nothing once the host's baseline is the
|
|
152
|
+
// only source. Heuristic (scans built bundle text for the distinctive `sl-` namespace)
|
|
153
|
+
// and WARN-only: it never fails the build.
|
|
154
|
+
if (meta.cssBaseline) {
|
|
155
|
+
let baseline = null;
|
|
156
|
+
try {
|
|
157
|
+
baseline = JSON.parse(readFileSync(resolve(here, '../dist/baseline.classes.json'), 'utf8'));
|
|
158
|
+
} catch {
|
|
159
|
+
warnings.push(`meta.cssBaseline is "${meta.cssBaseline}" but this SDK build has no baseline class list to check against.`);
|
|
160
|
+
}
|
|
161
|
+
if (baseline) {
|
|
162
|
+
if (baseline.version !== meta.cssBaseline) {
|
|
163
|
+
warnings.push(`meta.cssBaseline is "${meta.cssBaseline}" but this SDK ships baseline "${baseline.version}" — the host may serve a different set.`);
|
|
164
|
+
}
|
|
165
|
+
const baseSet = new Set(baseline.classes || []);
|
|
166
|
+
// SDK-defined names that aren't baseline helpers but are legitimately in bundles: the Sidekick
|
|
167
|
+
// screen's scope class (mobile-admin-container.md → Styling).
|
|
168
|
+
for (const c of ['sl-mobile-admin']) baseSet.add(c);
|
|
169
|
+
// Class usage lives in compiled markup strings — present in every bundle regardless
|
|
170
|
+
// of module format, so scan both umd + esm across all surfaces.
|
|
171
|
+
// `sl-…` NOT preceded by a word char or hyphen: a class name, never a CSS custom property
|
|
172
|
+
// (`var(--sl-color-accent)`, `--sl-widget-accent`) — those are theme tokens, not classes.
|
|
173
|
+
// Classes the app defines itself (`.sl-foo {…}` in its own CSS) aren't missing either.
|
|
174
|
+
const used = new Set();
|
|
175
|
+
const defined = new Set();
|
|
176
|
+
for (const [, block] of surfaces) {
|
|
177
|
+
for (const p of [block?.files?.js?.umd, block?.files?.js?.esm, block?.files?.css]) {
|
|
178
|
+
if (!p || typeof p !== 'string') continue;
|
|
179
|
+
const bp = resolveBundle(p);
|
|
180
|
+
if (!bp) continue;
|
|
181
|
+
const text = readFileSync(bp, 'utf8');
|
|
182
|
+
for (const m of text.matchAll(/(?<![\w-])sl-[a-z0-9-]+/g)) used.add(m[0]);
|
|
183
|
+
for (const m of text.matchAll(/\.(sl-[a-z0-9-]+)/g)) defined.add(m[1]);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const ownSl = [...defined].filter((c) => !baseSet.has(c)).sort();
|
|
187
|
+
if (ownSl.length) {
|
|
188
|
+
warnings.push(`the app's own classes use the platform's sl- prefix: ${ownSl.join(', ')} — rename them (e.g. drop the "sl-") so they can't collide with a future baseline class.`);
|
|
189
|
+
}
|
|
190
|
+
const unknown = [...used].filter((c) => !baseSet.has(c) && !defined.has(c)).sort();
|
|
191
|
+
if (used.size === 0) {
|
|
192
|
+
console.log(`${DIM}cssBaseline (${baseline.version}) — declared; no sl-* classes found in the bundles.${RESET}`);
|
|
193
|
+
} else if (unknown.length === 0) {
|
|
194
|
+
console.log(`${GREEN}✓${RESET} cssBaseline ${DIM}(${baseline.version})${RESET} — all ${used.size} sl-* classes used are in the baseline`);
|
|
195
|
+
} else {
|
|
196
|
+
console.log(`${YELLOW}⚠${RESET} cssBaseline ${DIM}(${baseline.version})${RESET} — ${unknown.length} sl-* class${unknown.length === 1 ? '' : 'es'} used but not in the baseline (they will not render):`);
|
|
197
|
+
for (const c of unknown) console.log(` ${YELLOW}${c}${RESET}`);
|
|
198
|
+
warnings.push(`sl-* classes used but not in cssBaseline "${baseline.version}": ${unknown.join(', ')} — define them in your own CSS, fix the typo, or drop them.`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ---- Host theming (theme tokens) -------------------------------------------
|
|
204
|
+
// If the app declares meta.respectsHostTheme, warn on hardcoded Tailwind PALETTE utilities in
|
|
205
|
+
// component SOURCE (e.g. bg-blue-600, text-zinc-900) — those pin a colour instead of following the
|
|
206
|
+
// host brand via the semantic tokens (bg-primary, text-foreground, border-border). Scans source,
|
|
207
|
+
// not compiled bundles (bundles are full of legitimate hex). Heuristic + WARN-only. See
|
|
208
|
+
// docs/theme-tokens.md.
|
|
209
|
+
if (meta.respectsHostTheme) {
|
|
210
|
+
const THEME_TOKENS_VERSION = 'v1';
|
|
211
|
+
if (!meta.themeTokens) {
|
|
212
|
+
warnings.push(`meta.respectsHostTheme is true but meta.themeTokens is not declared — set it to "${THEME_TOKENS_VERSION}".`);
|
|
213
|
+
} else if (meta.themeTokens !== THEME_TOKENS_VERSION) {
|
|
214
|
+
warnings.push(`meta.themeTokens is "${meta.themeTokens}" but this SDK ships theme tokens "${THEME_TOKENS_VERSION}".`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const PALETTE = 'slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose';
|
|
218
|
+
const UTIL = 'bg|text|border|ring|divide|from|via|to|fill|stroke|outline|decoration|shadow|accent|caret';
|
|
219
|
+
const paletteRe = new RegExp(`\\b(?:${UTIL})-(?:${PALETTE})-(?:50|100|200|300|400|500|600|700|800|900|950)\\b`, 'g');
|
|
220
|
+
|
|
221
|
+
const SRC_EXT = /\.(tsx|ts|jsx|js|vue|html|svelte)$/;
|
|
222
|
+
const SKIP_DIR = new Set(['node_modules', 'dist', '.nuxt', '.output', '.git', 'public']);
|
|
223
|
+
function walk(dir, out = []) {
|
|
224
|
+
let entries = [];
|
|
225
|
+
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return out; }
|
|
226
|
+
for (const e of entries) {
|
|
227
|
+
if (e.isDirectory()) { if (!SKIP_DIR.has(e.name)) walk(join(dir, e.name), out); }
|
|
228
|
+
else if (SRC_EXT.test(e.name)) out.push(join(dir, e.name));
|
|
229
|
+
}
|
|
230
|
+
return out;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const srcRoot = existsSync(join(appDir, 'src')) ? join(appDir, 'src') : appDir;
|
|
234
|
+
const offenders = [];
|
|
235
|
+
let totalHits = 0;
|
|
236
|
+
for (const f of walk(srcRoot)) {
|
|
237
|
+
const hits = [...new Set((readFileSync(f, 'utf8').match(paletteRe) || []))];
|
|
238
|
+
if (hits.length) { offenders.push({ file: f.replace(appDir + '/', '').replace(appDir + '\\', ''), hits }); totalHits += hits.length; }
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (offenders.length === 0) {
|
|
242
|
+
console.log(`${GREEN}✓${RESET} theme ${DIM}(respectsHostTheme)${RESET} — no hardcoded palette utilities in source`);
|
|
243
|
+
} else {
|
|
244
|
+
console.log(`${YELLOW}⚠${RESET} theme ${DIM}(respectsHostTheme)${RESET} — hardcoded palette utilities in ${offenders.length} file${offenders.length === 1 ? '' : 's'} (use bg-primary / text-foreground / border-border instead):`);
|
|
245
|
+
for (const o of offenders.slice(0, 15)) console.log(` ${DIM}${o.file}${RESET} ${YELLOW}${o.hits.slice(0, 6).join(' ')}${o.hits.length > 6 ? ' …' : ''}${RESET}`);
|
|
246
|
+
if (offenders.length > 15) console.log(` ${DIM}…and ${offenders.length - 15} more file(s)${RESET}`);
|
|
247
|
+
warnings.push(`respectsHostTheme is true but ${totalHits} hardcoded palette utilit${totalHits === 1 ? 'y' : 'ies'} found in source — replace with semantic tokens, or drop respectsHostTheme if the app intentionally brings its own look.`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ---- containers: a router of their own must be isolated from the host's ------------------------
|
|
252
|
+
// Since R5, react-router-dom is SHARED with the host, so a container that wraps itself in a
|
|
253
|
+
// MemoryRouter/HashRouter/BrowserRouter inside the host's router throws "You cannot render a <Router>
|
|
254
|
+
// inside another <Router>" — unless it resets the parent's router context first (docs/containers.md →
|
|
255
|
+
// "Pages that read their context from the URL"). Heuristic on the built bundle names; WARN-only.
|
|
256
|
+
{
|
|
257
|
+
const block = manifest.containers;
|
|
258
|
+
for (const p of [block?.files?.js?.esm, block?.files?.js?.umd]) {
|
|
259
|
+
if (!p) continue;
|
|
260
|
+
const bp = resolveBundle(p);
|
|
261
|
+
if (!bp) continue;
|
|
262
|
+
const text = readFileSync(bp, 'utf8');
|
|
263
|
+
const routers = ['MemoryRouter', 'HashRouter', 'BrowserRouter'].filter((r) => new RegExp(`\\b${r}\\b`).test(text));
|
|
264
|
+
if (!routers.length) { console.log(`${GREEN}✓${RESET} containers ${DIM}— no router of their own (the host's is used)${RESET}`); break; }
|
|
265
|
+
if (/\bUNSAFE_LocationContext\b/.test(text)) { console.log(`${GREEN}✓${RESET} containers ${DIM}— own ${routers.join('/')} isolated from the host's router${RESET}`); break; }
|
|
266
|
+
console.log(`${YELLOW}⚠${RESET} containers — ${routers.join('/')} without isolating it from the host's router`);
|
|
267
|
+
warnings.push(`containers bundle renders its own ${routers.join('/')} but never resets the host's router context — inside an R5 host that throws "You cannot render a <Router> inside another <Router>". Wrap it in UNSAFE_LocationContext.Provider + UNSAFE_NavigationContext.Provider value={null} (docs/containers.md), or drop the router and use the host's.`);
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ---- showing another app's widget: use the host's EmbeddedWidget, for widgets it allows -----------
|
|
273
|
+
// Hosts (Portal, Hub) pass every app an `EmbeddedWidget` prop (docs/widgets.md → "Using another app's
|
|
274
|
+
// widget"). A hand-rolled loader (getWidgets → evaluate the bundle) drifts and breaks; and the host only
|
|
275
|
+
// renders first-party widgets and the same publisher's apps, so anything else silently shows nothing.
|
|
276
|
+
// Static scan of the source; WARN-only.
|
|
277
|
+
{
|
|
278
|
+
const ownId = String(manifest.meta?.appId || '');
|
|
279
|
+
const handleOf = (id) => { const i = id.indexOf('--'); return i > 0 ? id.slice(0, i) : null; };
|
|
280
|
+
const EXT = /\.(tsx|ts|jsx|js)$/;
|
|
281
|
+
const SKIP = new Set(['node_modules', 'dist', '.git', 'public']);
|
|
282
|
+
const files = [];
|
|
283
|
+
(function walkSrc(dir) {
|
|
284
|
+
let entries = [];
|
|
285
|
+
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return; }
|
|
286
|
+
for (const e of entries) {
|
|
287
|
+
if (e.isDirectory()) { if (!SKIP.has(e.name)) walkSrc(join(dir, e.name)); }
|
|
288
|
+
else if (EXT.test(e.name)) files.push(join(dir, e.name));
|
|
289
|
+
}
|
|
290
|
+
})(existsSync(join(appDir, 'src')) ? join(appDir, 'src') : appDir);
|
|
291
|
+
const rel = (f) => (f.startsWith(appDir) ? f.slice(appDir.length + 1) : f);
|
|
292
|
+
const handRolled = [];
|
|
293
|
+
const blocked = new Set();
|
|
294
|
+
let embeds = 0;
|
|
295
|
+
for (const f of files) {
|
|
296
|
+
const text = readFileSync(f, 'utf8');
|
|
297
|
+
if (/\bgetWidgets\s*\(/.test(text) && /(new\s+Blob\s*\(|new\s+Function\s*\(|createRequireShim|import\s*\(\s*\/\*\s*@vite-ignore)/.test(text)) handRolled.push(rel(f));
|
|
298
|
+
for (const m of text.matchAll(/<EmbeddedWidget\b[^>]*?\bappId\s*=\s*\{?\s*["'`]([A-Za-z0-9._-]+)["'`]/g)) {
|
|
299
|
+
embeds++;
|
|
300
|
+
const id = m[1];
|
|
301
|
+
const owner = handleOf(id);
|
|
302
|
+
if (id === ownId || (owner !== null && owner !== handleOf(ownId))) blocked.add(id);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (handRolled.length) {
|
|
306
|
+
console.log(`${YELLOW}⚠${RESET} embedded widgets — a hand-rolled widget loader in ${handRolled.slice(0, 3).join(', ')}`);
|
|
307
|
+
warnings.push(`${handRolled.join(', ')} load${handRolled.length === 1 ? 's' : ''} another app's widget bundle by hand (getWidgets + evaluating it). Use the host's EmbeddedWidget prop instead: <EmbeddedWidget appId="…" /> (docs/widgets.md → "Using another app's widget").`);
|
|
308
|
+
}
|
|
309
|
+
if (blocked.size) {
|
|
310
|
+
console.log(`${YELLOW}⚠${RESET} embedded widgets — the host won't show: ${[...blocked].join(', ')}`);
|
|
311
|
+
warnings.push(`EmbeddedWidget for ${[...blocked].join(', ')} will render nothing: hosts only show first-party widgets and the same publisher's apps${ownId ? ` (this app is "${ownId}")` : ''}, and never the app itself.`);
|
|
312
|
+
}
|
|
313
|
+
if (embeds && !handRolled.length && !blocked.size) console.log(`${GREEN}✓${RESET} embedded widgets ${DIM}— ${embeds} via the host's EmbeddedWidget${RESET}`);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// ---- lists: declared set-up types and value lists, not keyed app data ------------------------------
|
|
317
|
+
// Keyed app data (setDataItem / getData, ctx.sl.appData.setData) has no type, no search and no checks, and
|
|
318
|
+
// is being retired (docs/admin-lists-and-pickers.md). Static scan of the source; WARN-only.
|
|
319
|
+
{
|
|
320
|
+
const { keyedAppDataCalls } = await import('./lib/keyed-app-data.mjs');
|
|
321
|
+
const EXT = /\.(tsx|ts|jsx|js|mjs)$/;
|
|
322
|
+
const SKIP = new Set(['node_modules', 'dist', '.git', 'public']);
|
|
323
|
+
const found = [];
|
|
324
|
+
(function walkKeyed(dir) {
|
|
325
|
+
let entries = [];
|
|
326
|
+
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return; }
|
|
327
|
+
for (const e of entries) {
|
|
328
|
+
if (e.isDirectory()) { if (!SKIP.has(e.name)) walkKeyed(join(dir, e.name)); }
|
|
329
|
+
else if (EXT.test(e.name)) {
|
|
330
|
+
const f = join(dir, e.name);
|
|
331
|
+
const calls = keyedAppDataCalls(readFileSync(f, 'utf8'));
|
|
332
|
+
if (calls.length) found.push([f.startsWith(appDir) ? f.slice(appDir.length + 1) : f, calls]);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
})(existsSync(join(appDir, 'src')) ? join(appDir, 'src') : appDir);
|
|
336
|
+
if (found.length) {
|
|
337
|
+
console.log(`${YELLOW}⚠${RESET} keyed app data — ${found.slice(0, 3).map(([f]) => f).join(', ')}${found.length > 3 ? ` and ${found.length - 3} more` : ''}`);
|
|
338
|
+
warnings.push(`${found.map(([f, c]) => `${f} (${c.join(', ')})`).join('; ')}: keyed app data is being retired. Keep lists as declared set-up types (app records with \`setup: true\` in the manifest's data block) and value lists (\`data.lists\`); Studio edits them and pickers read them fast (docs/admin-lists-and-pickers.md). Settings stay in getConfig / setConfig.`);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// ---- realtime: SL.realtime, never a provider library or key in the app ---------------------------
|
|
343
|
+
// SmartLinks Realtime (docs/realtime.md) hands out the stream and checks every publish; an app that talks
|
|
344
|
+
// to the provider directly bypasses the channel rules (and a provider key in a bundle is a key for all).
|
|
345
|
+
{
|
|
346
|
+
const EXT = /\.(tsx|ts|jsx|js)$/;
|
|
347
|
+
const SKIP = new Set(['node_modules', 'dist', '.git', 'public']);
|
|
348
|
+
const hitsRt = [];
|
|
349
|
+
(function walkRt(dir) {
|
|
350
|
+
let entries = [];
|
|
351
|
+
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return; }
|
|
352
|
+
for (const e of entries) {
|
|
353
|
+
if (e.isDirectory()) { if (!SKIP.has(e.name)) walkRt(join(dir, e.name)); }
|
|
354
|
+
else if (EXT.test(e.name)) {
|
|
355
|
+
const f = join(dir, e.name);
|
|
356
|
+
const text = readFileSync(f, 'utf8');
|
|
357
|
+
if (/from\s+['"]ably(\/[^'"]*)?['"]|require\(\s*['"]ably['"]\s*\)|ABLY_(API_)?KEY/.test(text)) hitsRt.push(f.startsWith(appDir) ? f.slice(appDir.length + 1) : f);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
})(existsSync(join(appDir, 'src')) ? join(appDir, 'src') : appDir);
|
|
361
|
+
if (hitsRt.length) {
|
|
362
|
+
console.log(`${YELLOW}⚠${RESET} realtime — talks to the realtime provider directly in ${hitsRt.slice(0, 3).join(', ')}`);
|
|
363
|
+
warnings.push(`${hitsRt.join(', ')} use${hitsRt.length === 1 ? 's' : ''} the realtime provider (Ably) directly. Use SL.realtime instead — declare channels in the manifest (realtime.channels) and use realtime.channel() / onChange() (docs/realtime.md); never put a provider key in an app.`);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// ---- admin cards: declared right, cached, and backed by an admin function ---------------------
|
|
368
|
+
// docs/admin-cards.md. A data card with no function (or a public one) can't load; a card with no
|
|
369
|
+
// refresh rule falls back to a 6h maxAge, which is rarely what's wanted.
|
|
370
|
+
if (manifest.adminCards) {
|
|
371
|
+
const cards = Array.isArray(manifest.adminCards.cards) ? manifest.adminCards.cards : [];
|
|
372
|
+
const fns = new Map(((manifest.functions && manifest.functions.definitions) || []).map((f) => [f && f.name, f]));
|
|
373
|
+
const SIZES = new Set(['small', 'medium', 'wide', 'full']);
|
|
374
|
+
const ON = /^(records(:[A-Za-z0-9._-]{1,60})?|products|proofs|assets|apps|collection|settings(:[A-Za-z0-9._-]{1,60})?)$/;
|
|
375
|
+
const cardProblems = [];
|
|
376
|
+
const before = warnings.length;
|
|
377
|
+
const seen = new Set();
|
|
378
|
+
if (!cards.length) warnings.push('adminCards is declared but has no cards.');
|
|
379
|
+
for (const c of cards) {
|
|
380
|
+
const id = c && typeof c.id === 'string' ? c.id : '';
|
|
381
|
+
const label = `admin card "${id || '?'}"`;
|
|
382
|
+
if (!/^[A-Za-z0-9_-]{1,40}$/.test(id)) { cardProblems.push(`${label}: id must be 1–40 letters, digits, "-" or "_".`); continue; }
|
|
383
|
+
if (seen.has(id)) cardProblems.push(`${label}: declared twice.`);
|
|
384
|
+
seen.add(id);
|
|
385
|
+
if (!c.title) warnings.push(`${label}: has no title.`);
|
|
386
|
+
const kind = c.kind || 'data';
|
|
387
|
+
if (kind !== 'data' && kind !== 'component') cardProblems.push(`${label}: kind must be "data" or "component".`);
|
|
388
|
+
if (kind === 'data' && !c.function) cardProblems.push(`${label}: a data card needs a "function" (an admin server function returning its payload).`);
|
|
389
|
+
if (c.function) {
|
|
390
|
+
const fn = fns.get(c.function);
|
|
391
|
+
if (!fn) cardProblems.push(`${label}: function "${c.function}" isn't in functions.definitions.`);
|
|
392
|
+
else if ((fn.trigger && fn.trigger.type) !== 'http') cardProblems.push(`${label}: function "${c.function}" must be an http function.`);
|
|
393
|
+
else if ((fn.visibility || 'admin') !== 'admin') cardProblems.push(`${label}: function "${c.function}" must have visibility "admin" — card data is for the collection's admins.`);
|
|
394
|
+
}
|
|
395
|
+
if (kind === 'component') {
|
|
396
|
+
if (!c.component) cardProblems.push(`${label}: a component card needs "component" (the export name in the adminCards bundle).`);
|
|
397
|
+
if (!manifest.adminCards.files || !manifest.adminCards.files.js) cardProblems.push(`${label}: component cards need adminCards.files (the adminCards bundle).`);
|
|
398
|
+
if (!c.function) warnings.push(`${label}: give the component a "function" too — it then gets cached data as a prop instead of fetching on every dashboard load.`);
|
|
399
|
+
}
|
|
400
|
+
for (const s of c.sizes || []) if (!SIZES.has(s)) cardProblems.push(`${label}: unknown size "${s}" (small, medium, wide, full).`);
|
|
401
|
+
const refresh = c.refresh || {};
|
|
402
|
+
for (const on of refresh.on || []) if (!ON.test(String(on))) cardProblems.push(`${label}: refresh.on "${on}" isn't a change kind (records, records:<type>, products, proofs, assets, apps, collection, settings, settings:<group>).`);
|
|
403
|
+
if (!(refresh.on || []).length && refresh.maxAge == null) {
|
|
404
|
+
warnings.push(`${label}: no refresh rule — it's recomputed every 6h. Add refresh.on (what its data depends on, e.g. "records:question") so it updates when that changes and never otherwise.`);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
for (const msg of cardProblems) problems.push({ surface: 'adminCards', spec: null, msg });
|
|
408
|
+
if (cardProblems.length) {
|
|
409
|
+
console.log(`${RED}✗${RESET} admin cards`);
|
|
410
|
+
for (const msg of cardProblems) console.log(` ${RED}${msg}${RESET}`);
|
|
411
|
+
} else if (warnings.length === before) console.log(`${GREEN}✓${RESET} admin cards ${DIM}— ${cards.length} card${cards.length === 1 ? '' : 's'}, each backed and cached${RESET}`);
|
|
412
|
+
else console.log(`${YELLOW}⚠${RESET} admin cards — see below`);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// ---- package.json scripts point at files that exist ------------------------------------------
|
|
416
|
+
// An upgrade that copies a script LINE but not the script FILE breaks every build at that step
|
|
417
|
+
// (e.g. `postbuild: node scripts/smartlinks-deploy.mjs` with no scripts/smartlinks-deploy.mjs).
|
|
418
|
+
// WARN — the doctor runs inside the build, and a skipped hook (Forge builds skip pre/post) is harmless.
|
|
419
|
+
try {
|
|
420
|
+
const pkg = JSON.parse(readFileSync(join(appDir, 'package.json'), 'utf8'));
|
|
421
|
+
const missing = [];
|
|
422
|
+
for (const [name, cmd] of Object.entries(pkg.scripts || {})) {
|
|
423
|
+
if (typeof cmd !== 'string') continue;
|
|
424
|
+
for (const m of cmd.matchAll(/(?:^|[;&|]\s*|\s)(?:node|tsx|ts-node|bash|sh)\s+(?:-{1,2}[\w-]+(?:=\S+)?\s+)*((?:\.{1,2}\/)?[\w./-]+\.(?:mjs|cjs|js|ts|sh))\b/g)) {
|
|
425
|
+
if (!existsSync(join(appDir, m[1]))) missing.push(`"${name}" runs ${m[1]}`);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (missing.length) {
|
|
429
|
+
console.log(`${YELLOW}⚠${RESET} package.json scripts — ${missing.length} point${missing.length === 1 ? 's' : ''} at a file that doesn't exist:`);
|
|
430
|
+
for (const x of missing) console.log(` ${YELLOW}${x}${RESET}`);
|
|
431
|
+
warnings.push(`package.json ${missing.join('; ')} — but the file is missing. Add the file, or remove the script (a pre/post hook like postbuild fails every \`npm run build\` without it).`);
|
|
432
|
+
} else {
|
|
433
|
+
console.log(`${GREEN}✓${RESET} package.json scripts ${DIM}— every script file they run exists${RESET}`);
|
|
434
|
+
}
|
|
435
|
+
} catch {
|
|
436
|
+
/* no package.json — not an npm app */
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
console.log('');
|
|
440
|
+
for (const w of warnings) console.log(`${YELLOW}⚠ ${w}${RESET}`);
|
|
441
|
+
|
|
442
|
+
if (problems.length) {
|
|
443
|
+
const contractProblems = problems.filter((p) => p.surface !== 'adminCards').length;
|
|
444
|
+
const cardCount = problems.length - contractProblems;
|
|
445
|
+
if (contractProblems) {
|
|
446
|
+
console.log(`\n${RED}${BOLD}FAIL${RESET} — ${contractProblems} problem${contractProblems === 1 ? '' : 's'} outside the shared-dependency contract.`);
|
|
447
|
+
console.log(`${DIM}Externalize only the ${CONTRACT.size} contract specifiers (import { SHARED_DEPENDENCY_SPECIFIERS } from '@proveanything/smartlinks'); bundle everything else.${RESET}`);
|
|
448
|
+
}
|
|
449
|
+
if (cardCount) console.log(`\n${RED}${BOLD}FAIL${RESET} — ${cardCount} admin card problem${cardCount === 1 ? '' : 's'} (docs/admin-cards.md).`);
|
|
450
|
+
process.exit(1);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
console.log(`${GREEN}${BOLD}OK${RESET} — bundles conform to shared-dependency contract ${SHARED_DEPENDENCY_CONTRACT_VERSION}.`);
|
|
454
|
+
process.exit(0);
|