@bitakit/cli 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +153 -2
- package/package.json +44 -4
- package/src/add.mjs +190 -0
- package/src/agent-rules.mjs +52 -0
- package/src/build.mjs +203 -0
- package/src/cli.mjs +266 -0
- package/src/doctor.mjs +908 -0
- package/src/index.mjs +30 -0
- package/src/init.mjs +254 -0
- package/src/inventory.mjs +126 -0
- package/src/project.mjs +97 -0
- package/src/routes.mjs +124 -0
- package/src/templates.mjs +530 -0
package/src/doctor.mjs
ADDED
|
@@ -0,0 +1,908 @@
|
|
|
1
|
+
import { readdir, readFile, realpath } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
import {
|
|
5
|
+
describeBitaKit,
|
|
6
|
+
describeModule,
|
|
7
|
+
describeDefinitions,
|
|
8
|
+
cssImports,
|
|
9
|
+
navigationPlacement,
|
|
10
|
+
} from '@bitakit/discovery';
|
|
11
|
+
import { exists, findProject } from './project.mjs';
|
|
12
|
+
import { appRoutes, catchAllRoute, rootLayouts, shellGroup } from './routes.mjs';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Checks a project for the problems that most often break a BitaKit app: mixed package
|
|
16
|
+
* versions, duplicate Core copies, missing wiring, unreachable plugin pages and links,
|
|
17
|
+
* colliding service aliases, `requires` nobody satisfies, plugin dependency cycles, missing
|
|
18
|
+
* styles and deprecated layouts.
|
|
19
|
+
* Each result: `{ status: 'ok' | 'warn' | 'error', title, details, fix }`.
|
|
20
|
+
*/
|
|
21
|
+
export async function doctor({ cwd } = {}) {
|
|
22
|
+
const project = await findProject(cwd);
|
|
23
|
+
const results = [await checkVersions(project), await checkCopies(project)];
|
|
24
|
+
if (project.kind === 'plugin') {
|
|
25
|
+
results.push(checkPluginPackage(project));
|
|
26
|
+
try {
|
|
27
|
+
if (!(await exists(project.moduleRoot))) return { project, results };
|
|
28
|
+
const module = await describeModule(project.moduleRoot);
|
|
29
|
+
const info = await describeDefinitions(module.slotDefinitions, {
|
|
30
|
+
helper: 'defineSlot',
|
|
31
|
+
moduleId: project.pluginId,
|
|
32
|
+
properties: ['accepts', 'policy'],
|
|
33
|
+
});
|
|
34
|
+
results.push(
|
|
35
|
+
checkSlots({
|
|
36
|
+
app: { ...module, slots: [] },
|
|
37
|
+
plugins: [],
|
|
38
|
+
slots: [],
|
|
39
|
+
slotDeclarations: info.definitions,
|
|
40
|
+
unresolvedSlots: info.unresolved,
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
results.push({ status: 'error', title: 'Slot discovery failed', details: [error.message] });
|
|
45
|
+
}
|
|
46
|
+
return { project, results };
|
|
47
|
+
}
|
|
48
|
+
if (project.configError) {
|
|
49
|
+
results.push({
|
|
50
|
+
status: 'error',
|
|
51
|
+
title: `${path.basename(project.configFile)} could not be loaded`,
|
|
52
|
+
details: [project.configError.message],
|
|
53
|
+
fix: project.legacyConfig
|
|
54
|
+
? 'Run "npx bitakit migrate": it rewrites foundation.config.ts to bitakit.config.ts with defineBitaKit from @bitakit/discovery.'
|
|
55
|
+
: 'Fix the error above; discovery reads this file to find your plugins.',
|
|
56
|
+
});
|
|
57
|
+
return { project, results };
|
|
58
|
+
}
|
|
59
|
+
let description;
|
|
60
|
+
try {
|
|
61
|
+
description = await describeBitaKit(project.root);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
results.push({ status: 'error', title: 'Discovery failed', details: [error.message] });
|
|
64
|
+
return { project, results };
|
|
65
|
+
}
|
|
66
|
+
results.push(...(await checkWiring(project)));
|
|
67
|
+
const modules = [
|
|
68
|
+
{ ...description.app, isApp: true },
|
|
69
|
+
...description.plugins.filter((plugin) => plugin.enabled),
|
|
70
|
+
];
|
|
71
|
+
const scanned = await Promise.all(
|
|
72
|
+
modules.map(async (module) => ({
|
|
73
|
+
module,
|
|
74
|
+
pages: await pagePaths(project.root, module),
|
|
75
|
+
links: await literals(
|
|
76
|
+
project.root,
|
|
77
|
+
module.actions,
|
|
78
|
+
['defineAction', 'defineURLAction'],
|
|
79
|
+
'href',
|
|
80
|
+
),
|
|
81
|
+
aliases: await literals(
|
|
82
|
+
project.root,
|
|
83
|
+
module.services,
|
|
84
|
+
['defineService', 'provideService'],
|
|
85
|
+
'exposeAs',
|
|
86
|
+
),
|
|
87
|
+
})),
|
|
88
|
+
);
|
|
89
|
+
const routes = await appRoutes(project);
|
|
90
|
+
const shell = '@bitakit/shell' in { ...project.manifest.dependencies };
|
|
91
|
+
results.push(checkPages(project, scanned, routes, { shell, group: await shellGroup(project) }));
|
|
92
|
+
results.push(checkLinks(scanned, routes));
|
|
93
|
+
results.push(checkAliases(scanned));
|
|
94
|
+
results.push(await checkRequirements(project, description, scanned));
|
|
95
|
+
results.push(...checkGraph(description));
|
|
96
|
+
results.push(await checkStyles(project, description));
|
|
97
|
+
results.push(...(await checkTailwind(project, description)));
|
|
98
|
+
results.push(checkIgnored(description), checkDeprecated(description), checkSlots(description));
|
|
99
|
+
if (shell) {
|
|
100
|
+
const missing = [];
|
|
101
|
+
for (const module of modules)
|
|
102
|
+
for (const entry of module.actions) {
|
|
103
|
+
const source = await readFile(path.join(project.root, entry.file), 'utf8');
|
|
104
|
+
for (const issue of navigationPlacement(source).missing)
|
|
105
|
+
missing.push(
|
|
106
|
+
`${entry.file}:${issue.line}: navigation group has no explicit slot placement.`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
if (missing.length)
|
|
110
|
+
results.push({
|
|
111
|
+
status: 'warn',
|
|
112
|
+
title: 'Navigation Actions still rely on groups',
|
|
113
|
+
details: missing,
|
|
114
|
+
fix: 'Choose the intended owner slot. For the default Shell run bitakit migrate --navigation-slot shell.navigation --dry-run, review it, then apply. Logical groups alone do not place UI.',
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return { project, results };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Installed @bitakit packages as Node resolves them: nearest node_modules first, then parents. */
|
|
121
|
+
async function installedPackages(root) {
|
|
122
|
+
const packages = new Map();
|
|
123
|
+
for (let directory = root; ; directory = path.dirname(directory)) {
|
|
124
|
+
const scope = path.join(directory, 'node_modules/@bitakit');
|
|
125
|
+
for (const name of await readdir(scope).catch(() => [])) {
|
|
126
|
+
if (packages.has(name)) continue;
|
|
127
|
+
const manifest = await readFile(path.join(scope, name, 'package.json'), 'utf8')
|
|
128
|
+
.then(JSON.parse)
|
|
129
|
+
.catch(() => undefined);
|
|
130
|
+
// Private workspace apps link into node_modules too; they are not published packages.
|
|
131
|
+
if (manifest?.version && !manifest.private)
|
|
132
|
+
packages.set(name, {
|
|
133
|
+
name: manifest.name ?? `@bitakit/${name}`,
|
|
134
|
+
version: manifest.version,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
if (path.dirname(directory) === directory) return [...packages.values()];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
async function installRoot(root) {
|
|
141
|
+
for (
|
|
142
|
+
let directory = path.dirname(root);
|
|
143
|
+
path.dirname(directory) !== directory;
|
|
144
|
+
directory = path.dirname(directory)
|
|
145
|
+
) {
|
|
146
|
+
const manifest = await readFile(path.join(directory, 'package.json'), 'utf8').then(
|
|
147
|
+
JSON.parse,
|
|
148
|
+
() => undefined,
|
|
149
|
+
);
|
|
150
|
+
if (manifest?.workspaces) return directory;
|
|
151
|
+
}
|
|
152
|
+
return root;
|
|
153
|
+
}
|
|
154
|
+
async function checkVersions(project) {
|
|
155
|
+
const packages = (await installedPackages(project.root)).filter(
|
|
156
|
+
(item) => item.name !== '@bitakit/create-app',
|
|
157
|
+
);
|
|
158
|
+
if (!packages.length)
|
|
159
|
+
return { status: 'warn', title: 'No @bitakit packages installed', fix: 'Run npm install.' };
|
|
160
|
+
const versions = [...new Set(packages.map((item) => item.version))];
|
|
161
|
+
if (versions.length === 1)
|
|
162
|
+
return { status: 'ok', title: `All ${packages.length} @bitakit packages are ${versions[0]}` };
|
|
163
|
+
return {
|
|
164
|
+
status: 'error',
|
|
165
|
+
title: 'Mixed @bitakit versions',
|
|
166
|
+
details: packages.map((item) => `${item.name}@${item.version}`),
|
|
167
|
+
fix: `Install one release for all of them: npm install ${packages.map((item) => `${item.name}@latest`).join(' ')}`,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/** Linked plugins with their own node_modules load a second copy of Core. */
|
|
171
|
+
async function checkCopies(project) {
|
|
172
|
+
const copies = new Set();
|
|
173
|
+
const visit = async (directory, depth) => {
|
|
174
|
+
if (depth > 6) return;
|
|
175
|
+
const core = path.join(directory, 'node_modules/@bitakit/core');
|
|
176
|
+
if (await exists(path.join(core, 'package.json'))) copies.add(await realpath(core));
|
|
177
|
+
const entries = await readdir(path.join(directory, 'node_modules'), {
|
|
178
|
+
withFileTypes: true,
|
|
179
|
+
}).catch(() => []);
|
|
180
|
+
for (const entry of entries) {
|
|
181
|
+
if (entry.name.startsWith('.')) continue;
|
|
182
|
+
const children = entry.name.startsWith('@')
|
|
183
|
+
? (await readdir(path.join(directory, 'node_modules', entry.name)).catch(() => [])).map(
|
|
184
|
+
(name) => path.join(directory, 'node_modules', entry.name, name),
|
|
185
|
+
)
|
|
186
|
+
: [path.join(directory, 'node_modules', entry.name)];
|
|
187
|
+
for (const child of children) {
|
|
188
|
+
const real = await realpath(child).catch(() => child);
|
|
189
|
+
if (!real.split(path.sep).includes('node_modules')) await visit(real, depth + 1);
|
|
190
|
+
else if (depth < 2) await visit(child, depth + 1);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
await visit(await installRoot(project.root), 0);
|
|
195
|
+
if (copies.size <= 1)
|
|
196
|
+
return {
|
|
197
|
+
status: 'ok',
|
|
198
|
+
title: copies.size ? 'One copy of @bitakit/core' : 'Core is not installed yet',
|
|
199
|
+
};
|
|
200
|
+
return {
|
|
201
|
+
status: 'error',
|
|
202
|
+
title: `${copies.size} copies of @bitakit/core are installed`,
|
|
203
|
+
details: [...copies].map((copy) => path.relative(project.root, copy) || copy),
|
|
204
|
+
fix: 'Plugins must list @bitakit/core as a peerDependency. For a linked plugin, delete its own node_modules/@bitakit/core or install it from a packed tarball (npm pack).',
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
function checkPluginPackage(project) {
|
|
208
|
+
const manifest = project.manifest;
|
|
209
|
+
const problems = [];
|
|
210
|
+
if (!manifest.exports?.['./package.json'])
|
|
211
|
+
problems.push('Export "./package.json" so apps can read the bitakit metadata.');
|
|
212
|
+
const entry = project.metadata?.plugin;
|
|
213
|
+
if (!entry || !manifest.exports?.[entry])
|
|
214
|
+
problems.push(`Export ${entry ?? '"./plugin"'} (the plugin entry named in bitakit.plugin).`);
|
|
215
|
+
if (manifest.dependencies?.['@bitakit/core'])
|
|
216
|
+
problems.push(
|
|
217
|
+
'Move @bitakit/core from dependencies to peerDependencies to avoid a second copy.',
|
|
218
|
+
);
|
|
219
|
+
if (!manifest.peerDependencies?.['@bitakit/core'])
|
|
220
|
+
problems.push('Add @bitakit/core to peerDependencies.');
|
|
221
|
+
if (manifest.foundation && !manifest.bitakit)
|
|
222
|
+
problems.push('Rename the "foundation" field in package.json to "bitakit".');
|
|
223
|
+
return problems.length
|
|
224
|
+
? { status: 'error', title: 'Plugin package manifest needs changes', details: problems }
|
|
225
|
+
: { status: 'ok', title: `Plugin package "${project.pluginId}" is ready to publish` };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Config wrapper, provider and ignore rules. */
|
|
229
|
+
async function checkWiring(project) {
|
|
230
|
+
const results = [];
|
|
231
|
+
const read = (file) => readFile(path.join(project.root, file), 'utf8').catch(() => '');
|
|
232
|
+
if (project.framework === 'next') {
|
|
233
|
+
const config =
|
|
234
|
+
(await read('next.config.ts')) ||
|
|
235
|
+
(await read('next.config.mjs')) ||
|
|
236
|
+
(await read('next.config.js'));
|
|
237
|
+
results.push(
|
|
238
|
+
/withBitaKit/.test(config)
|
|
239
|
+
? { status: 'ok', title: 'next.config wraps the configuration with withBitaKit' }
|
|
240
|
+
: /withFoundation/.test(config)
|
|
241
|
+
? {
|
|
242
|
+
status: 'error',
|
|
243
|
+
title: 'next.config uses withFoundation, which 0.2 removed',
|
|
244
|
+
fix: 'Run "npx bitakit migrate", or rename withFoundation to withBitaKit.',
|
|
245
|
+
}
|
|
246
|
+
: {
|
|
247
|
+
status: 'error',
|
|
248
|
+
title: 'next.config does not run BitaKit discovery',
|
|
249
|
+
fix: "import { withBitaKit } from '@bitakit/next/config';\nexport default withBitaKit({});",
|
|
250
|
+
},
|
|
251
|
+
);
|
|
252
|
+
const scripts = JSON.stringify(project.manifest.scripts ?? {});
|
|
253
|
+
if (scripts.includes('--webpack'))
|
|
254
|
+
results.push({
|
|
255
|
+
status: 'warn',
|
|
256
|
+
title: 'Scripts still force Webpack',
|
|
257
|
+
fix: 'Remove --webpack from the dev and build scripts; the generated file needs no bundler alias.',
|
|
258
|
+
});
|
|
259
|
+
if (scripts.includes('foundation-generate'))
|
|
260
|
+
results.push({
|
|
261
|
+
status: 'warn',
|
|
262
|
+
title: 'Scripts use the old foundation-generate command',
|
|
263
|
+
fix: 'Replace foundation-generate with "bitakit generate".',
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
if (project.framework === 'tanstack') {
|
|
267
|
+
const config = await read('vite.config.ts');
|
|
268
|
+
results.push(
|
|
269
|
+
/\bbitakit\(/.test(config)
|
|
270
|
+
? { status: 'ok', title: 'vite.config runs the BitaKit plugin' }
|
|
271
|
+
: /\bfoundation\(/.test(config)
|
|
272
|
+
? {
|
|
273
|
+
status: 'error',
|
|
274
|
+
title: 'vite.config uses foundation(), which 0.2 removed',
|
|
275
|
+
fix: 'Run "npx bitakit migrate", or import { bitakit } from \'@bitakit/tanstack/config\' and call bitakit().',
|
|
276
|
+
}
|
|
277
|
+
: {
|
|
278
|
+
status: 'error',
|
|
279
|
+
title: 'vite.config does not run BitaKit discovery',
|
|
280
|
+
fix: "import { bitakit } from '@bitakit/tanstack/config';\nplugins: [bitakit(), tanstackStart(), ...]",
|
|
281
|
+
},
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
let provider = false;
|
|
285
|
+
for (const file of rootLayouts(project))
|
|
286
|
+
if (await mountsProvider(path.join(project.root, file), project.root)) provider = true;
|
|
287
|
+
results.push(
|
|
288
|
+
provider
|
|
289
|
+
? { status: 'ok', title: 'The root layout mounts the BitaKit provider' }
|
|
290
|
+
: {
|
|
291
|
+
status: 'error',
|
|
292
|
+
title: 'No BitaKit provider in the root layout',
|
|
293
|
+
fix: "import { BitaKitProvider } from '@/bitakit.gen';\n<BitaKitProvider>{children}</BitaKitProvider>",
|
|
294
|
+
},
|
|
295
|
+
);
|
|
296
|
+
const ignore = await read('.gitignore');
|
|
297
|
+
if (!ignore.includes('bitakit.gen'))
|
|
298
|
+
results.push({
|
|
299
|
+
status: 'warn',
|
|
300
|
+
title: 'Generated files are not ignored by Git',
|
|
301
|
+
fix: 'Add "src/bitakit.gen.*" to .gitignore.',
|
|
302
|
+
});
|
|
303
|
+
if (project.legacyConfig)
|
|
304
|
+
results.push({
|
|
305
|
+
status: 'warn',
|
|
306
|
+
title: 'foundation.config.ts is deprecated',
|
|
307
|
+
fix: 'Rename it to bitakit.config.ts and use defineBitaKit (npx bitakit migrate).',
|
|
308
|
+
});
|
|
309
|
+
return results;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Follow statically rendered local wrappers; an unused import or comment is not a mount. */
|
|
313
|
+
async function mountsProvider(file, root, seen = new Set()) {
|
|
314
|
+
if (seen.has(file)) return false;
|
|
315
|
+
seen.add(file);
|
|
316
|
+
const code = await readFile(file, 'utf8').catch(() => '');
|
|
317
|
+
const tree = ts.createSourceFile(file, code, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
318
|
+
const imports = new Map();
|
|
319
|
+
const rendered = new Set();
|
|
320
|
+
for (const statement of tree.statements) {
|
|
321
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
|
|
322
|
+
continue;
|
|
323
|
+
const from = statement.moduleSpecifier.text;
|
|
324
|
+
const clause = statement.importClause;
|
|
325
|
+
if (clause?.name) imports.set(clause.name.text, { from, name: 'default' });
|
|
326
|
+
if (clause?.namedBindings && ts.isNamedImports(clause.namedBindings))
|
|
327
|
+
for (const binding of clause.namedBindings.elements)
|
|
328
|
+
imports.set(binding.name.text, {
|
|
329
|
+
from,
|
|
330
|
+
name: binding.propertyName?.text ?? binding.name.text,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
const visit = (node) => {
|
|
334
|
+
if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node))
|
|
335
|
+
rendered.add(node.tagName.getText(tree));
|
|
336
|
+
ts.forEachChild(node, visit);
|
|
337
|
+
};
|
|
338
|
+
visit(tree);
|
|
339
|
+
for (const name of rendered) {
|
|
340
|
+
const entry = imports.get(name);
|
|
341
|
+
if (!entry) continue;
|
|
342
|
+
// The generated file exports BitaKitProvider; the packages export BitaKitRuntimeProvider.
|
|
343
|
+
if (
|
|
344
|
+
(entry.name === 'BitaKitProvider' && /bitakit\.gen$/.test(entry.from)) ||
|
|
345
|
+
(entry.name === 'BitaKitRuntimeProvider' && /^@bitakit\/(?:ui|next|tanstack)$/.test(entry.from))
|
|
346
|
+
)
|
|
347
|
+
return true;
|
|
348
|
+
const base = entry.from.startsWith('@/')
|
|
349
|
+
? path.join(root, 'src', entry.from.slice(2))
|
|
350
|
+
: entry.from.startsWith('.')
|
|
351
|
+
? path.resolve(path.dirname(file), entry.from)
|
|
352
|
+
: undefined;
|
|
353
|
+
if (!base) continue;
|
|
354
|
+
for (const target of [
|
|
355
|
+
base,
|
|
356
|
+
`${base}.tsx`,
|
|
357
|
+
`${base}.ts`,
|
|
358
|
+
`${base}.jsx`,
|
|
359
|
+
`${base}.js`,
|
|
360
|
+
path.join(base, 'index.tsx'),
|
|
361
|
+
]) {
|
|
362
|
+
if (await mountsProvider(target, root, seen)) return true;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const unwrap = (node) =>
|
|
369
|
+
ts.isAsExpression(node) || ts.isSatisfiesExpression(node) || ts.isParenthesizedExpression(node)
|
|
370
|
+
? unwrap(node.expression)
|
|
371
|
+
: node;
|
|
372
|
+
/** The string literals of a value, or of the elements of an array literal: `'a'` and `['a', 'b']`. */
|
|
373
|
+
const stringsOf = (node) => {
|
|
374
|
+
const value = unwrap(node);
|
|
375
|
+
return (ts.isArrayLiteralExpression(value) ? value.elements : [value]).filter(
|
|
376
|
+
ts.isStringLiteralLike,
|
|
377
|
+
);
|
|
378
|
+
};
|
|
379
|
+
async function parse(root, entry) {
|
|
380
|
+
const source = await readFile(path.join(root, entry.file), 'utf8').catch(() => undefined);
|
|
381
|
+
return source === undefined
|
|
382
|
+
? undefined
|
|
383
|
+
: ts.createSourceFile(entry.file, source, ts.ScriptTarget.Latest, true);
|
|
384
|
+
}
|
|
385
|
+
/** String literal values of `property` in calls such as `defineAction({ href: '/x' })`; array elements count too. */
|
|
386
|
+
async function literals(root, entries, callees, property) {
|
|
387
|
+
const found = [];
|
|
388
|
+
for (const entry of entries) {
|
|
389
|
+
const file = await parse(root, entry);
|
|
390
|
+
if (!file) continue;
|
|
391
|
+
const visit = (node) => {
|
|
392
|
+
// Options may follow a factory argument: defineService(() => ..., { exposeAs: 'items' }).
|
|
393
|
+
if (
|
|
394
|
+
ts.isCallExpression(node) &&
|
|
395
|
+
ts.isIdentifier(node.expression) &&
|
|
396
|
+
callees.includes(node.expression.text)
|
|
397
|
+
)
|
|
398
|
+
for (const argument of node.arguments.filter(ts.isObjectLiteralExpression))
|
|
399
|
+
for (const item of argument.properties)
|
|
400
|
+
if (ts.isPropertyAssignment(item) && item.name.getText(file) === property)
|
|
401
|
+
for (const value of stringsOf(item.initializer))
|
|
402
|
+
found.push({ value: value.text, file: entry.file });
|
|
403
|
+
ts.forEachChild(node, visit);
|
|
404
|
+
};
|
|
405
|
+
visit(file);
|
|
406
|
+
}
|
|
407
|
+
return found;
|
|
408
|
+
}
|
|
409
|
+
/** String literals of a top-level `export const <name> = [...]`, such as the `requires` of a provider file. */
|
|
410
|
+
async function exportedLiterals(root, entries, name) {
|
|
411
|
+
const found = [];
|
|
412
|
+
for (const entry of entries) {
|
|
413
|
+
const file = await parse(root, entry);
|
|
414
|
+
for (const statement of file?.statements ?? [])
|
|
415
|
+
if (
|
|
416
|
+
ts.isVariableStatement(statement) &&
|
|
417
|
+
statement.modifiers?.some((item) => item.kind === ts.SyntaxKind.ExportKeyword)
|
|
418
|
+
)
|
|
419
|
+
for (const { name: declared, initializer } of statement.declarationList.declarations)
|
|
420
|
+
if (ts.isIdentifier(declared) && declared.text === name && initializer)
|
|
421
|
+
for (const value of stringsOf(initializer))
|
|
422
|
+
found.push({ value: value.text, file: entry.file });
|
|
423
|
+
}
|
|
424
|
+
return found;
|
|
425
|
+
}
|
|
426
|
+
/** Contract names of the keys core exports; other keys are read from `serviceKey('name')` in the same file. */
|
|
427
|
+
const coreKeys = new Map([
|
|
428
|
+
['authService', 'auth'],
|
|
429
|
+
['translationService', 'i18n'],
|
|
430
|
+
['settingsStorage', 'settings.storage'],
|
|
431
|
+
]);
|
|
432
|
+
/** Contract names that services register with `provides`; `unresolved` when a key's name cannot be read. */
|
|
433
|
+
async function providedKeys(root, entries) {
|
|
434
|
+
const names = [];
|
|
435
|
+
let unresolved = false;
|
|
436
|
+
for (const entry of entries) {
|
|
437
|
+
const file = await parse(root, entry);
|
|
438
|
+
if (!file) continue;
|
|
439
|
+
const declared = new Map();
|
|
440
|
+
const used = [];
|
|
441
|
+
const nameOf = (node) =>
|
|
442
|
+
// `defineContract('name').of<T>()` carries its name in the inner call.
|
|
443
|
+
ts.isCallExpression(node) &&
|
|
444
|
+
ts.isPropertyAccessExpression(node.expression) &&
|
|
445
|
+
node.expression.name.text === 'of'
|
|
446
|
+
? nameOf(node.expression.expression)
|
|
447
|
+
: ts.isCallExpression(node) &&
|
|
448
|
+
ts.isIdentifier(node.expression) &&
|
|
449
|
+
['serviceKey', 'defineContract'].includes(node.expression.text) &&
|
|
450
|
+
node.arguments[0] &&
|
|
451
|
+
ts.isStringLiteralLike(node.arguments[0])
|
|
452
|
+
? node.arguments[0].text
|
|
453
|
+
: undefined;
|
|
454
|
+
const visit = (node) => {
|
|
455
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer) {
|
|
456
|
+
const name = nameOf(node.initializer);
|
|
457
|
+
if (name) declared.set(node.name.text, name);
|
|
458
|
+
}
|
|
459
|
+
if (
|
|
460
|
+
ts.isCallExpression(node) &&
|
|
461
|
+
ts.isIdentifier(node.expression) &&
|
|
462
|
+
node.expression.text === 'defineService'
|
|
463
|
+
) {
|
|
464
|
+
// Options held in a variable cannot be read.
|
|
465
|
+
if (node.arguments.slice(1).some((argument) => !ts.isObjectLiteralExpression(argument)))
|
|
466
|
+
unresolved = true;
|
|
467
|
+
for (const argument of node.arguments.filter(ts.isObjectLiteralExpression))
|
|
468
|
+
for (const item of argument.properties)
|
|
469
|
+
if (item.name?.getText(file) === 'provides')
|
|
470
|
+
used.push(ts.isPropertyAssignment(item) ? item.initializer : item.name);
|
|
471
|
+
}
|
|
472
|
+
ts.forEachChild(node, visit);
|
|
473
|
+
};
|
|
474
|
+
visit(file);
|
|
475
|
+
for (const key of used) {
|
|
476
|
+
const name = ts.isIdentifier(key)
|
|
477
|
+
? (declared.get(key.text) ?? coreKeys.get(key.text))
|
|
478
|
+
: nameOf(key);
|
|
479
|
+
if (name) names.push(name);
|
|
480
|
+
else unresolved = true;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return { names, unresolved };
|
|
484
|
+
}
|
|
485
|
+
/** Page paths: derived from `ui/pages` file names, or the `definePage({ path })` literal of a legacy page. */
|
|
486
|
+
async function pagePaths(root, module) {
|
|
487
|
+
const pages = [];
|
|
488
|
+
for (const entry of module.pages) {
|
|
489
|
+
const derived =
|
|
490
|
+
`${module.isApp ? '' : `/${module.id}`}${entry.path ? `/${entry.path.split('.').join('/')}` : ''}` ||
|
|
491
|
+
'/';
|
|
492
|
+
// A legacy definePage({ path }) export keeps its own path wherever the file lives.
|
|
493
|
+
const [literal] = await literals(root, [entry], ['definePage'], 'path');
|
|
494
|
+
pages.push({ value: literal?.value ?? derived, file: entry.file });
|
|
495
|
+
}
|
|
496
|
+
return pages;
|
|
497
|
+
}
|
|
498
|
+
const label = (module) => (module.isApp ? 'the app' : `plugin "${module.id}"`);
|
|
499
|
+
const match = (routes, href) =>
|
|
500
|
+
routes.filter((route) => route.pattern.test(href.replace(/\[[^\]]+\]/g, 'x')));
|
|
501
|
+
|
|
502
|
+
function checkPages(project, scanned, routes, options) {
|
|
503
|
+
const problems = [];
|
|
504
|
+
let fix;
|
|
505
|
+
for (const { module, pages } of scanned)
|
|
506
|
+
for (const page of pages) {
|
|
507
|
+
const matching = match(routes, page.value);
|
|
508
|
+
const owner = matching.find((route) => !route.catchAll);
|
|
509
|
+
if (owner)
|
|
510
|
+
problems.push(
|
|
511
|
+
`${label(module)} has a page at ${page.value}, but the app route ${owner.file} takes precedence.`,
|
|
512
|
+
);
|
|
513
|
+
else if (!matching.length) {
|
|
514
|
+
problems.push(
|
|
515
|
+
`${label(module)} has a page at ${page.value} (${page.file}), but no route renders plugin pages.`,
|
|
516
|
+
);
|
|
517
|
+
const route = catchAllRoute(project, options);
|
|
518
|
+
fix = `Create ${route.file}:\n\n${route.content}`;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
const count = scanned.reduce((total, item) => total + item.pages.length, 0);
|
|
522
|
+
return problems.length
|
|
523
|
+
? { status: 'error', title: 'Some plugin pages cannot be reached', details: problems, fix }
|
|
524
|
+
: {
|
|
525
|
+
status: 'ok',
|
|
526
|
+
title: !count
|
|
527
|
+
? 'No plugin pages'
|
|
528
|
+
: count === 1
|
|
529
|
+
? 'The plugin page can be reached'
|
|
530
|
+
: `All ${count} plugin pages can be reached`,
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
function checkLinks(scanned, routes) {
|
|
534
|
+
const pages = new Set(scanned.flatMap((item) => item.pages.map((page) => page.value)));
|
|
535
|
+
const problems = [];
|
|
536
|
+
let count = 0;
|
|
537
|
+
for (const { module, links } of scanned)
|
|
538
|
+
for (const link of links) {
|
|
539
|
+
if (!link.value.startsWith('/') || link.value.startsWith('//')) continue;
|
|
540
|
+
count++;
|
|
541
|
+
const href = link.value.replace(/[?#].*$/, '') || '/';
|
|
542
|
+
if (pages.has(href)) continue;
|
|
543
|
+
if (match(routes, href).some((route) => !route.catchAll)) continue;
|
|
544
|
+
problems.push(
|
|
545
|
+
`${label(module)} links to ${href} (${link.file}), but no page or route exists there.`,
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
return problems.length
|
|
549
|
+
? {
|
|
550
|
+
status: 'warn',
|
|
551
|
+
title: 'Some navigation links lead nowhere',
|
|
552
|
+
details: problems,
|
|
553
|
+
fix: 'Add a page under ui/pages or an app route for these paths.',
|
|
554
|
+
}
|
|
555
|
+
: {
|
|
556
|
+
status: 'ok',
|
|
557
|
+
title: !count
|
|
558
|
+
? 'No navigation links'
|
|
559
|
+
: count === 1
|
|
560
|
+
? 'The navigation link has a page'
|
|
561
|
+
: `All ${count} navigation links have a page`,
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
/** Service IDs are namespaced per plugin; only `exposeAs` aliases can collide. */
|
|
565
|
+
function checkAliases(scanned) {
|
|
566
|
+
const owners = new Map();
|
|
567
|
+
const problems = [];
|
|
568
|
+
for (const { module, aliases } of scanned)
|
|
569
|
+
for (const { value, file } of aliases) {
|
|
570
|
+
const existing = owners.get(value);
|
|
571
|
+
if (existing && existing.module !== module)
|
|
572
|
+
problems.push(
|
|
573
|
+
`${label(existing.module)} (${existing.file}) and ${label(module)} (${file}) both expose a service as "${value}".`,
|
|
574
|
+
);
|
|
575
|
+
else owners.set(value, { module, file });
|
|
576
|
+
}
|
|
577
|
+
return problems.length
|
|
578
|
+
? {
|
|
579
|
+
status: 'error',
|
|
580
|
+
title: 'Service aliases collide',
|
|
581
|
+
details: problems,
|
|
582
|
+
fix: 'Change one exposeAs, or drop it and use services.<plugin>.<name>.',
|
|
583
|
+
}
|
|
584
|
+
: { status: 'ok', title: 'Service names are unique' };
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Every `requires` of a service, action, interceptor or provider must name something the
|
|
588
|
+
* composition provides: a service ID, an `exposeAs` alias or a `provides` contract. Local
|
|
589
|
+
* plugins are fully known, so dotted names in their namespace are checked exactly. Installed
|
|
590
|
+
* plugin packages, host `services` and keys declared in other files can provide any other name,
|
|
591
|
+
* so those are only checked when none of them exist.
|
|
592
|
+
*/
|
|
593
|
+
async function checkRequirements(project, description, scanned) {
|
|
594
|
+
const root = project.root;
|
|
595
|
+
const services = new Set();
|
|
596
|
+
const providers = new Set();
|
|
597
|
+
const disabled = new Map();
|
|
598
|
+
let unresolved = false;
|
|
599
|
+
const idOf = (module, entry) => (module.isApp ? entry.path : `${module.id}.${entry.path}`);
|
|
600
|
+
for (const { module, aliases } of scanned) {
|
|
601
|
+
for (const entry of module.services) services.add(idOf(module, entry));
|
|
602
|
+
for (const entry of module.providers) providers.add(idOf(module, entry));
|
|
603
|
+
// The app module carries its configured ID as a prefix only when plugin.config.ts sets one.
|
|
604
|
+
if (module.isApp)
|
|
605
|
+
for (const entry of module.services) services.add(`${module.id}.${entry.path}`);
|
|
606
|
+
for (const { value } of aliases) services.add(value);
|
|
607
|
+
const provided = await providedKeys(root, module.services);
|
|
608
|
+
provided.names.forEach((name) => services.add(name));
|
|
609
|
+
unresolved ||= provided.unresolved;
|
|
610
|
+
}
|
|
611
|
+
for (const plugin of description.plugins.filter((item) => !item.enabled))
|
|
612
|
+
for (const entry of [...plugin.services, ...plugin.providers])
|
|
613
|
+
disabled.set(`${plugin.id}.${entry.path}`, plugin.id);
|
|
614
|
+
let hosted = false;
|
|
615
|
+
for (const file of rootLayouts(project))
|
|
616
|
+
hosted ||= /\bservices\s*=/.test(await readFile(path.join(root, file), 'utf8').catch(() => ''));
|
|
617
|
+
const namespaces = new Set([description.app.id, ...description.plugins.map((item) => item.id)]);
|
|
618
|
+
const open = description.packages.length > 0 || unresolved || hosted;
|
|
619
|
+
let count = 0;
|
|
620
|
+
const problems = [];
|
|
621
|
+
for (const { module } of scanned) {
|
|
622
|
+
const found = [
|
|
623
|
+
...(
|
|
624
|
+
await Promise.all([
|
|
625
|
+
literals(root, module.services, ['defineService'], 'requires'),
|
|
626
|
+
literals(root, module.actions, ['defineAction'], 'requires'),
|
|
627
|
+
literals(root, module.interceptors, ['defineInterceptor'], 'requires'),
|
|
628
|
+
])
|
|
629
|
+
)
|
|
630
|
+
.flat()
|
|
631
|
+
.map((item) => ({ ...item, provider: false })),
|
|
632
|
+
...(await exportedLiterals(root, module.providers, 'requires')).map((item) => ({
|
|
633
|
+
...item,
|
|
634
|
+
provider: true,
|
|
635
|
+
})),
|
|
636
|
+
];
|
|
637
|
+
for (const { value, file, provider } of found) {
|
|
638
|
+
count++;
|
|
639
|
+
if (services.has(value) || (provider && providers.has(value))) continue;
|
|
640
|
+
// Only dotted names can be IDs inside a local plugin; a flat name such as "auth" is a contract.
|
|
641
|
+
const [namespace] = value.split('.');
|
|
642
|
+
const local = value.includes('.') && namespaces.has(namespace);
|
|
643
|
+
if (!local && open) continue;
|
|
644
|
+
const owner = disabled.get(value);
|
|
645
|
+
const siblings = [...services, ...providers].filter(
|
|
646
|
+
(name) => name.split('.')[0] === namespace,
|
|
647
|
+
);
|
|
648
|
+
problems.push(
|
|
649
|
+
`${label(module)} requires "${value}" (${file}), but ${
|
|
650
|
+
owner
|
|
651
|
+
? `plugin "${owner}", which provides it, is disabled`
|
|
652
|
+
: `no ${provider ? 'provider, ' : ''}service, alias or contract has that name`
|
|
653
|
+
}.${local && siblings.length ? ` Available in "${namespace}": ${siblings.join(', ')}.` : ''}`,
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
return problems.length
|
|
658
|
+
? {
|
|
659
|
+
status: 'error',
|
|
660
|
+
title: 'Some required services are not provided',
|
|
661
|
+
details: problems,
|
|
662
|
+
fix: 'Correct the name, add the service, or install and enable the plugin that provides it.',
|
|
663
|
+
}
|
|
664
|
+
: {
|
|
665
|
+
status: 'ok',
|
|
666
|
+
title: !count
|
|
667
|
+
? 'No service requirements'
|
|
668
|
+
: count === 1
|
|
669
|
+
? 'The service requirement can be satisfied'
|
|
670
|
+
: `All ${count} service requirements can be satisfied`,
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
/** Plugin `dependencies` must not form a cycle, and the plugins they name must be enabled. */
|
|
674
|
+
function checkGraph(description) {
|
|
675
|
+
const dependenciesOf = (plugin) =>
|
|
676
|
+
Array.isArray(plugin.dependencies) ? plugin.dependencies : [];
|
|
677
|
+
const enabled = new Map(
|
|
678
|
+
description.plugins.filter((plugin) => plugin.enabled).map((plugin) => [plugin.id, plugin]),
|
|
679
|
+
);
|
|
680
|
+
const cycles = [];
|
|
681
|
+
const state = new Map();
|
|
682
|
+
const visit = (id, trail) => {
|
|
683
|
+
if (state.get(id) === 'done') return;
|
|
684
|
+
if (state.get(id) === 'visiting') {
|
|
685
|
+
cycles.push([...trail.slice(trail.indexOf(id)), id]);
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
state.set(id, 'visiting');
|
|
689
|
+
for (const dependency of dependenciesOf(enabled.get(id)))
|
|
690
|
+
if (enabled.has(dependency)) visit(dependency, [...trail, id]);
|
|
691
|
+
state.set(id, 'done');
|
|
692
|
+
};
|
|
693
|
+
for (const id of enabled.keys()) visit(id, []);
|
|
694
|
+
const off = new Set(
|
|
695
|
+
description.plugins.filter((plugin) => !plugin.enabled).map((plugin) => plugin.id),
|
|
696
|
+
);
|
|
697
|
+
const disabled = [...enabled.values()].flatMap((plugin) =>
|
|
698
|
+
dependenciesOf(plugin)
|
|
699
|
+
.filter((dependency) => off.has(dependency))
|
|
700
|
+
.map(
|
|
701
|
+
(dependency) =>
|
|
702
|
+
`plugin "${plugin.id}" depends on "${dependency}", which is disabled, so "${plugin.id}" fails to start.`,
|
|
703
|
+
),
|
|
704
|
+
);
|
|
705
|
+
const results = [];
|
|
706
|
+
if (cycles.length)
|
|
707
|
+
results.push({
|
|
708
|
+
status: 'error',
|
|
709
|
+
title: 'Plugin dependencies form a cycle',
|
|
710
|
+
details: cycles.map((cycle) => cycle.join(' → ')),
|
|
711
|
+
fix: 'Break the cycle with an event, or with a shared service that both plugins can require.',
|
|
712
|
+
});
|
|
713
|
+
if (disabled.length)
|
|
714
|
+
results.push({
|
|
715
|
+
status: 'warn',
|
|
716
|
+
title: 'Enabled plugins depend on disabled plugins',
|
|
717
|
+
details: disabled,
|
|
718
|
+
fix: 'Enable the plugin in plugins.overrides of bitakit.config.ts, or remove it from the dependencies.',
|
|
719
|
+
});
|
|
720
|
+
return results.length ? results : [{ status: 'ok', title: 'Plugin dependencies are consistent' }];
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Package styles use Tailwind (`@source`, `@apply`, imports under the "style" condition), so the
|
|
724
|
+
* root stylesheet must be compiled by the framework's Tailwind integration. A replaced
|
|
725
|
+
* next.config that lost create-next-app's loader rule leaves the app unstyled without an error.
|
|
726
|
+
*/
|
|
727
|
+
async function checkTailwind(project, description) {
|
|
728
|
+
if (!description.styles.length || !['next', 'tanstack'].includes(project.framework)) return [];
|
|
729
|
+
const read = async (names) =>
|
|
730
|
+
(
|
|
731
|
+
await Promise.all(
|
|
732
|
+
names.map((name) => readFile(path.join(project.root, name), 'utf8').catch(() => '')),
|
|
733
|
+
)
|
|
734
|
+
).join('\n');
|
|
735
|
+
if (project.framework === 'next') {
|
|
736
|
+
const config = await read(['next.config.ts', 'next.config.mjs', 'next.config.js']);
|
|
737
|
+
const postcss = await read([
|
|
738
|
+
'postcss.config.mjs',
|
|
739
|
+
'postcss.config.js',
|
|
740
|
+
'postcss.config.cjs',
|
|
741
|
+
'postcss.config.ts',
|
|
742
|
+
]);
|
|
743
|
+
if (/@tailwindcss\/turbopack/.test(config) || /@tailwindcss\/postcss/.test(postcss))
|
|
744
|
+
return [{ status: 'ok', title: 'Tailwind compiles the app styles' }];
|
|
745
|
+
return [
|
|
746
|
+
{
|
|
747
|
+
status: 'error',
|
|
748
|
+
title: 'Tailwind does not compile the app styles',
|
|
749
|
+
fix: "Keep create-next-app's loader rule in next.config, inside withBitaKit({ ... }):\n\nturbopack: { rules: { '*.css': { loaders: ['@tailwindcss/turbopack'], as: '*.css' } } }\n\nor add postcss.config.mjs with the @tailwindcss/postcss plugin.",
|
|
750
|
+
},
|
|
751
|
+
];
|
|
752
|
+
}
|
|
753
|
+
const vite = await read(['vite.config.ts', 'vite.config.mts', 'vite.config.js']);
|
|
754
|
+
return /@tailwindcss\/vite/.test(vite)
|
|
755
|
+
? [{ status: 'ok', title: 'Tailwind compiles the app styles' }]
|
|
756
|
+
: [
|
|
757
|
+
{
|
|
758
|
+
status: 'error',
|
|
759
|
+
title: 'Tailwind does not compile the app styles',
|
|
760
|
+
fix: "Add tailwindcss() from '@tailwindcss/vite' to the Vite plugins.",
|
|
761
|
+
},
|
|
762
|
+
];
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
async function checkStyles(project, description) {
|
|
766
|
+
if (!description.styles.length) return { status: 'ok', title: 'No package styles to import' };
|
|
767
|
+
const visited = new Set();
|
|
768
|
+
const generated = path.resolve(project.root, description.appDirectory, 'bitakit.gen.css');
|
|
769
|
+
const resolveLocal = (specifier, from) =>
|
|
770
|
+
specifier.startsWith('@/')
|
|
771
|
+
? path.resolve(project.root, 'src', specifier.slice(2))
|
|
772
|
+
: specifier.startsWith('.')
|
|
773
|
+
? path.resolve(path.dirname(from), specifier)
|
|
774
|
+
: undefined;
|
|
775
|
+
async function followsGenerated(file) {
|
|
776
|
+
if (file === generated) return true;
|
|
777
|
+
if (!file || visited.has(file)) return false;
|
|
778
|
+
visited.add(file);
|
|
779
|
+
const css = await readFile(file, 'utf8').catch(() => undefined);
|
|
780
|
+
if (css === undefined) return false;
|
|
781
|
+
for (const { value } of cssImports(css, file))
|
|
782
|
+
if (await followsGenerated(resolveLocal(value, file))) return true;
|
|
783
|
+
return false;
|
|
784
|
+
}
|
|
785
|
+
try {
|
|
786
|
+
for (const layout of rootLayouts(project)) {
|
|
787
|
+
const file = path.join(project.root, layout);
|
|
788
|
+
const source = await readFile(file, 'utf8').catch(() => '');
|
|
789
|
+
const tree = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true);
|
|
790
|
+
for (const node of tree.statements) {
|
|
791
|
+
if (!ts.isImportDeclaration(node) || !ts.isStringLiteral(node.moduleSpecifier)) continue;
|
|
792
|
+
const value = node.moduleSpecifier.text.replace(/\?url$/, '');
|
|
793
|
+
if (!value.endsWith('.css')) continue;
|
|
794
|
+
const entry = resolveLocal(value, file);
|
|
795
|
+
if (await followsGenerated(entry))
|
|
796
|
+
return {
|
|
797
|
+
status: 'ok',
|
|
798
|
+
title: `Package styles are imported in ${path.relative(project.root, entry)}`,
|
|
799
|
+
details: [
|
|
800
|
+
'Static root import graph verified. Rendered cascade, spacing and contrast require browser verification.',
|
|
801
|
+
],
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
} catch (error) {
|
|
806
|
+
return {
|
|
807
|
+
status: 'error',
|
|
808
|
+
title: 'The root stylesheet could not be parsed',
|
|
809
|
+
details: [error.message],
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
const target = project.framework === 'tanstack' ? 'src/styles.css' : 'src/app/globals.css';
|
|
813
|
+
const relative = path.relative(
|
|
814
|
+
path.dirname(path.join(project.root, target)),
|
|
815
|
+
path.join(
|
|
816
|
+
description.appDirectory === '.'
|
|
817
|
+
? project.root
|
|
818
|
+
: path.join(project.root, description.appDirectory),
|
|
819
|
+
'bitakit.gen.css',
|
|
820
|
+
),
|
|
821
|
+
);
|
|
822
|
+
return {
|
|
823
|
+
status: 'warn',
|
|
824
|
+
title: 'Installed packages ship styles that are not imported',
|
|
825
|
+
details: description.styles,
|
|
826
|
+
fix: `Add this line after @import 'tailwindcss'; in ${target}:\n\n@import '${relative.split(path.sep).join('/')}';`,
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
/** Discovery warnings are either deprecations that migrate fixes, or files it skips. */
|
|
830
|
+
const isDeprecation = (warning) => warning.includes('bitakit migrate');
|
|
831
|
+
const plural = (count, noun) => `${count} ${noun}${count === 1 ? '' : 's'}`;
|
|
832
|
+
function checkIgnored(description) {
|
|
833
|
+
const ignored = description.warnings.filter((warning) => !isDeprecation(warning));
|
|
834
|
+
if (!ignored.length)
|
|
835
|
+
return { status: 'ok', title: 'Every contribution file has a default export' };
|
|
836
|
+
return {
|
|
837
|
+
status: 'warn',
|
|
838
|
+
title: `${plural(ignored.length, 'file')} in contribution folders ${ignored.length === 1 ? 'is' : 'are'} ignored by discovery`,
|
|
839
|
+
details: ignored,
|
|
840
|
+
fix: 'Add a default export to each file, or prefix helper files with "-" so discovery skips them on purpose.',
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
/** A slot file nobody renders is a silent no-op; layouts declare the slots they render. */
|
|
844
|
+
function checkSlots(description) {
|
|
845
|
+
const declared = new Set(description.slots ?? []);
|
|
846
|
+
const contracts = new Map();
|
|
847
|
+
const duplicates = [];
|
|
848
|
+
for (const entry of description.slotDeclarations ?? []) {
|
|
849
|
+
if (contracts.has(entry.id))
|
|
850
|
+
duplicates.push(
|
|
851
|
+
`Duplicate slot "${entry.id}": ${contracts.get(entry.id).file} and ${entry.file}.`,
|
|
852
|
+
);
|
|
853
|
+
else contracts.set(entry.id, entry);
|
|
854
|
+
declared.add(entry.id);
|
|
855
|
+
}
|
|
856
|
+
if (duplicates.length)
|
|
857
|
+
return {
|
|
858
|
+
status: 'error',
|
|
859
|
+
title: 'Duplicate slot declarations',
|
|
860
|
+
details: duplicates,
|
|
861
|
+
fix: 'Keep one owning definition per canonical slot ID; flat and nested forms are alternatives.',
|
|
862
|
+
};
|
|
863
|
+
const problems = [];
|
|
864
|
+
for (const module of [
|
|
865
|
+
description.app,
|
|
866
|
+
...description.plugins.filter((item) => item.enabled !== false),
|
|
867
|
+
])
|
|
868
|
+
for (const slot of module.slots ?? [])
|
|
869
|
+
if (!declared.has(slot.path))
|
|
870
|
+
problems.push(
|
|
871
|
+
`${slot.file} targets slot "${slot.path}", which no installed layout declares.`,
|
|
872
|
+
);
|
|
873
|
+
else if (
|
|
874
|
+
contracts.get(slot.path)?.accepts &&
|
|
875
|
+
contracts.get(slot.path).accepts !== 'component'
|
|
876
|
+
)
|
|
877
|
+
problems.push(
|
|
878
|
+
`${slot.file} contributes a component to "${slot.path}", whose contract accepts ${contracts.get(slot.path).accepts}.`,
|
|
879
|
+
);
|
|
880
|
+
if (description.unresolvedSlots?.length)
|
|
881
|
+
problems.push(
|
|
882
|
+
`Static inspection cannot resolve slot definitions in: ${description.unresolvedSlots.join(', ')}. Runtime normalization and typechecking remain authoritative.`,
|
|
883
|
+
);
|
|
884
|
+
if (!problems.length)
|
|
885
|
+
return {
|
|
886
|
+
status: 'ok',
|
|
887
|
+
// A clean run stays short; `bitakit info` lists the slots.
|
|
888
|
+
title: declared.size
|
|
889
|
+
? `Slot files target declared slots (${plural(declared.size, 'slot')} available)`
|
|
890
|
+
: 'No layout slots are declared',
|
|
891
|
+
};
|
|
892
|
+
return {
|
|
893
|
+
status: 'warn',
|
|
894
|
+
title: `${plural(problems.length, 'slot contract issue')} need review`,
|
|
895
|
+
details: problems,
|
|
896
|
+
fix: 'Declare the owner contract with defineSlot in slots/. ui/slots/ contributes components; Action slots must reference declared action contracts. Legacy bitakit.config.ts slots are name hints only.',
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
function checkDeprecated(description) {
|
|
900
|
+
const deprecated = description.warnings.filter(isDeprecation);
|
|
901
|
+
if (!deprecated.length) return { status: 'ok', title: 'Folder layout is current' };
|
|
902
|
+
return {
|
|
903
|
+
status: 'warn',
|
|
904
|
+
title: `${plural(deprecated.length, 'file')} use${deprecated.length === 1 ? 's' : ''} a deprecated layout`,
|
|
905
|
+
details: deprecated,
|
|
906
|
+
fix: 'Run "npx bitakit migrate" to rename and move them.',
|
|
907
|
+
};
|
|
908
|
+
}
|