@universalcms/connect 0.0.0-stage → 0.1.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/README.md +122 -2
- package/dist/cli.mjs +149 -0
- package/dist/core.mjs +224 -0
- package/dist/setup-environment.mjs +237 -0
- package/dist/templates.mjs +84 -0
- package/dist/vendor/analyze-project.mjs +82 -0
- package/dist/vendor/check-delivery.mjs +158 -0
- package/dist/vendor/dependency-diagnostics.mjs +72 -0
- package/dist/vendor/inspect-installed-dependencies.mjs +125 -0
- package/dist/vendor/project-analysis.mjs +446 -0
- package/dist/vite-handler.mjs +54 -0
- package/dist/wizard.mjs +99 -0
- package/package.json +23 -3
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** Sanitized dependency advice. Browser-safe: no filesystem, modules or network. */
|
|
2
|
+
export const CMS_DEPENDENCY_NAMES = Object.freeze({
|
|
3
|
+
sdk: '@universalcms/sdk', react: '@universalcms/react', next: '@universalcms/next',
|
|
4
|
+
});
|
|
5
|
+
const packageKeys = Object.keys(CMS_DEPENDENCY_NAMES);
|
|
6
|
+
const declarations = ['runtime', 'development', 'missing', 'unknown'];
|
|
7
|
+
const installations = ['present', 'missing', 'unknown'];
|
|
8
|
+
const compatibility = ['compatible', 'incompatible', 'unknown'];
|
|
9
|
+
|
|
10
|
+
function values(value, keys) {
|
|
11
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
12
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
13
|
+
if (Object.keys(descriptors).length !== keys.length || keys.some(key => !descriptors[key] || !('value' in descriptors[key]))) return null;
|
|
14
|
+
return Object.fromEntries(keys.map(key => [key, descriptors[key].value]));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Reject arbitrary labels, paths, package versions, credentials and getters. */
|
|
18
|
+
export function validateDependencyDiagnostics(value) {
|
|
19
|
+
try {
|
|
20
|
+
const diagnostic = values(value, ['generatedAt', 'packages', 'node']);
|
|
21
|
+
if (!diagnostic || typeof diagnostic.generatedAt !== 'string' ||
|
|
22
|
+
!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/.test(diagnostic.generatedAt) ||
|
|
23
|
+
new Date(diagnostic.generatedAt).toISOString() !== diagnostic.generatedAt ||
|
|
24
|
+
!compatibility.includes(diagnostic.node)) return null;
|
|
25
|
+
const candidates = values(diagnostic.packages, packageKeys);
|
|
26
|
+
if (!candidates) return null;
|
|
27
|
+
const packages = {};
|
|
28
|
+
for (const key of packageKeys) {
|
|
29
|
+
const candidate = values(candidates[key], ['declared', 'installed', 'compatibility']);
|
|
30
|
+
if (!candidate || !declarations.includes(candidate.declared) || !installations.includes(candidate.installed) ||
|
|
31
|
+
!compatibility.includes(candidate.compatibility) ||
|
|
32
|
+
(candidate.installed !== 'present' && candidate.compatibility !== 'unknown')) return null;
|
|
33
|
+
packages[key] = { declared: candidate.declared, installed: candidate.installed, compatibility: candidate.compatibility };
|
|
34
|
+
}
|
|
35
|
+
if (packages.sdk.compatibility === 'compatible' && diagnostic.node !== 'compatible') return null;
|
|
36
|
+
if (packages.next.compatibility === 'compatible' && (diagnostic.node !== 'compatible' || packages.sdk.compatibility !== 'compatible')) return null;
|
|
37
|
+
// The React renderer may run in a browser even when the inspection process
|
|
38
|
+
// cannot run the credentialed Node client. Its package dependency must exist.
|
|
39
|
+
if (packages.react.compatibility === 'compatible' && packages.sdk.installed !== 'present') return null;
|
|
40
|
+
return { generatedAt: diagnostic.generatedAt, packages, node: diagnostic.node };
|
|
41
|
+
} catch { return null; }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A manifest establishes declarations only, never installation or runtime health. */
|
|
45
|
+
export function createManifestDependencyDiagnostics(packageJsonText, now = new Date()) {
|
|
46
|
+
let manifest = null;
|
|
47
|
+
try {
|
|
48
|
+
if (typeof packageJsonText === 'string' && new TextEncoder().encode(packageJsonText).byteLength <= 128 * 1024) {
|
|
49
|
+
const parsed = JSON.parse(packageJsonText);
|
|
50
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) manifest = parsed;
|
|
51
|
+
}
|
|
52
|
+
} catch { /* Keep declaration confidence unknown, without reflecting source. */ }
|
|
53
|
+
const sections = ['dependencies', 'devDependencies', 'optionalDependencies'];
|
|
54
|
+
const own = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
|
|
55
|
+
const invalidSections = manifest && sections.some(key => own(manifest, key) && (!manifest[key] || typeof manifest[key] !== 'object' || Array.isArray(manifest[key])));
|
|
56
|
+
const declaration = name => {
|
|
57
|
+
if (!manifest || invalidSections) return 'unknown';
|
|
58
|
+
for (const section of sections) {
|
|
59
|
+
if (manifest[section] && own(manifest[section], name) && (typeof manifest[section][name] !== 'string' || !manifest[section][name].trim())) return 'unknown';
|
|
60
|
+
}
|
|
61
|
+
if (['dependencies', 'optionalDependencies'].some(key => manifest[key] && own(manifest[key], name))) return 'runtime';
|
|
62
|
+
if (manifest.devDependencies && own(manifest.devDependencies, name)) return 'development';
|
|
63
|
+
return 'missing';
|
|
64
|
+
};
|
|
65
|
+
const packages = {};
|
|
66
|
+
for (const [key, name] of Object.entries(CMS_DEPENDENCY_NAMES)) {
|
|
67
|
+
const declared = declaration(name);
|
|
68
|
+
packages[key] = { declared, installed: 'unknown', compatibility: 'unknown' };
|
|
69
|
+
}
|
|
70
|
+
const generatedAt = new Date(now).toISOString();
|
|
71
|
+
return { generatedAt, packages, node: 'unknown' };
|
|
72
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/** Bounded local metadata inspection. Never executes website modules or scripts. */
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import { lstat, open, realpath } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { createManifestDependencyDiagnostics, CMS_DEPENDENCY_NAMES } from './dependency-diagnostics.mjs';
|
|
6
|
+
|
|
7
|
+
const MAX_METADATA_BYTES = 128 * 1024;
|
|
8
|
+
const packages = [
|
|
9
|
+
{ key: 'sdk', name: CMS_DEPENDENCY_NAMES.sdk, entry: 'dist/index.js', exportCondition: 'default' },
|
|
10
|
+
{ key: 'react', name: CMS_DEPENDENCY_NAMES.react, entry: 'dist/index.js', exportCondition: 'import' },
|
|
11
|
+
{ key: 'next', name: CMS_DEPENDENCY_NAMES.next, entry: 'dist/index.js', exportCondition: 'import' },
|
|
12
|
+
{ key: 'reactPeer', name: 'react', entry: 'index.js' },
|
|
13
|
+
{ key: 'nextPeer', name: 'next', entry: 'dist/server/next.js' },
|
|
14
|
+
];
|
|
15
|
+
const inside = (root, target) => {
|
|
16
|
+
const relative = path.relative(root, target);
|
|
17
|
+
return relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
18
|
+
};
|
|
19
|
+
const plain = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
20
|
+
|
|
21
|
+
async function readMetadata(filename, directory) {
|
|
22
|
+
let handle;
|
|
23
|
+
try {
|
|
24
|
+
const before = await lstat(filename);
|
|
25
|
+
if (!before.isFile() || before.isSymbolicLink() || before.size > MAX_METADATA_BYTES || await realpath(filename) !== filename || !inside(directory, filename)) return null;
|
|
26
|
+
handle = await open(filename, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
27
|
+
const opened = await handle.stat(), current = await lstat(filename);
|
|
28
|
+
if (!opened.isFile() || current.isSymbolicLink() || opened.ino !== current.ino || opened.dev !== current.dev ||
|
|
29
|
+
opened.size > MAX_METADATA_BYTES || await realpath(filename) !== filename) return null;
|
|
30
|
+
const buffer = Buffer.alloc(MAX_METADATA_BYTES + 1);
|
|
31
|
+
let bytes = 0;
|
|
32
|
+
while (bytes < buffer.length) {
|
|
33
|
+
const part = await handle.read(buffer, bytes, buffer.length - bytes, bytes);
|
|
34
|
+
if (!part.bytesRead) break;
|
|
35
|
+
bytes += part.bytesRead;
|
|
36
|
+
}
|
|
37
|
+
if (bytes > MAX_METADATA_BYTES) return null;
|
|
38
|
+
const parsed = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(buffer.subarray(0, bytes)));
|
|
39
|
+
return plain(parsed) ? parsed : null;
|
|
40
|
+
} catch { return null; }
|
|
41
|
+
finally { await handle?.close(); }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function numericVersion(value) {
|
|
45
|
+
if (typeof value !== 'string') return null;
|
|
46
|
+
const match = /^(?:v)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.exec(value);
|
|
47
|
+
if (!match) return null;
|
|
48
|
+
const numbers = match.slice(1).map(Number);
|
|
49
|
+
return numbers.every(Number.isSafeInteger) ? numbers : null;
|
|
50
|
+
}
|
|
51
|
+
function nodeCompatibility(value) {
|
|
52
|
+
const version = numericVersion(value);
|
|
53
|
+
return !version ? 'unknown' : version[0] > 20 || (version[0] === 20 && version[1] >= 19) ? 'compatible' : 'incompatible';
|
|
54
|
+
}
|
|
55
|
+
function peerCompatibility(value, major, minimumMinor = 0) {
|
|
56
|
+
const version = numericVersion(value);
|
|
57
|
+
return !version ? 'unknown' : version[0] === major && version[1] >= minimumMinor ? 'compatible' : 'incompatible';
|
|
58
|
+
}
|
|
59
|
+
function allCompatible(states) {
|
|
60
|
+
return states.includes('incompatible') ? 'incompatible' : states.every(value => value === 'compatible') ? 'compatible' : 'unknown';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function inspectPackage(modules, descriptor) {
|
|
64
|
+
const directory = path.join(modules, ...descriptor.name.split('/'));
|
|
65
|
+
let info;
|
|
66
|
+
// Validate a scoped package's parent before even probing its child. A scope
|
|
67
|
+
// link outside node_modules must not leak whether that external child exists.
|
|
68
|
+
if (descriptor.name.startsWith('@')) {
|
|
69
|
+
try {
|
|
70
|
+
const scope = await realpath(path.dirname(directory));
|
|
71
|
+
if (!inside(modules, scope) || !(await lstat(scope)).isDirectory()) return { installed: 'unknown', version: null };
|
|
72
|
+
} catch (error) { return { installed: error.code === 'ENOENT' || error.code === 'ENOTDIR' ? 'missing' : 'unknown', version: null }; }
|
|
73
|
+
}
|
|
74
|
+
try { info = await lstat(directory); }
|
|
75
|
+
catch (error) { return { installed: error.code === 'ENOENT' || error.code === 'ENOTDIR' ? 'missing' : 'unknown', version: null }; }
|
|
76
|
+
try {
|
|
77
|
+
if (!info.isDirectory() && !info.isSymbolicLink()) return { installed: 'unknown', version: null };
|
|
78
|
+
// pnpm stores package directories behind symlinks inside node_modules/.pnpm.
|
|
79
|
+
// External links and shared workspaces are outside this bounded inspection.
|
|
80
|
+
const actual = await realpath(directory);
|
|
81
|
+
if (!inside(modules, actual) || actual === modules || !(await lstat(actual)).isDirectory()) return { installed: 'unknown', version: null };
|
|
82
|
+
const metadata = await readMetadata(path.join(actual, 'package.json'), actual);
|
|
83
|
+
if (!metadata || metadata.name !== descriptor.name || typeof metadata.version !== 'string') return { installed: 'unknown', version: null };
|
|
84
|
+
const entry = path.join(actual, descriptor.entry), entryInfo = await lstat(entry);
|
|
85
|
+
if (!entryInfo.isFile() || entryInfo.isSymbolicLink() || await realpath(entry) !== entry || !inside(actual, entry)) return { installed: 'unknown', version: null };
|
|
86
|
+
if (descriptor.exportCondition) {
|
|
87
|
+
if (!plain(metadata.exports) || !plain(metadata.exports['.']) || metadata.exports['.'][descriptor.exportCondition] !== `./${descriptor.entry}`) return { installed: 'unknown', version: null };
|
|
88
|
+
} else if (descriptor.name === 'next' && ![`./${descriptor.entry}`, descriptor.entry].includes(metadata.main)) return { installed: 'unknown', version: null };
|
|
89
|
+
return { installed: 'present', version: metadata.version };
|
|
90
|
+
} catch { return { installed: 'unknown', version: null }; }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Inspect one selected project; public output never contains versions or paths. */
|
|
94
|
+
export async function inspectInstalledDependencies(directory, packageJsonText, { now = new Date(), nodeVersion = process.version } = {}) {
|
|
95
|
+
const diagnostics = createManifestDependencyDiagnostics(packageJsonText, now);
|
|
96
|
+
diagnostics.node = nodeCompatibility(nodeVersion);
|
|
97
|
+
let root;
|
|
98
|
+
try {
|
|
99
|
+
const requested = path.resolve(directory), rootInfo = await lstat(requested);
|
|
100
|
+
if (!rootInfo.isDirectory() || rootInfo.isSymbolicLink()) return diagnostics;
|
|
101
|
+
root = await realpath(requested);
|
|
102
|
+
} catch { return diagnostics; }
|
|
103
|
+
const modules = path.join(root, 'node_modules');
|
|
104
|
+
let modulesInfo;
|
|
105
|
+
try { modulesInfo = await lstat(modules); }
|
|
106
|
+
catch (error) {
|
|
107
|
+
if (error.code === 'ENOENT' || error.code === 'ENOTDIR') {
|
|
108
|
+
for (const candidate of Object.values(diagnostics.packages)) candidate.installed = 'missing';
|
|
109
|
+
}
|
|
110
|
+
return diagnostics;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
if (!modulesInfo.isDirectory() || modulesInfo.isSymbolicLink() || await realpath(modules) !== modules) return diagnostics;
|
|
114
|
+
} catch { return diagnostics; }
|
|
115
|
+
const observed = {};
|
|
116
|
+
for (const descriptor of packages) observed[descriptor.key] = await inspectPackage(modules, descriptor);
|
|
117
|
+
for (const key of Object.keys(CMS_DEPENDENCY_NAMES)) diagnostics.packages[key].installed = observed[key].installed;
|
|
118
|
+
const release = key => observed[key].installed === 'present' && observed[key].version === '0.1.0' ? 'compatible' : 'unknown';
|
|
119
|
+
const reactPeer = observed.reactPeer.installed === 'present' ? peerCompatibility(observed.reactPeer.version, 19, 2) : 'unknown';
|
|
120
|
+
const nextPeer = observed.nextPeer.installed === 'present' ? peerCompatibility(observed.nextPeer.version, 16) : 'unknown';
|
|
121
|
+
if (observed.sdk.installed === 'present') diagnostics.packages.sdk.compatibility = allCompatible([release('sdk'), diagnostics.node]);
|
|
122
|
+
if (observed.react.installed === 'present') diagnostics.packages.react.compatibility = allCompatible([release('react'), release('sdk'), reactPeer]);
|
|
123
|
+
if (observed.next.installed === 'present') diagnostics.packages.next.compatibility = allCompatible([release('next'), release('sdk'), diagnostics.node, reactPeer, nextPeer]);
|
|
124
|
+
return diagnostics;
|
|
125
|
+
}
|
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import { validateDependencyDiagnostics } from './dependency-diagnostics.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Local project evidence only. This module runs in browsers and Node without
|
|
5
|
+
* executing a project's scripts, importing its modules, or contacting a URL.
|
|
6
|
+
* Reports are integration advice, never authorization or connection proof.
|
|
7
|
+
*/
|
|
8
|
+
export const MAX_ANALYSIS_FILE_BYTES = 128 * 1024;
|
|
9
|
+
export const MAX_ANALYSIS_TOTAL_BYTES = 1024 * 1024;
|
|
10
|
+
const configNames = (name) => ['js', 'mjs', 'cjs', 'ts'].map(extension => `${name}.config.${extension}`);
|
|
11
|
+
const nodeEntries = ['server.js', 'server.mjs', 'server.cjs', 'server.ts', 'app.js', 'app.mjs', 'index.js', 'index.mjs', 'server/index.js', 'server/index.mjs', 'server/index.cjs', 'server/index.ts', 'src/server.js', 'src/server.mjs', 'src/server.ts'];
|
|
12
|
+
export const PROJECT_ANALYSIS_FILES = Object.freeze([
|
|
13
|
+
'package.json', 'composer.json', 'pnpm-workspace.yaml', 'angular.json',
|
|
14
|
+
'requirements.txt', 'pyproject.toml', 'Gemfile', 'pom.xml', 'build.gradle', 'build.gradle.kts',
|
|
15
|
+
...configNames('next'), ...configNames('nuxt'), ...configNames('astro'), ...configNames('svelte'), ...configNames('vite'),
|
|
16
|
+
...nodeEntries,
|
|
17
|
+
]);
|
|
18
|
+
export const PROJECT_ANALYSIS_SIGNALS = Object.freeze({
|
|
19
|
+
react: 'React dependency', vue: 'Vue dependency', angular: 'Angular dependency',
|
|
20
|
+
next: 'Next.js dependency and framework command', nuxt: 'Nuxt dependency and framework command',
|
|
21
|
+
astro: 'Astro dependency and framework command', svelte: 'SvelteKit dependency and framework command',
|
|
22
|
+
laravel: 'Laravel production Composer dependency', wordpress: 'WordPress production Composer dependency',
|
|
23
|
+
php: 'PHP runtime Composer requirement', python: 'Python web framework dependency',
|
|
24
|
+
ruby: 'Ruby on Rails dependency', java: 'Java Spring Boot dependency',
|
|
25
|
+
node: 'Node server entry, server imports and startup command',
|
|
26
|
+
nextStatic: 'Literal Next.js static export configuration', astroStatic: 'Literal Astro static output configuration',
|
|
27
|
+
astroServer: 'Literal Astro server output with Node adapter',
|
|
28
|
+
nuxtStatic: 'Nuxt generate command without a server startup command',
|
|
29
|
+
svelteStatic: 'SvelteKit static adapter configuration', svelteNode: 'SvelteKit Node adapter configuration',
|
|
30
|
+
monorepo: 'Multiple project workspace declaration', dynamic: 'Configuration needs developer inspection',
|
|
31
|
+
});
|
|
32
|
+
export const PROJECT_ANALYSIS_WARNINGS = Object.freeze({
|
|
33
|
+
unknown: 'A backend or build-time content integration could not be established. Ask the website developer to complete setup.',
|
|
34
|
+
monorepo: 'This folder contains a workspace. Analyze the individual website project folder instead.',
|
|
35
|
+
conflict: 'Conflicting framework or backend evidence needs developer review.',
|
|
36
|
+
invalid: 'A selected project file is invalid or exceeds the analysis limits. Ask the developer to check the project files.',
|
|
37
|
+
dynamic: 'A configuration uses values that cannot be resolved safely without running code. Ask the developer to confirm the deployment runtime.',
|
|
38
|
+
frontend: 'A frontend dependency does not identify its backend. Node.js build tools alone are not a Node.js website server.',
|
|
39
|
+
unverified: 'This identifies project configuration only. Credentials, deployment and CMS connectivity still need verification.',
|
|
40
|
+
});
|
|
41
|
+
const S = PROJECT_ANALYSIS_SIGNALS, W = PROJECT_ANALYSIS_WARNINGS;
|
|
42
|
+
const own = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
|
|
43
|
+
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
44
|
+
const byteLength = value => new TextEncoder().encode(value).byteLength;
|
|
45
|
+
const exactKeys = (value, keys) => object(value) && Object.keys(value).length === keys.length && Object.keys(value).every(key => keys.includes(key));
|
|
46
|
+
const signalNames = new Set(Object.values(S));
|
|
47
|
+
const warningNames = new Set(Object.values(W));
|
|
48
|
+
const allowedFiles = new Set(PROJECT_ANALYSIS_FILES);
|
|
49
|
+
|
|
50
|
+
function skipQuoted(source, begin, quote) {
|
|
51
|
+
let index = begin + 1;
|
|
52
|
+
while (index < source.length) {
|
|
53
|
+
if (source[index] === '\\') { index += 2; continue; }
|
|
54
|
+
if (source[index++] === quote) break;
|
|
55
|
+
}
|
|
56
|
+
return index;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function skipTemplate(source, begin, nesting = 0) {
|
|
60
|
+
if (nesting > 24) return source.length;
|
|
61
|
+
let index = begin + 1;
|
|
62
|
+
while (index < source.length) {
|
|
63
|
+
if (source[index] === '\\') { index += 2; continue; }
|
|
64
|
+
if (source[index] === '`') return index + 1;
|
|
65
|
+
if (source[index] !== '$' || source[index + 1] !== '{') { index++; continue; }
|
|
66
|
+
index += 2;
|
|
67
|
+
let depth = 1;
|
|
68
|
+
while (index < source.length && depth) {
|
|
69
|
+
const char = source[index];
|
|
70
|
+
if (char === '"' || char === "'") { index = skipQuoted(source, index, char); continue; }
|
|
71
|
+
if (char === '`') { index = skipTemplate(source, index, nesting + 1); continue; }
|
|
72
|
+
if (char === '/' && source[index + 1] === '/') { while (index < source.length && source[index] !== '\n') index++; continue; }
|
|
73
|
+
if (char === '/' && source[index + 1] === '*') { const end = source.indexOf('*/', index + 2); index = end < 0 ? source.length : end + 2; continue; }
|
|
74
|
+
if (char === '\\') { index += 2; continue; }
|
|
75
|
+
if (char === '{') depth++;
|
|
76
|
+
if (char === '}') depth--;
|
|
77
|
+
index++;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return index;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Small bounded lexical reader: comments and template bodies never count as
|
|
84
|
+
// configuration evidence. No code, regular-expression body or escape is run.
|
|
85
|
+
function tokens(source) {
|
|
86
|
+
const result = [];
|
|
87
|
+
for (let i = 0; i < source.length;) {
|
|
88
|
+
const c = source[i], next = source[i + 1];
|
|
89
|
+
if (/\s/.test(c)) { i++; continue; }
|
|
90
|
+
if (c === '/' && next === '/') { while (i < source.length && source[i] !== '\n') i++; continue; }
|
|
91
|
+
if (c === '/' && next === '*') { const end = source.indexOf('*/', i + 2); i = end < 0 ? source.length : end + 2; continue; }
|
|
92
|
+
if (c === '/' && (!result.length || ['=', '(', ':', ',', '[', '{', '!', '?', ';', '>', 'return', 'case', 'yield'].includes(result.at(-1)?.value))) {
|
|
93
|
+
// A regexp may contain quotes, comment-like text and fake source tokens.
|
|
94
|
+
let inClass = false;
|
|
95
|
+
for (i++; i < source.length; i++) {
|
|
96
|
+
if (source[i] === '\\') { i++; continue; }
|
|
97
|
+
if (source[i] === '[') inClass = true;
|
|
98
|
+
if (source[i] === ']') inClass = false;
|
|
99
|
+
if (source[i] === '/' && !inClass) { i++; while (/[a-z]/i.test(source[i] ?? '') && i < source.length) i++; break; }
|
|
100
|
+
if (source[i] === '\n' || source[i] === '\r') break;
|
|
101
|
+
}
|
|
102
|
+
result.push({ type: 'dynamic', value: '' }); continue;
|
|
103
|
+
}
|
|
104
|
+
if (c === '`') {
|
|
105
|
+
i = skipTemplate(source, i);
|
|
106
|
+
result.push({ type: 'dynamic', value: '' }); continue;
|
|
107
|
+
}
|
|
108
|
+
if (c === '"' || c === "'") {
|
|
109
|
+
let value = '', escaped = false, closed = false;
|
|
110
|
+
for (i++; i < source.length; i++) {
|
|
111
|
+
if (source[i] === '\\') { escaped = true; i++; continue; }
|
|
112
|
+
if (source[i] === c) { i++; closed = true; break; }
|
|
113
|
+
value += source[i];
|
|
114
|
+
}
|
|
115
|
+
result.push({ type: escaped || !closed ? 'dynamic' : 'string', value: escaped || !closed ? '' : value }); continue;
|
|
116
|
+
}
|
|
117
|
+
if (/[A-Za-z_$]/.test(c)) {
|
|
118
|
+
const start = i++;
|
|
119
|
+
while (i < source.length && /[A-Za-z0-9_$]/.test(source[i])) i++;
|
|
120
|
+
result.push({ type: 'name', value: source.slice(start, i) }); continue;
|
|
121
|
+
}
|
|
122
|
+
result.push({ type: 'punctuation', value: c }); i++;
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function imported(tokenList, moduleName) {
|
|
128
|
+
return tokenList.some((token, index) => token.type === 'string' && token.value === moduleName && (
|
|
129
|
+
tokenList[index - 1]?.value === 'from' ||
|
|
130
|
+
(tokenList[index - 1]?.value === '(' && ['require', 'import'].includes(tokenList[index - 2]?.value))
|
|
131
|
+
));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function defaultImport(tokenList, moduleName) {
|
|
135
|
+
const matches = [];
|
|
136
|
+
for (let index = 0; index < tokenList.length; index++) {
|
|
137
|
+
if (tokenList[index]?.value === 'import' && tokenList[index + 1]?.type === 'name' && tokenList[index + 2]?.value === 'from' && tokenList[index + 3]?.type === 'string' && tokenList[index + 3]?.value === moduleName) matches.push(tokenList[index + 1].value);
|
|
138
|
+
}
|
|
139
|
+
return matches.length === 1 ? matches[0] : null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function readObject(list, begin, depth = 0) {
|
|
143
|
+
const properties = Object.create(null);
|
|
144
|
+
if (depth > 24) return { properties, opaque: true, end: begin };
|
|
145
|
+
let index = begin + 1, opaque = false;
|
|
146
|
+
while (index < list.length && list[index]?.value !== '}') {
|
|
147
|
+
const key = list[index];
|
|
148
|
+
if (!['string', 'name'].includes(key?.type) || list[index + 1]?.value !== ':' || key.value === '__proto__' || own(properties, key.value)) return { properties, opaque: true, end: index };
|
|
149
|
+
index += 2;
|
|
150
|
+
const start = index;
|
|
151
|
+
const stack = [];
|
|
152
|
+
while (index < list.length) {
|
|
153
|
+
const value = list[index].value;
|
|
154
|
+
if (!stack.length && [',', '}'].includes(value)) break;
|
|
155
|
+
if (['{', '[', '('].includes(value)) stack.push(value);
|
|
156
|
+
if (['}', ']', ')'].includes(value)) {
|
|
157
|
+
const expected = { '}': '{', ']': '[', ')': '(' }[value];
|
|
158
|
+
if (stack.pop() !== expected) return { properties, opaque: true, end: index };
|
|
159
|
+
}
|
|
160
|
+
index++;
|
|
161
|
+
}
|
|
162
|
+
const expression = list.slice(start, index);
|
|
163
|
+
if (!expression.length) opaque = true;
|
|
164
|
+
let nested = null;
|
|
165
|
+
if (expression[0]?.value === '{') {
|
|
166
|
+
nested = readObject(expression, 0, depth + 1);
|
|
167
|
+
if (nested.opaque || nested.end !== expression.length - 1) opaque = true;
|
|
168
|
+
}
|
|
169
|
+
properties[key.value] = { expression, nested };
|
|
170
|
+
if (list[index]?.value === ',') index++;
|
|
171
|
+
else if (list[index]?.value !== '}') opaque = true;
|
|
172
|
+
}
|
|
173
|
+
if (list[index]?.value !== '}') opaque = true;
|
|
174
|
+
return { properties, opaque, end: index };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function adapterCall(config, moduleName, nestedKey) {
|
|
178
|
+
const binding = defaultImport(config.list, moduleName);
|
|
179
|
+
const properties = nestedKey ? config.properties[nestedKey]?.nested?.properties : config.properties;
|
|
180
|
+
const expression = properties?.adapter?.expression;
|
|
181
|
+
// Resolve the actual configured adapter. Merely importing it is insufficient.
|
|
182
|
+
return !!binding && !!expression && expression[0]?.type === 'name' && expression[0]?.value === binding && expression[1]?.value === '(' && expression.at(-1)?.value === ')' && !expression.some(token => ['?', '=', '&', '|'].includes(token.value));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Only literal exported objects are understood. Computed config stays pending. */
|
|
186
|
+
function literalConfig(source, wrapper) {
|
|
187
|
+
const list = tokens(source);
|
|
188
|
+
let begin = -1;
|
|
189
|
+
for (let i = 0; i < list.length; i++) {
|
|
190
|
+
if (list[i]?.value === 'export' && list[i + 1]?.value === 'default') begin = i + 2;
|
|
191
|
+
if (list[i]?.value === 'module' && list[i + 1]?.value === '.' && list[i + 2]?.value === 'exports' && list[i + 3]?.value === '=') begin = i + 4;
|
|
192
|
+
}
|
|
193
|
+
if (begin < 0) return { literals: {}, properties: {}, opaque: true, list };
|
|
194
|
+
if (wrapper && list[begin]?.value === wrapper && list[begin + 1]?.value === '(') begin += 2;
|
|
195
|
+
if (list[begin]?.value !== '{') return { literals: {}, properties: {}, opaque: true, list };
|
|
196
|
+
const parsed = readObject(list, begin), literals = {};
|
|
197
|
+
let opaque = parsed.opaque;
|
|
198
|
+
for (const key of ['output', 'ssr', 'preset']) {
|
|
199
|
+
const expression = parsed.properties[key]?.expression;
|
|
200
|
+
if (!expression) continue;
|
|
201
|
+
if (expression.length === 1 && (expression[0].type === 'string' || ['true', 'false'].includes(expression[0].value))) literals[key] = expression[0].value;
|
|
202
|
+
else opaque = true;
|
|
203
|
+
}
|
|
204
|
+
// Assignment, a second export or condition after the object can override it.
|
|
205
|
+
if (list.slice(parsed.end + 1).some(token => ![')', ';'].includes(token.value))) opaque = true;
|
|
206
|
+
return { literals, properties: parsed.properties, opaque, list };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function dependency(manifest, name, productionOnly = false) {
|
|
210
|
+
return (productionOnly ? ['dependencies'] : ['dependencies', 'devDependencies']).some(section =>
|
|
211
|
+
object(manifest?.[section]) && own(manifest[section], name) && typeof manifest[section][name] === 'string');
|
|
212
|
+
}
|
|
213
|
+
function commands(manifest) {
|
|
214
|
+
if (!object(manifest?.scripts)) return [];
|
|
215
|
+
return ['dev', 'start', 'serve', 'build', 'generate', 'preview'].flatMap(key => typeof manifest.scripts[key] === 'string' && manifest.scripts[key].length <= 8192 ? simpleCommands(manifest.scripts[key]) : []);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Understand ordinary command boundaries and quoted words, never shell
|
|
219
|
+
// expansion/substitution. Text printed by `echo "next build"` is not a command.
|
|
220
|
+
function simpleCommands(source) {
|
|
221
|
+
const result = [];
|
|
222
|
+
let command = [], word = '', quote = null, dynamic = false;
|
|
223
|
+
const endWord = () => { if (word) command.push(word); word = ''; };
|
|
224
|
+
const endCommand = () => { endWord(); if (command.length && !dynamic) result.push(command); command = []; dynamic = false; };
|
|
225
|
+
for (let index = 0; index < source.length; index++) {
|
|
226
|
+
const value = source[index];
|
|
227
|
+
if (value === '\\' || value === '$' || value === '`' || value === '(' || value === ')') dynamic = true;
|
|
228
|
+
if (quote) { if (value === quote) quote = null; else word += value; continue; }
|
|
229
|
+
if (value === '"' || value === "'") { quote = value; continue; }
|
|
230
|
+
if ([';', '&', '|', '\n', '\r'].includes(value)) { endCommand(); continue; }
|
|
231
|
+
if (/\s/.test(value)) { endWord(); continue; }
|
|
232
|
+
if (value === '#') { endCommand(); break; }
|
|
233
|
+
word += value;
|
|
234
|
+
}
|
|
235
|
+
if (quote) dynamic = true;
|
|
236
|
+
endCommand();
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
239
|
+
function commandUsed(scripts, command, operation = '[a-z-]+') {
|
|
240
|
+
return scripts.some(words => new RegExp(`^(?:${command})$`).test(words[0]) && new RegExp(`^(?:${operation})$`).test(words[1] ?? ''));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Only known execution-preserving Node options may precede a server entry.
|
|
244
|
+
// Unknown flags and eval, print, check, preload or loader modes stay pending;
|
|
245
|
+
// skipping arbitrary option values could mistake code or another file for it.
|
|
246
|
+
const nodeEntryFlags = new Set([
|
|
247
|
+
'--enable-source-maps', '--watch', '--watch-preserve-output',
|
|
248
|
+
'--no-warnings', '--trace-warnings', '--trace-uncaught',
|
|
249
|
+
]);
|
|
250
|
+
const nodeCondition = /^[A-Za-z0-9][A-Za-z0-9_.:-]*$/;
|
|
251
|
+
function invokesServerEntry(words, entry) {
|
|
252
|
+
const matches = value => value === entry || value === `./${entry}`;
|
|
253
|
+
if (['tsx', 'ts-node'].includes(words[0])) return matches(words[1]);
|
|
254
|
+
if (words[0] !== 'node') return false;
|
|
255
|
+
for (let index = 1; index < words.length; index++) {
|
|
256
|
+
const option = words[index];
|
|
257
|
+
if (option === '--') return matches(words[index + 1]);
|
|
258
|
+
if (!option.startsWith('-')) return matches(option);
|
|
259
|
+
if (nodeEntryFlags.has(option)) continue;
|
|
260
|
+
if (option.startsWith('--conditions=')) {
|
|
261
|
+
if (!nodeCondition.test(option.slice('--conditions='.length))) return false;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (option === '--conditions' || option === '-C') {
|
|
265
|
+
if (!nodeCondition.test(words[index + 1] ?? '')) return false;
|
|
266
|
+
index++;
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function composerRequires(manifest, name) {
|
|
275
|
+
return object(manifest?.require) && own(manifest.require, name) && typeof manifest.require[name] === 'string';
|
|
276
|
+
}
|
|
277
|
+
function evidenceAllowed(file, signal) {
|
|
278
|
+
if (!allowedFiles.has(file) || !signalNames.has(signal)) return false;
|
|
279
|
+
if ([S.react, S.vue, S.angular, S.next, S.nuxt, S.astro, S.svelte, S.nuxtStatic].includes(signal)) return file === 'package.json';
|
|
280
|
+
if ([S.laravel, S.wordpress, S.php].includes(signal)) return file === 'composer.json';
|
|
281
|
+
if (signal === S.python) return ['requirements.txt', 'pyproject.toml'].includes(file);
|
|
282
|
+
if (signal === S.ruby) return file === 'Gemfile';
|
|
283
|
+
if (signal === S.java) return ['pom.xml', 'build.gradle', 'build.gradle.kts'].includes(file);
|
|
284
|
+
if (signal === S.node) return nodeEntries.includes(file);
|
|
285
|
+
if (signal === S.monorepo) return ['package.json', 'pnpm-workspace.yaml'].includes(file);
|
|
286
|
+
if (signal === S.nextStatic) return configNames('next').includes(file);
|
|
287
|
+
if ([S.astroStatic, S.astroServer].includes(signal)) return configNames('astro').includes(file);
|
|
288
|
+
if ([S.svelteStatic, S.svelteNode].includes(signal)) return configNames('svelte').includes(file);
|
|
289
|
+
return signal === S.dynamic && ['next', 'nuxt', 'astro', 'svelte'].some(name => configNames(name).includes(file));
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function classify(evidence, invalid = false) {
|
|
293
|
+
const has = signal => evidence.some(item => item.signal === signal);
|
|
294
|
+
const frontends = [[S.react, 'React'], [S.vue, 'Vue'], [S.angular, 'Angular']].filter(([signal]) => has(signal)).map(([, name]) => name);
|
|
295
|
+
let frontend = frontends.length === 1 ? frontends[0] : frontends.length ? 'Unknown' : 'None';
|
|
296
|
+
const frameworks = [[S.next, 'Next.js'], [S.nuxt, 'Nuxt'], [S.astro, 'Astro'], [S.svelte, 'SvelteKit'], [S.laravel, 'Laravel'], [S.wordpress, 'WordPress']].filter(([signal]) => has(signal)).map(([, name]) => name);
|
|
297
|
+
// Laravel's React/Vue asset packages do not make its PHP backend into Node.
|
|
298
|
+
let framework = frameworks.length === 1 ? frameworks[0] : frameworks.length ? 'Custom' : frontends.length === 1 ? frontend : 'Custom';
|
|
299
|
+
if (framework === 'Next.js' && frontend === 'None') frontend = 'React';
|
|
300
|
+
if (framework === 'Nuxt' && frontend === 'None') frontend = 'Vue';
|
|
301
|
+
if (framework === 'SvelteKit' && frontend === 'None') frontend = 'Unknown';
|
|
302
|
+
const warnings = [];
|
|
303
|
+
const backends = new Set();
|
|
304
|
+
if ([S.laravel, S.wordpress, S.php].some(has)) backends.add('php');
|
|
305
|
+
if (has(S.python)) backends.add('python');
|
|
306
|
+
if (has(S.ruby)) backends.add('ruby');
|
|
307
|
+
if (has(S.java)) backends.add('java');
|
|
308
|
+
if (has(S.node)) backends.add('node');
|
|
309
|
+
let backend = backends.size === 1 ? [...backends][0] : 'unknown';
|
|
310
|
+
let runtime = null, rendering = 'unknown';
|
|
311
|
+
const staticMode = has(S.nextStatic) || has(S.astroStatic) || has(S.svelteStatic) || has(S.nuxtStatic);
|
|
312
|
+
const serverMode = has(S.node) || has(S.astroServer) || has(S.svelteNode);
|
|
313
|
+
const inferredNode = (framework === 'Next.js' || framework === 'Nuxt') && !staticMode;
|
|
314
|
+
if ((serverMode || inferredNode) && backends.size && !backends.has('node')) backends.add('node');
|
|
315
|
+
const configKinds = new Set(evidence.filter(item => [S.nextStatic, S.astroStatic, S.astroServer, S.svelteStatic, S.svelteNode].includes(item.signal)).map(item => item.signal));
|
|
316
|
+
const conflict = frameworks.length > 1 || backends.size > 1 || (staticMode && (serverMode || backends.size > 0)) || configKinds.size > 1;
|
|
317
|
+
if (invalid) warnings.push(W.invalid);
|
|
318
|
+
if (has(S.monorepo)) warnings.push(W.monorepo);
|
|
319
|
+
if (conflict) warnings.push(W.conflict);
|
|
320
|
+
if (has(S.dynamic)) warnings.push(W.dynamic);
|
|
321
|
+
if (!warnings.length) {
|
|
322
|
+
if (staticMode) { backend = 'none'; runtime = 'static'; rendering = 'static'; }
|
|
323
|
+
else if (serverMode || inferredNode) { backend = 'node'; runtime = 'node'; rendering = 'server'; }
|
|
324
|
+
else if (['php', 'python', 'ruby', 'java'].includes(backend)) { runtime = 'rest'; rendering = 'server'; }
|
|
325
|
+
}
|
|
326
|
+
if (conflict) { backend = 'unknown'; if (frameworks.length > 1) framework = 'Custom'; }
|
|
327
|
+
if (!runtime) {
|
|
328
|
+
if (frontends.length && !backends.size && !frameworks.length) warnings.push(W.frontend);
|
|
329
|
+
warnings.push(W.unknown);
|
|
330
|
+
} else warnings.push(W.unverified);
|
|
331
|
+
return { version: 1, status: runtime ? 'detected' : 'needs-developer', frontend, framework, backend, runtime, rendering, evidence, warnings };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** @param {Record<string, string>} files Exact allowlisted project-relative names. */
|
|
335
|
+
export function analyzeProjectFiles(files) {
|
|
336
|
+
const evidence = [], selected = Object.create(null);
|
|
337
|
+
let total = 0, invalid = !object(files);
|
|
338
|
+
const add = (file, signal) => { if (!evidence.some(item => item.file === file && item.signal === signal)) evidence.push({ file, signal }); };
|
|
339
|
+
for (const name of PROJECT_ANALYSIS_FILES) {
|
|
340
|
+
if (!object(files) || !own(files, name)) continue;
|
|
341
|
+
const descriptor = Object.getOwnPropertyDescriptor(files, name);
|
|
342
|
+
const value = descriptor?.value;
|
|
343
|
+
if (typeof value !== 'string' || value.length > MAX_ANALYSIS_FILE_BYTES || byteLength(value) > MAX_ANALYSIS_FILE_BYTES) { invalid = true; continue; }
|
|
344
|
+
total += byteLength(value);
|
|
345
|
+
if (total > MAX_ANALYSIS_TOTAL_BYTES) { invalid = true; continue; }
|
|
346
|
+
selected[name] = value;
|
|
347
|
+
}
|
|
348
|
+
const json = name => {
|
|
349
|
+
if (!own(selected, name)) return null;
|
|
350
|
+
try { const value = JSON.parse(selected[name]); if (!object(value)) throw Error(); return value; }
|
|
351
|
+
catch { invalid = true; return null; }
|
|
352
|
+
};
|
|
353
|
+
const pkg = json('package.json'), composer = json('composer.json'), scripts = commands(pkg);
|
|
354
|
+
if (pkg && own(pkg, 'workspaces') && ((Array.isArray(pkg.workspaces) && pkg.workspaces.length) || (object(pkg.workspaces) && Object.keys(pkg.workspaces).length))) add('package.json', S.monorepo);
|
|
355
|
+
if (selected['pnpm-workspace.yaml'] && /^\s*packages\s*:/m.test(selected['pnpm-workspace.yaml'])) add('pnpm-workspace.yaml', S.monorepo);
|
|
356
|
+
if (dependency(pkg, 'react')) add('package.json', S.react);
|
|
357
|
+
if (dependency(pkg, 'vue')) add('package.json', S.vue);
|
|
358
|
+
if (dependency(pkg, '@angular/core')) add('package.json', S.angular);
|
|
359
|
+
if (dependency(pkg, 'next') && commandUsed(scripts, 'next', '(?:dev|build|start)')) add('package.json', S.next);
|
|
360
|
+
if (dependency(pkg, 'nuxt') && commandUsed(scripts, '(?:nuxt|nuxi)', '(?:dev|build|start|generate|preview)')) add('package.json', S.nuxt);
|
|
361
|
+
if (dependency(pkg, 'astro') && commandUsed(scripts, 'astro', '(?:dev|build|preview)')) add('package.json', S.astro);
|
|
362
|
+
if (dependency(pkg, '@sveltejs/kit') && commandUsed(scripts, '(?:vite|svelte-kit)', '(?:dev|build|preview)')) add('package.json', S.svelte);
|
|
363
|
+
if (composerRequires(composer, 'laravel/framework')) add('composer.json', S.laravel);
|
|
364
|
+
else if (['johnpbloch/wordpress', 'roots/wordpress', 'roots/wordpress-core'].some(name => composerRequires(composer, name))) add('composer.json', S.wordpress);
|
|
365
|
+
if (composerRequires(composer, 'php')) add('composer.json', S.php);
|
|
366
|
+
for (const name of ['requirements.txt', 'pyproject.toml']) {
|
|
367
|
+
const value = selected[name];
|
|
368
|
+
if (value && (name === 'requirements.txt' ? /^\s*(?:django|flask|fastapi)(?:\[[a-z, -]+\])?\s*(?:[=<>!~;]|$)/im.test(value.replace(/^\s*#.*$/gm, '')) : /(?:^\s*(?:django|flask|fastapi)\s*=|["'](?:django|flask|fastapi)(?:[=<>!~]|["']))/im.test(value.replace(/^\s*#.*$/gm, '')))) add(name, S.python);
|
|
369
|
+
}
|
|
370
|
+
if (selected.Gemfile && /^\s*gem\s+["']rails["'](?:\s|,|$)/m.test(selected.Gemfile)) add('Gemfile', S.ruby);
|
|
371
|
+
if (selected['pom.xml'] && /<artifactId>\s*spring-boot-starter(?:-[a-z-]+)?\s*<\/artifactId>/.test(selected['pom.xml'].replace(/<!--[\s\S]*?-->/g, ''))) add('pom.xml', S.java);
|
|
372
|
+
for (const name of ['build.gradle', 'build.gradle.kts']) if (selected[name] && tokens(selected[name]).some(token => token.type === 'string' && token.value.startsWith('org.springframework.boot:spring-boot-starter'))) add(name, S.java);
|
|
373
|
+
|
|
374
|
+
for (const name of nodeEntries) {
|
|
375
|
+
if (!own(selected, name)) continue;
|
|
376
|
+
if (!scripts.some(words => invokesServerEntry(words, name))) continue;
|
|
377
|
+
const list = tokens(selected[name]);
|
|
378
|
+
const serverImports = ['express', 'fastify', 'koa', 'h3'].some(name => dependency(pkg, name, true) && imported(list, name));
|
|
379
|
+
const httpImports = ['node:http', 'http', 'node:https', 'https'].some(name => imported(list, name));
|
|
380
|
+
const createsServer = list.some((token, index) => token.value === 'createServer' && list[index + 1]?.value === '(');
|
|
381
|
+
const listens = list.some((token, index) => token.value === 'listen' && list[index + 1]?.value === '(');
|
|
382
|
+
if (listens && (serverImports || (httpImports && createsServer))) add(name, S.node);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const has = signal => evidence.some(item => item.signal === signal);
|
|
386
|
+
for (const [name, wrapper, frameworkSignal] of [['next', null, S.next], ['nuxt', 'defineNuxtConfig', S.nuxt], ['astro', 'defineConfig', S.astro], ['svelte', null, S.svelte]]) {
|
|
387
|
+
if (!has(frameworkSignal)) continue;
|
|
388
|
+
const found = configNames(name).filter(file => own(selected, file));
|
|
389
|
+
for (const file of found) {
|
|
390
|
+
const config = literalConfig(selected[file], wrapper);
|
|
391
|
+
const { literals, opaque, list } = config;
|
|
392
|
+
if (opaque || found.length > 1) { add(file, S.dynamic); continue; }
|
|
393
|
+
if (name === 'next' && literals.output === 'export') add(file, S.nextStatic);
|
|
394
|
+
if (name === 'next' && literals.output && !['export', 'standalone'].includes(literals.output)) add(file, S.dynamic);
|
|
395
|
+
if (name === 'nuxt' && (literals.ssr === 'false' || list.some(token => token.value === 'preset'))) add(file, S.dynamic);
|
|
396
|
+
if (name === 'astro') {
|
|
397
|
+
if (literals.output === 'static') add(file, S.astroStatic);
|
|
398
|
+
else if (literals.output === 'server' && dependency(pkg, '@astrojs/node') && adapterCall(config, '@astrojs/node')) add(file, S.astroServer);
|
|
399
|
+
else add(file, S.dynamic);
|
|
400
|
+
}
|
|
401
|
+
if (name === 'svelte') {
|
|
402
|
+
const staticAdapter = dependency(pkg, '@sveltejs/adapter-static') && adapterCall(config, '@sveltejs/adapter-static', 'kit');
|
|
403
|
+
const nodeAdapter = dependency(pkg, '@sveltejs/adapter-node') && adapterCall(config, '@sveltejs/adapter-node', 'kit');
|
|
404
|
+
if (staticAdapter) add(file, S.svelteStatic);
|
|
405
|
+
if (nodeAdapter) add(file, S.svelteNode);
|
|
406
|
+
if (!staticAdapter && !nodeAdapter) add(file, S.dynamic);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (has(S.nuxt) && commandUsed(scripts, '(?:nuxt|nuxi)', 'generate') && !commandUsed(scripts, '(?:nuxt|nuxi)', '(?:start|preview)') && !scripts.some(words => words[0] === 'node')) add('package.json', S.nuxtStatic);
|
|
411
|
+
return classify(evidence, invalid);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** Strict imported-report validation: arbitrary text and inconsistent advice fail closed. */
|
|
415
|
+
export function validateProjectAnalysis(value) {
|
|
416
|
+
const keys = ['version', 'status', 'frontend', 'framework', 'backend', 'runtime', 'rendering', 'evidence', 'warnings'];
|
|
417
|
+
if (!object(value) || ![1, 2].includes(value.version) || !exactKeys(value, value.version === 2 ? [...keys, 'diagnostics'] : keys)) return null;
|
|
418
|
+
const diagnostics = value.version === 2 ? validateDependencyDiagnostics(value.diagnostics) : null;
|
|
419
|
+
if (value.version === 2 && !diagnostics) return null;
|
|
420
|
+
if (!Array.isArray(value.evidence) || value.evidence.length > 64 || !Array.isArray(value.warnings) || value.warnings.length > 8) return null;
|
|
421
|
+
const evidence = [], seen = new Set();
|
|
422
|
+
for (const item of value.evidence) {
|
|
423
|
+
if (!exactKeys(item, ['file', 'signal']) || typeof item.file !== 'string' || typeof item.signal !== 'string' || !evidenceAllowed(item.file, item.signal)) return null;
|
|
424
|
+
const identity = `${item.file}:${item.signal}`;
|
|
425
|
+
if (seen.has(identity)) return null;
|
|
426
|
+
seen.add(identity); evidence.push({ file: item.file, signal: item.signal });
|
|
427
|
+
}
|
|
428
|
+
if (value.warnings.some(warning => typeof warning !== 'string' || !warningNames.has(warning)) || new Set(value.warnings).size !== value.warnings.length) return null;
|
|
429
|
+
const has = signal => evidence.some(item => item.signal === signal);
|
|
430
|
+
if ((has(S.nextStatic) && !has(S.next)) || ((has(S.astroStatic) || has(S.astroServer)) && !has(S.astro)) || ((has(S.svelteStatic) || has(S.svelteNode)) && !has(S.svelte)) || (has(S.nuxtStatic) && !has(S.nuxt))) return null;
|
|
431
|
+
for (const item of evidence) if (item.signal === S.dynamic) {
|
|
432
|
+
const needed = [['next', S.next], ['nuxt', S.nuxt], ['astro', S.astro], ['svelte', S.svelte]].find(([name]) => configNames(name).includes(item.file))?.[1];
|
|
433
|
+
if (!needed || !has(needed)) return null;
|
|
434
|
+
}
|
|
435
|
+
const expected = classify(evidence, value.warnings.includes(W.invalid));
|
|
436
|
+
for (const key of ['status', 'frontend', 'framework', 'backend', 'runtime', 'rendering']) if (value[key] !== expected[key]) return null;
|
|
437
|
+
if (JSON.stringify(value.warnings) !== JSON.stringify(expected.warnings)) return null;
|
|
438
|
+
return diagnostics ? { ...expected, version: 2, diagnostics } : expected;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** Framework identity may be clear even when a frontend's backend is still unknown. */
|
|
442
|
+
export function detectedProjectFramework(value) {
|
|
443
|
+
const report = validateProjectAnalysis(value);
|
|
444
|
+
if (!report || report.framework === 'Custom' || report.warnings.some(warning => [W.invalid, W.monorepo, W.conflict, W.dynamic].includes(warning))) return null;
|
|
445
|
+
return report.framework;
|
|
446
|
+
}
|