Skip to content

Commit 0e97276

Browse files
committed
fixup save-script
1 parent 817ccc1 commit 0e97276

2 files changed

Lines changed: 13 additions & 52 deletions

File tree

src/daemon/session-store.ts

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'node:fs';
2-
import path from 'node:path';
32
import os from 'node:os';
3+
import path from 'node:path';
44
import type { CommandFlags } from '../core/dispatch.ts';
55
import type { SessionAction, SessionState } from './types.ts';
66
import { inferFillText } from './action-utils.ts';
@@ -47,6 +47,9 @@ export class SessionStore {
4747
},
4848
): void {
4949
if (entry.flags?.noRecord) return;
50+
if (entry.flags?.saveScript) {
51+
session.recordSession = true;
52+
}
5053
session.actions.push({
5154
ts: Date.now(),
5255
command: entry.command,
@@ -58,25 +61,13 @@ export class SessionStore {
5861

5962
writeSessionLog(session: SessionState): void {
6063
try {
64+
if (!session.recordSession) return;
6165
if (!fs.existsSync(this.sessionsDir)) fs.mkdirSync(this.sessionsDir, { recursive: true });
6266
const safeName = session.name.replace(/[^a-zA-Z0-9._-]/g, '_');
6367
const timestamp = new Date(session.createdAt).toISOString().replace(/[:.]/g, '-');
6468
const scriptPath = path.join(this.sessionsDir, `${safeName}-${timestamp}.ad`);
65-
const filePath = this.resolveSessionJsonPath(session, safeName, timestamp);
66-
const payload = {
67-
name: session.name,
68-
device: session.device,
69-
createdAt: session.createdAt,
70-
appBundleId: session.appBundleId,
71-
actions: session.actions,
72-
optimizedActions: this.buildOptimizedActions(session),
73-
};
74-
const script = formatScript(session, payload.optimizedActions);
69+
const script = formatScript(session, this.buildOptimizedActions(session));
7570
fs.writeFileSync(scriptPath, script);
76-
if (session.actions.some((action) => action.flags?.recordJson)) {
77-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
78-
fs.writeFileSync(filePath, JSON.stringify(payload, null, 2));
79-
}
8071
} catch {
8172
// ignore
8273
}
@@ -95,36 +86,6 @@ export class SessionStore {
9586
return path.resolve(filePath);
9687
}
9788

98-
private resolveSessionJsonPath(session: SessionState, safeName: string, timestamp: string): string {
99-
const defaultFile = path.join(this.sessionsDir, `${safeName}-${timestamp}.json`);
100-
const actionWithOut = [...session.actions].reverse().find(
101-
(action) =>
102-
action.flags?.recordJson &&
103-
typeof action.flags?.out === 'string' &&
104-
action.flags.out.trim().length > 0,
105-
);
106-
if (!actionWithOut || !actionWithOut.flags?.out) {
107-
return defaultFile;
108-
}
109-
110-
const rawOut = actionWithOut.flags.out.trim();
111-
const resolvedOut = SessionStore.expandHome(rawOut);
112-
const wantsDirectory = rawOut.endsWith('/') || rawOut.endsWith('\\');
113-
if (wantsDirectory) {
114-
return path.join(resolvedOut, `${safeName}-${timestamp}.json`);
115-
}
116-
117-
try {
118-
if (fs.existsSync(resolvedOut) && fs.statSync(resolvedOut).isDirectory()) {
119-
return path.join(resolvedOut, `${safeName}-${timestamp}.json`);
120-
}
121-
} catch {
122-
return defaultFile;
123-
}
124-
125-
return resolvedOut;
126-
}
127-
12889
private buildOptimizedActions(session: SessionState): SessionAction[] {
12990
const optimized: SessionAction[] = [];
13091
for (const action of session.actions) {
@@ -205,8 +166,8 @@ function sanitizeFlags(flags: CommandFlags | undefined): SessionAction['flags']
205166
snapshotRaw,
206167
snapshotBackend,
207168
appsMetadata,
169+
saveScript,
208170
noRecord,
209-
recordJson,
210171
} = flags;
211172
return {
212173
platform,
@@ -222,8 +183,8 @@ function sanitizeFlags(flags: CommandFlags | undefined): SessionAction['flags']
222183
snapshotRaw,
223184
snapshotBackend,
224185
appsMetadata,
186+
saveScript,
225187
noRecord,
226-
recordJson,
227188
};
228189
}
229190

src/utils/args.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export type ParsedArgs = {
2121
appsFilter?: 'launchable' | 'user-installed' | 'all';
2222
appsMetadata?: boolean;
2323
activity?: string;
24+
saveScript?: boolean;
2425
noRecord?: boolean;
25-
recordJson?: boolean;
2626
replayUpdate?: boolean;
2727
help: boolean;
2828
};
@@ -62,8 +62,8 @@ export function parseArgs(argv: string[]): ParsedArgs {
6262
flags.noRecord = true;
6363
continue;
6464
}
65-
if (arg === '--record-json') {
66-
flags.recordJson = true;
65+
if (arg === '--save-script') {
66+
flags.saveScript = true;
6767
continue;
6868
}
6969
if (arg === '--update' || arg === '-u') {
@@ -225,8 +225,8 @@ Flags:
225225
--verbose Stream daemon/runner logs
226226
--json JSON output
227227
--no-record Do not record this action
228-
--record-json Record JSON session log
229-
--update, -u Replay: heal selectors and update replay file in place
228+
--save-script Save session script (.ad) on close
229+
--update, -u Replay: update selectors and rewrite replay file in place
230230
--user-installed Apps: list user-installed packages (Android only)
231231
--all Apps: list all packages (Android only)
232232
`;

0 commit comments

Comments
 (0)