11import fs from 'node:fs' ;
22import { readFile } from 'node:fs/promises' ;
3- import { homedir } from 'node:os' ;
43import nodePath from 'node:path' ;
54
65import * as commander from 'commander' ;
@@ -26,10 +25,10 @@ import type { Workspace } from '~/models/workspace';
2625
2726import type { RequestTestResult } from '../../insomnia-scripting-environment/src/objects' ;
2827import packageJson from '../package.json' ;
28+ import { flushAnalytics , InsoEvent , trackInsoEvent } from './analytics' ;
2929import { exportSpecification , writeFileWithCliOptions } from './commands/export-specification' ;
3030import { getRuleSetFileFromFolderByFilename , lintSpecification } from './commands/lint-specification' ;
3131import { RunCollectionResultReport } from './commands/run-collection/result-report' ;
32- import type { Database } from './db' ;
3332import { isFile , loadDb } from './db' ;
3433import { insomniaExportAdapter } from './db/adapters/insomnia-adapter' ;
3534import { loadApiSpec , promptApiSpec } from './db/models/api-spec' ;
@@ -38,9 +37,12 @@ import type { BaseModel } from './db/models/types';
3837import { loadTestSuites , promptTestSuites } from './db/models/unit-test-suite' ;
3938import { matchIdIsh } from './db/models/util' ;
4039import { loadWorkspace , promptWorkspace } from './db/models/workspace' ;
41- import { BasicReporter , logger , LogLevel } from './logger' ;
40+ import type { Database } from './db/types' ;
41+ import { InsoError } from './errors' ;
42+ import { BasicReporter , logger , LogLevel } from './logger' ;
4243import { logTestResult , logTestResultSummary , reporterTypes , type TestReporter } from './reporter' ;
4344import { generateDocumentation } from './scripts/docs' ;
45+ import { getAppDataDir , getDefaultProductName } from './util' ;
4446
4547export interface GlobalOptions {
4648 ci : boolean ;
@@ -89,45 +91,6 @@ export const tryToReadInsoConfigFile = async (configFile?: string, workingDir?:
8991 return { } ;
9092} ;
9193
92- export class InsoError extends Error {
93- cause ?: Error | null ;
94-
95- constructor ( message : string , cause ?: Error ) {
96- super ( message ) ;
97- this . name = 'InsoError' ;
98- this . cause = cause ;
99- }
100- }
101-
102- /**
103- * getAppDataDir returns the data directory for an Electron app,
104- * it is equivalent to the app.getPath('userData') API in Electron.
105- * https://www.electronjs.org/docs/api/app#appgetpathname
106- */
107- export function getAppDataDir ( app : string ) : string {
108- switch ( process . platform ) {
109- case 'darwin' : {
110- return nodePath . join ( homedir ( ) , 'Library' , 'Application Support' , app ) ;
111- }
112- case 'win32' : {
113- return nodePath . join ( process . env . APPDATA || nodePath . join ( homedir ( ) , 'AppData' , 'Roaming' ) , app ) ;
114- }
115- case 'linux' : {
116- return nodePath . join ( process . env . XDG_DATA_HOME || nodePath . join ( homedir ( ) , '.config' ) , app ) ;
117- }
118- default : {
119- throw new Error ( 'Unsupported platform' ) ;
120- }
121- }
122- }
123- export const getDefaultProductName = ( ) : string => {
124- const name = process . env . DEFAULT_APP_NAME ;
125- if ( ! name ) {
126- throw new Error ( 'Environment variable DEFAULT_APP_NAME is not set.' ) ;
127- }
128- return name ;
129- } ;
130-
13194const localAppDir = getAppDataDir ( getDefaultProductName ( ) ) ;
13295
13396export const getAbsoluteFilePath = ( { workingDir, file } : { workingDir ?: string ; file : string } ) => {
@@ -504,8 +467,15 @@ export const go = (args?: string[]) => {
504467
505468 // TODO: is this necessary?
506469 const success = options . verbose ? await runTestPromise : await noConsoleLog ( ( ) => runTestPromise ) ;
470+
471+ await trackInsoEvent ( InsoEvent . runTest , { success } ) ;
472+ await flushAnalytics ( ) ;
473+
507474 return process . exit ( success ? 0 : 1 ) ;
508475 } catch ( error ) {
476+ await trackInsoEvent ( InsoEvent . runTest , { success : false } ) ;
477+ await flushAnalytics ( ) ;
478+
509479 logErrorAndExit ( error ) ;
510480 }
511481 return process . exit ( 1 ) ;
@@ -892,11 +862,18 @@ export const go = (args?: string[]) => {
892862 logTestResultSummary ( testResultsQueue ) ;
893863
894864 await report . saveReport ( ) ;
865+
866+ await trackInsoEvent ( InsoEvent . runCollection , { success } ) ;
867+ await flushAnalytics ( ) ;
868+
895869 return process . exit ( success ? 0 : 1 ) ;
896870 } catch ( error ) {
897871 report . update ( { error : ( error instanceof Error ? error . message : String ( error ) ) || 'Unknown error' } ) ;
898872 await report . saveReport ( ) ;
899873
874+ await trackInsoEvent ( InsoEvent . runCollection , { success : false } ) ;
875+ await flushAnalytics ( ) ;
876+
900877 logErrorAndExit ( error ) ;
901878 }
902879 return process . exit ( 1 ) ;
@@ -948,8 +925,15 @@ export const go = (args?: string[]) => {
948925
949926 try {
950927 const { isValid } = await lintSpecification ( { specContent, rulesetFileName } ) ;
928+
929+ await trackInsoEvent ( InsoEvent . lintSpec , { success : isValid } ) ;
930+ await flushAnalytics ( ) ;
931+
951932 return process . exit ( isValid ? 0 : 1 ) ;
952933 } catch ( error ) {
934+ await trackInsoEvent ( InsoEvent . lintSpec , { success : false } ) ;
935+ await flushAnalytics ( ) ;
936+
953937 logErrorAndExit ( error ) ;
954938 }
955939 return process . exit ( 1 ) ;
@@ -980,12 +964,23 @@ export const go = (args?: string[]) => {
980964 options . output && getAbsoluteFilePath ( { workingDir : options . workingDir , file : options . output } ) ;
981965 if ( ! outputPath ) {
982966 logger . log ( toExport ) ;
967+
968+ await trackInsoEvent ( InsoEvent . exportSpec , { success : true } ) ;
969+ await flushAnalytics ( ) ;
970+
983971 return process . exit ( 0 ) ;
984972 }
985973 const filePath = await writeFileWithCliOptions ( outputPath , toExport ) ;
986974 logger . log ( `Specification exported to "${ filePath } ".` ) ;
975+
976+ await trackInsoEvent ( InsoEvent . exportSpec , { success : true } ) ;
977+ await flushAnalytics ( ) ;
978+
987979 return process . exit ( 0 ) ;
988980 } catch ( error ) {
981+ await trackInsoEvent ( InsoEvent . exportSpec , { success : false } ) ;
982+ await flushAnalytics ( ) ;
983+
989984 logErrorAndExit ( error ) ;
990985 }
991986 return process . exit ( 1 ) ;
@@ -1020,6 +1015,9 @@ export const go = (args?: string[]) => {
10201015
10211016 logger . debug ( `>> ${ scriptArgs . slice ( 1 ) . join ( ' ' ) } ` ) ;
10221017
1018+ // Track script invocation - the underlying command will track its own success/failure
1019+ await trackInsoEvent ( InsoEvent . script ) ;
1020+
10231021 program . parseAsync ( scriptArgs ) . catch ( logErrorAndExit ) ;
10241022 } ) ;
10251023
0 commit comments