Skip to content

Commit 526b267

Browse files
committed
It's possible to run import commands against the directory making it easier to pass session-id
1 parent 5b12095 commit 526b267

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

src/cli.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,38 +309,67 @@ importCommand
309309
});
310310
}));
311311

312+
const SESSION_FILE_NAME = ".fundamento-session.json";
313+
314+
function resolveSessionId(sessionIdOrDir) {
315+
const sessionFile = path.join(sessionIdOrDir, SESSION_FILE_NAME);
316+
if (!fs.existsSync(sessionFile)) return sessionIdOrDir;
317+
try {
318+
const data = JSON.parse(fs.readFileSync(sessionFile, "utf8"));
319+
if (!data.session_id) throw new Error();
320+
return data.session_id;
321+
} catch {
322+
console.error(chalk.red("Error:"), `Invalid session file: ${sessionFile}`);
323+
process.exit(1);
324+
}
325+
}
326+
312327
importCommand
313-
.command("status <session-id>")
328+
.command("status <session-id|directory>")
314329
.description("Show current status of an import session")
315-
.action(withClient(async (client, sessionId) => {
330+
.action(withClient(async (client, sessionIdOrDir) => {
316331
const manager = new ImportSessionManager(client);
317-
await manager.status(sessionId);
332+
await manager.status(resolveSessionId(sessionIdOrDir));
318333
}));
319334

320335
importCommand
321-
.command("cancel <session-id>")
336+
.command("cancel <session-id|directory>")
322337
.description("Cancel a pending or uploading import session")
323-
.action(withClient(async (client, sessionId) => {
338+
.action(withClient(async (client, sessionIdOrDir) => {
324339
const manager = new ImportSessionManager(client);
325-
await manager.cancel(sessionId);
340+
const sessionId = resolveSessionId(sessionIdOrDir);
341+
try {
342+
await manager.cancel(sessionId);
343+
} catch (error) {
344+
if (error.message.includes("404")) {
345+
console.log(`Session ${sessionId} no longer exists on the server.`);
346+
} else {
347+
throw error;
348+
}
349+
}
350+
const sessionFile = path.join(sessionIdOrDir, SESSION_FILE_NAME);
351+
if (fs.existsSync(sessionFile)) {
352+
fs.unlinkSync(sessionFile);
353+
console.log(`Removed session file: ${sessionFile}`);
354+
}
326355
}));
327356

328357
importCommand
329-
.command("retry <session-id>")
358+
.command("retry <session-id|directory>")
330359
.description("Retry failed files in an import session")
331-
.action(withClient(async (client, sessionId) => {
360+
.action(withClient(async (client, sessionIdOrDir) => {
332361
const manager = new ImportSessionManager(client);
333-
await manager.retry(sessionId);
362+
await manager.retry(resolveSessionId(sessionIdOrDir));
334363
}));
335364

336365
importCommand
337-
.command("log <session-id>")
366+
.command("log <session-id|directory>")
338367
.description("Show file-by-file import log for a session")
339368
.option("--failed-only", "Only show failed files")
340369
.option("-j, --json", "Output as JSON")
341-
.action(withClient(async (client, sessionId, options) => {
370+
.action(withClient(async (client, sessionIdOrDir, options) => {
342371
const manager = new ImportSessionManager(client);
343-
await manager.log(sessionId, { failedOnly: options.failedOnly, json: options.json });
372+
await manager.log(resolveSessionId(sessionIdOrDir), { failedOnly: options.failedOnly, json: options.json });
344373
}));
345374

346375
function collect(value, previous) {

0 commit comments

Comments
 (0)