import Client from './client';
import getScope from './get-scope.ts';
import getArgs from './get-args';

export default async (sentry, error, apiUrl, configFiles) => {
  // Do not report errors in development
  if (!process.pkg) {
    return;
  }

  let user = null;
  let team = null;

  try {
    const { token } = configFiles.readAuthConfigFile();
    const { currentTeam } = configFiles.readConfigFile();
    const client = new Client({ apiUrl, token, currentTeam, debug: false });
    ({ user, team } = await getScope(client));
  } catch (err) {
    // We can safely ignore this, as the error
    // reporting works even without this metadata attached.
  }

  if (user || team) {
    sentry.withScope(scope => {
      if (user) {
        const spec = {
          email: user.email,
          id: user.uid
        };

        if (user.username) {
          spec.username = user.username;
        }

        if (user.name) {
          spec.name = user.name;
        }

        scope.setUser(spec);
      }

      if (team) {
        scope.setTag('currentTeam', team.id);
      }

      // Report process.argv without sensitive data
      let args
      try {
        args = getArgs(process.argv.slice(2), {});
      } catch (_) {}

      if (args) {
        const flags = ['--env', '--build-env', '--token']
        for (const flag of flags) {
          if (args[flag]) args[flag] = 'REDACTED';
        }
        if (args._.length >= 4 && args._[0].startsWith('secret') && args._[1] === 'add') {
          args._[3] = 'REDACTED';
        }
        scope.setExtra('args', args);
      }

      sentry.captureException(error);
    });
  } else {
    sentry.captureException(error);
  }

  const client = sentry.getCurrentHub().getClient();

  if (client) {
    await client.close();
  }
};

@@ -660,32 +660,8 @@
  %7D%0A%0A
-  if (user %7C%7C team) %7B%0A  
   se
@@ -686,26 +686,24 @@
 pe =%3E %7B%0A    
-  
 if (user) %7B%0A
@@ -704,26 +704,24 @@
 er) %7B%0A      
- 
- 
 const spec =
@@ -723,18 +723,16 @@
 pec = %7B%0A
-  
         
@@ -750,18 +750,16 @@
 .email,%0A
-  
         
@@ -781,16 +781,12 @@
     
-  
 %7D;%0A%0A
-  
     
@@ -812,26 +812,24 @@
 ) %7B%0A        
-  
 spec.usernam
@@ -845,34 +845,32 @@
 username;%0A      
-  
 %7D%0A%0A        if (u
@@ -858,26 +858,24 @@
    %7D%0A%0A      
- 
- 
 if (user.nam
@@ -875,26 +875,24 @@
 ser.name) %7B%0A
-  
         spec
@@ -912,31 +912,27 @@
 name;%0A      
- 
- 
 %7D%0A%0A
-  
       scope.
@@ -946,31 +946,27 @@
 (spec);%0A    
-  
 %7D%0A%0A
-  
     if (team
@@ -965,26 +965,24 @@
 if (team) %7B%0A
-  
       scope.
@@ -1013,31 +1013,27 @@
 am.id);%0A    
-  
 %7D%0A%0A
-  
     // Repor
@@ -1074,18 +1074,16 @@
 ata%0A    
-  
 let args
@@ -1087,18 +1087,16 @@
 rgs%0A    
-  
 try %7B%0A  
@@ -1095,26 +1095,24 @@
 try %7B%0A      
- 
- 
 args = getAr
@@ -1146,18 +1146,16 @@
 %7D);%0A    
-  
 %7D catch 
@@ -1162,18 +1162,16 @@
 (_) %7B%7D%0A%0A
-  
     if (
@@ -1180,26 +1180,24 @@
 gs) %7B%0A      
-  
 const flags 
@@ -1234,18 +1234,16 @@
 token'%5D%0A
-  
       fo
@@ -1272,26 +1272,24 @@
 ) %7B%0A        
-  
 if (args%5Bfla
@@ -1319,30 +1319,26 @@
 TED';%0A      
-  %7D%0A  
+%7D%0A
       if (ar
@@ -1418,26 +1418,24 @@
 ) %7B%0A        
-  
 args._%5B3%5D = 
@@ -1448,30 +1448,26 @@
 TED';%0A      
- 
- 
 %7D%0A
-  
       scope.
@@ -1498,69 +1498,10 @@
     
-  %7D%0A%0A      sentry.captureException(error);%0A    %7D);%0A  %7D else %7B
+%7D%0A
 %0A   
@@ -1532,24 +1532,26 @@
 (error);%0A  %7D
+);
 %0A%0A  const cl

Add always report stdarg to Sentry (#1982)