11import fs from 'node:fs' ;
2- import path from 'node:path' ;
32import os from 'node:os' ;
3+ import path from 'node:path' ;
44import type { CommandFlags } from '../core/dispatch.ts' ;
55import type { SessionAction , SessionState } from './types.ts' ;
66import { 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 - z A - Z 0 - 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
0 commit comments