@@ -4,9 +4,10 @@ import { getSelectedContact } from './contacts.svelte';
44import { runIcaSidecar , type SidecarResult } from './sidecar' ;
55
66const FORMAT_FLAGS = new Set ( [ '--format' , '-f' ] ) ;
7+ const OUTPUT_FLAGS = new Set ( [ '--output' , '-o' ] ) ;
78const CONTACT_FLAGS = new Set ( [ '--contact' , '-c' ] ) ;
8- const COMBINED_SHORT_FLAGS = new Set ( [ '-c' , '-f' ] ) ;
9- const COMBINED_LONG_FLAGS = new Set ( [ '--contact' , '--format' ] ) ;
9+ const COMBINED_SHORT_FLAGS = new Set ( [ '-c' , '-f' , '-o' ] ) ;
10+ const COMBINED_LONG_FLAGS = new Set ( [ '--contact' , '--format' , '--output' ] ) ;
1011
1112export class MissingContactError extends Error {
1213 constructor ( message = 'No contact selected. Choose a contact before running the analyzer.' ) {
@@ -30,6 +31,14 @@ export interface IcaCsvResult {
3031 args : string [ ] ;
3132}
3233
34+ export interface IcaCommandResult {
35+ code : SidecarResult [ 'code' ] ;
36+ signal : SidecarResult [ 'signal' ] ;
37+ stderr : string ;
38+ stdout : string ;
39+ args : string [ ] ;
40+ }
41+
3342function toCamelCase ( header : string , headerIndex : number ) : string {
3443 const cleaned = header . replace ( / [ ^ 0 - 9 A - Z a - z ] + / g, ' ' ) . trim ( ) ;
3544 if ( ! cleaned ) {
@@ -120,6 +129,11 @@ function ensureCsvFormat(args: string[]): string[] {
120129 return [ ...withoutFormat , '--format' , 'csv' ] ;
121130}
122131
132+ function ensureOutputPath ( args : string [ ] , outputPath : string ) : string [ ] {
133+ const withoutOutput = removeOption ( args , OUTPUT_FLAGS ) ;
134+ return [ ...withoutOutput , '--output' , outputPath ] ;
135+ }
136+
123137function parseCsvOutput (
124138 rawCsv : string ,
125139 finalArgs : string [ ]
@@ -245,6 +259,33 @@ export async function invokeIcaCsv(args: string | string[]): Promise<IcaCsvResul
245259 } ;
246260}
247261
262+ export async function invokeIcaCsvToFile (
263+ args : string | string [ ] ,
264+ outputPath : string
265+ ) : Promise < IcaCommandResult > {
266+ const parsedArgs = parseArgInput ( args ) ;
267+ const expandedArgs = expandKnownCombinedArgs ( parsedArgs ) ;
268+ const argsWithContact = await addContactArgument ( expandedArgs ) ;
269+ const csvArgs = ensureCsvFormat ( argsWithContact ) ;
270+ const finalArgs = ensureOutputPath ( csvArgs , outputPath ) ;
271+
272+ const result = await runIcaSidecar ( finalArgs ) ;
273+ const stderr = result . stderr . trim ( ) ;
274+ const stdout = result . stdout . trim ( ) ;
275+
276+ if ( result . code !== 0 ) {
277+ throw new Error ( stderr || stdout || `ICA exited with code ${ result . code ?? 'unknown' } .` ) ;
278+ }
279+
280+ return {
281+ code : result . code ,
282+ signal : result . signal ,
283+ stderr,
284+ stdout,
285+ args : finalArgs
286+ } ;
287+ }
288+
248289export async function runMessageTotals ( ) : Promise < IcaCsvResult > {
249290 return invokeIcaCsv ( [ 'message_totals' ] ) ;
250291}
0 commit comments