UNPKG

1.25 kBPlain TextView Raw
1import { CliEnhancer, CLI_USAGE_ERROR } from '@alwaysai/alwayscli';
2import logSymbols = require('log-symbols');
3import { audit, openAuditLog } from './util/audit';
4import { ALWAYSAI_AUDIT_LOG } from './environment';
5import { postTrackingDataToSegment } from './util/post-tracking-data-to-segment';
6
7export const enhancer: CliEnhancer = argvInterface => async (...argv: string[]) => {
8 if (ALWAYSAI_AUDIT_LOG) {
9 try {
10 await openAuditLog(ALWAYSAI_AUDIT_LOG);
11 } catch (exception) {
12 console.error(
13 `${logSymbols.warning} Failed to open audit log: "${exception.message}"`,
14 );
15 }
16 }
17
18 audit(`start "${argv.join(' ')}"`);
19
20 const argvString = argv.join(' ');
21 const trackingPromise = postTrackingDataToSegment(argvString);
22 try {
23 const returnValue = await argvInterface(...argv);
24
25 await new Promise(resolve => {
26 audit(`end "${returnValue}"`, () => {
27 resolve();
28 });
29 });
30 await trackingPromise;
31 return returnValue;
32 } catch (exception) {
33 if (exception.code !== CLI_USAGE_ERROR) {
34 await Promise.all([
35 postTrackingDataToSegment(argvString, exception),
36 trackingPromise,
37 ]);
38 } else {
39 await trackingPromise;
40 }
41 throw exception;
42 }
43};