110 lines
3.6 KiB
JavaScript
110 lines
3.6 KiB
JavaScript
export const createProjectDirectoryRuntime = (dependencies) => {
|
|||
|
|
const {
|
||
|
|
fsPromises,
|
||
|
|
path,
|
||
|
|
normalizeDirectoryPath,
|
||
|
|
readSettingsFromDiskMigrated,
|
||
|
|
getReadSettingsFromDiskMigrated,
|
||
|
|
sanitizeProjects,
|
||
|
|
} = dependencies;
|
||
|
|
|
||
|
|
const resolveDirectoryCandidate = (value) => {
|
||
|
|
if (typeof value !== 'string') {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
const trimmed = value.trim();
|
||
|
|
if (!trimmed) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
const normalized = normalizeDirectoryPath(trimmed);
|
||
|
|
return path.resolve(normalized);
|
||
|
|
};
|
||
|
|
|
||
|
|
const validateDirectoryPath = async (candidate) => {
|
||
|
|
const resolved = resolveDirectoryCandidate(candidate);
|
||
|
|
if (!resolved) {
|
||
|
|
return { ok: false, error: 'Directory parameter is required' };
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
const stats = await fsPromises.stat(resolved);
|
||
|
|
if (!stats.isDirectory()) {
|
||
|
|
return { ok: false, error: 'Specified path is not a directory' };
|
||
|
|
}
|
||
|
|
return { ok: true, directory: resolved };
|
||
|
|
} catch (error) {
|
||
|
|
const err = error;
|
||
|
|
if (err && typeof err === 'object' && err.code === 'ENOENT') {
|
||
|
|
return { ok: false, error: 'Directory not found' };
|
||
|
|
}
|
||
|
|
if (err && typeof err === 'object' && err.code === 'EACCES') {
|
||
|
|
return { ok: false, error: 'Access to directory denied' };
|
||
|
|
}
|
||
|
|
return { ok: false, error: 'Failed to validate directory' };
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const resolveProjectDirectory = async (req) => {
|
||
|
|
const headerDirectory = typeof req.get === 'function' ? req.get('x-opencode-directory') : null;
|
||
|
|
const queryDirectory = Array.isArray(req.query?.directory)
|
||
|
|
? req.query.directory[0]
|
||
|
|
: req.query?.directory;
|
||
|
|
const requested = headerDirectory || queryDirectory || null;
|
||
|
|
|
||
|
|
if (requested) {
|
||
|
|
const validated = await validateDirectoryPath(requested);
|
||
|
|
if (!validated.ok) {
|
||
|
|
return { directory: null, error: validated.error };
|
||
|
|
}
|
||
|
|
return { directory: validated.directory, error: null };
|
||
|
|
}
|
||
|
|
|
||
|
|
const readSettings = typeof getReadSettingsFromDiskMigrated === 'function'
|
||
|
|
? getReadSettingsFromDiskMigrated()
|
||
|
|
: readSettingsFromDiskMigrated;
|
||
|
|
const settings = await readSettings();
|
||
|
|
const projects = sanitizeProjects(settings.projects) || [];
|
||
|
|
if (projects.length === 0) {
|
||
|
|
return { directory: null, error: 'Directory parameter or active project is required' };
|
||
|
|
}
|
||
|
|
|
||
|
|
const activeId = typeof settings.activeProjectId === 'string' ? settings.activeProjectId : '';
|
||
|
|
const active = projects.find((project) => project.id === activeId) || projects[0];
|
||
|
|
if (!active || !active.path) {
|
||
|
|
return { directory: null, error: 'Directory parameter or active project is required' };
|
||
|
|
}
|
||
|
|
|
||
|
|
const validated = await validateDirectoryPath(active.path);
|
||
|
|
if (!validated.ok) {
|
||
|
|
return { directory: null, error: validated.error };
|
||
|
|
}
|
||
|
|
|
||
|
|
return { directory: validated.directory, error: null };
|
||
|
|
};
|
||
|
|
|
||
|
|
const resolveOptionalProjectDirectory = async (req) => {
|
||
|
|
const headerDirectory = typeof req.get === 'function' ? req.get('x-opencode-directory') : null;
|
||
|
|
const queryDirectory = Array.isArray(req.query?.directory)
|
||
|
|
? req.query.directory[0]
|
||
|
|
: req.query?.directory;
|
||
|
|
const requested = headerDirectory || queryDirectory || null;
|
||
|
|
|
||
|
|
if (!requested) {
|
||
|
|
return { directory: null, error: null };
|
||
|
|
}
|
||
|
|
|
||
|
|
const validated = await validateDirectoryPath(requested);
|
||
|
|
if (!validated.ok) {
|
||
|
|
return { directory: null, error: validated.error };
|
||
|
|
}
|
||
|
|
|
||
|
|
return { directory: validated.directory, error: null };
|
||
|
|
};
|
||
|
|
|
||
|
|
return {
|
||
|
|
resolveDirectoryCandidate,
|
||
|
|
validateDirectoryPath,
|
||
|
|
resolveProjectDirectory,
|
||
|
|
resolveOptionalProjectDirectory,
|
||
|
|
};
|
||
|
|
};
|