/ src / utils / user.ts
user.ts
 1  import { getGlobalConfig, getOrCreateUserID } from './config.js'
 2  import { memoize } from 'lodash-es'
 3  import { env } from './env.js'
 4  import { type StatsigUser } from '@statsig/js-client'
 5  import { execFileNoThrow } from './execFileNoThrow.js'
 6  import { logError, SESSION_ID } from './log.js'
 7  
 8  export const getGitEmail = memoize(async (): Promise<string | undefined> => {
 9    const result = await execFileNoThrow('git', ['config', 'user.email'])
10    if (result.code !== 0) {
11      logError(`Failed to get git email: ${result.stdout} ${result.stderr}`)
12      return undefined
13    }
14    return result.stdout.trim() || undefined
15  })
16  
17  export const getUser = memoize(async (): Promise<StatsigUser> => {
18    const userID = getOrCreateUserID()
19    const config = getGlobalConfig()
20    const email =
21      process.env.USER_TYPE === 'ant'
22        ? (config.oauthAccount?.emailAddress ??
23          (await getGitEmail()) ??
24          (process.env.COO_CREATOR
25            ? `${process.env.COO_CREATOR}@anthropic.com`
26            : undefined))
27        : undefined
28  
29    return {
30      customIDs: {
31        // for session level tests
32        sessionId: SESSION_ID,
33      },
34      userID,
35      appVersion: MACRO.VERSION,
36      userAgent: env.platform,
37      email,
38      custom: {
39        nodeVersion: env.nodeVersion,
40        userType: process.env.USER_TYPE,
41        organizationUuid: config.oauthAccount?.organizationUuid,
42        accountUuid: config.oauthAccount?.accountUuid,
43      },
44    }
45  })