Skip to content

Commit 86a7d38

Browse files
committed
Set file search path for opened scripts
1 parent 64401be commit 86a7d38

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

main.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ function getOpenUrlPathname(url) {
123123
return url.split(/[?#]/, 1)[0].toLowerCase();
124124
}
125125

126+
function quoteCypherString(value) {
127+
return `'${value.replace(/'/g, "''")}'`;
128+
}
129+
130+
function getUrlBasePath(url) {
131+
const cleanUrl = url.split(/[?#]/, 1)[0];
132+
const slashIndex = cleanUrl.lastIndexOf('/');
133+
if (slashIndex === -1) {
134+
return '';
135+
}
136+
return cleanUrl.slice(0, slashIndex);
137+
}
138+
126139
function splitCypherScript(script) {
127140
const statements = [];
128141
let current = '';
@@ -299,6 +312,14 @@ async function executeCypherScript(url) {
299312
return;
300313
}
301314

315+
const basePath = getUrlBasePath(url);
316+
if (basePath) {
317+
const ok = await prependFileSearchPath(basePath);
318+
if (!ok) {
319+
return;
320+
}
321+
}
322+
302323
const statements = splitCypherScript(script);
303324
if (statements.length === 0) {
304325
print('No Cypher statements found', 'info');
@@ -317,6 +338,46 @@ async function executeCypherScript(url) {
317338
}
318339
}
319340

341+
async function getCurrentFileSearchPath() {
342+
let result = null;
343+
344+
try {
345+
result = await conn.query("CALL CURRENT_SETTING('file_search_path') RETURN *;");
346+
if (!result.isSuccess()) {
347+
throw new Error(await result.getErrorMessage());
348+
}
349+
350+
const rows = await result.getAllObjects();
351+
const row = rows[0] || {};
352+
return row.file_search_path || Object.values(row)[0] || '';
353+
} finally {
354+
if (result) {
355+
await result.close().catch(() => {});
356+
}
357+
}
358+
}
359+
360+
async function prependFileSearchPath(basePath) {
361+
if (!conn) {
362+
print('Database not initialized', 'error');
363+
return false;
364+
}
365+
366+
try {
367+
const currentPath = await getCurrentFileSearchPath();
368+
const paths = currentPath ? currentPath.split(',').filter(Boolean) : [];
369+
const nextPath = paths.includes(basePath) ? currentPath : [basePath, ...paths].join(',');
370+
const ok = await runQuery(`CALL file_search_path=${quoteCypherString(nextPath)}`);
371+
if (ok) {
372+
print(`File search path: ${nextPath}`, 'info');
373+
}
374+
return ok;
375+
} catch (err) {
376+
print(`Error setting file search path: ${err.message}`, 'error');
377+
return false;
378+
}
379+
}
380+
320381
async function openDatabaseUrl(url) {
321382
print(`Opening database ${url}`, 'info');
322383

0 commit comments

Comments
 (0)