Files
2026-07-31 13:12:54 -04:00

70 lines
2.6 KiB
JavaScript

import { detectJsPackageManager, installCommandFor, readPackageJson, runScriptCommandFor, } from './packageManager.js';
/**
* Fallback adapter for any project that isn't recognized as Laravel,
* CodeIgniter, or React. Drives everything off `package.json` scripts when
* one exists; otherwise tool calls fail with a clear, actionable message
* instead of guessing.
*/
export class GenericAdapter {
root;
kind = 'generic';
label;
packageManager;
pkg;
constructor(root) {
this.root = root;
this.pkg = readPackageJson(root);
this.packageManager = this.pkg ? detectJsPackageManager(root) : undefined;
this.label = this.pkg ? `Generic Node project (${this.packageManager})` : 'Unrecognized project';
}
installCommand() {
if (!this.packageManager) {
throw new Error(`No package.json found at "${this.root}" and no recognized framework detected. ` +
`Nothing for run_install to do here.`);
}
const { command, args } = installCommandFor(this.packageManager);
return { command, args, cwd: this.root };
}
devCommand() {
return this.scriptCommand(['dev', 'start', 'serve']);
}
buildCommand() {
return this.scriptCommandOrNull(['build']);
}
testCommand(extraArgs = []) {
return this.scriptCommandOrNull(['test'], extraArgs);
}
lintCommand(fix = false) {
return this.scriptCommandOrNull(fix ? ['format', 'lint:fix'] : ['lint']);
}
readyPattern() {
return /(https?:\/\/(?:localhost|127\.0\.0\.1|0\.0\.0\.0)[:\d]*\S*)|ready|listening/i;
}
describe() {
return {
kind: this.kind,
label: this.label,
root: this.root,
packageManager: this.packageManager ?? null,
scripts: this.pkg?.scripts ?? {},
};
}
scriptCommand(candidates) {
const result = this.scriptCommandOrNull(candidates);
if (!result) {
throw new Error(`No package.json script found for any of [${candidates.join(', ')}] in "${this.root}". ` +
`Add one of these scripts, or use run_script with an explicit script name.`);
}
return result;
}
scriptCommandOrNull(candidates, extraArgs = []) {
if (!this.pkg?.scripts || !this.packageManager)
return null;
const script = candidates.find((name) => this.pkg?.scripts?.[name]);
if (!script)
return null;
const { command, args } = runScriptCommandFor(this.packageManager, script, extraArgs);
return { command, args, cwd: this.root };
}
}
//# sourceMappingURL=generic.js.map