(config: Config, action: CommanderAction)
| 41 | type CommanderAction = (...args: any[]) => void | Promise<void>; |
| 42 | |
| 43 | export function telemetryAction(config: Config, action: CommanderAction): CommanderAction { |
| 44 | return async (...actionArgs: any[]): Promise<void> => { |
| 45 | const start = new Date(); |
| 46 | // This is how commanderjs works--the command object is either the last |
| 47 | // element or second to last if there are additional options (via `.allowUnknownOption()`) |
| 48 | const lastArg = actionArgs[actionArgs.length - 1]; |
| 49 | const cmd: Command = lastArg instanceof Command ? lastArg : actionArgs[actionArgs.length - 2]; |
| 50 | const command = getFullCommandName(cmd); |
| 51 | let error: any; |
| 52 | |
| 53 | try { |
| 54 | await action(...actionArgs); |
| 55 | } catch (e) { |
| 56 | error = e; |
| 57 | } |
| 58 | |
| 59 | const end = new Date(); |
| 60 | const duration = end.getTime() - start.getTime(); |
| 61 | |
| 62 | const packages = Object.entries({ |
| 63 | ...config.app.package.devDependencies, |
| 64 | ...config.app.package.dependencies, |
| 65 | }); |
| 66 | |
| 67 | // Only collect packages in the capacitor org: |
| 68 | // https://www.npmjs.com/org/capacitor |
| 69 | const capacitorPackages = packages.filter(([k]) => k.startsWith('@capacitor/')); |
| 70 | |
| 71 | const versions = capacitorPackages.map(([k, v]) => [ |
| 72 | `${k.replace(/^@capacitor\//, '').replace(/-/g, '_')}_version`, |
| 73 | v, |
| 74 | ]); |
| 75 | |
| 76 | const data: CommandMetricData = { |
| 77 | app_id: await getAppIdentifier(config), |
| 78 | command, |
| 79 | arguments: cmd.args.join(' '), |
| 80 | options: JSON.stringify(cmd.opts()), |
| 81 | duration, |
| 82 | error: error ? (error.message ? error.message : String(error)) : null, |
| 83 | node_version: process.version, |
| 84 | os: config.cli.os, |
| 85 | ios_package_manager: await getIOSPackageManager(config), |
| 86 | ...Object.fromEntries(versions), |
| 87 | }; |
| 88 | |
| 89 | debug('metric payload: %O', data); |
| 90 | |
| 91 | if (isInteractive()) { |
| 92 | let sysconfig = await readConfig(); |
| 93 | |
| 94 | if (!error && typeof sysconfig.telemetry === 'undefined') { |
| 95 | // Telemetry is opt-out; turn telemetry on then inform the user how to opt-out. |
| 96 | sysconfig = { ...sysconfig, telemetry: true }; |
| 97 | await writeConfig(sysconfig); |
| 98 | |
| 99 | output.write(THANK_YOU); |
| 100 | } |
no test coverage detected