Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): opt-in anonymous usage data (#4022)
- Loading branch information
Showing
10 changed files
with
483 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { open, mkdirp } from '@ionic/utils-fs'; | ||
import { fork } from '@ionic/utils-subprocess'; | ||
import Debug from 'debug'; | ||
import { request } from 'https'; | ||
import { resolve } from 'path'; | ||
|
||
import type { Metric } from './telemetry'; | ||
import { ENV_PATHS } from './util/cli'; | ||
|
||
const debug = Debug('capacitor:ipc'); | ||
|
||
export interface TelemetryIPCMessage { | ||
type: 'telemetry'; | ||
data: Metric<string, unknown>; | ||
} | ||
|
||
export type IPCMessage = TelemetryIPCMessage; | ||
|
||
/** | ||
* Send an IPC message to a forked process. | ||
*/ | ||
export async function send(msg: IPCMessage): Promise<void> { | ||
const dir = ENV_PATHS.log; | ||
await mkdirp(dir); | ||
const logPath = resolve(dir, 'ipc.log'); | ||
|
||
debug( | ||
'Sending %O IPC message to forked process (logs: %O)', | ||
msg.type, | ||
logPath, | ||
); | ||
|
||
const fd = await open(logPath, 'a'); | ||
const p = fork(process.argv[1], ['📡'], { stdio: ['ignore', fd, fd, 'ipc'] }); | ||
|
||
p.send(msg); | ||
p.disconnect(); | ||
p.unref(); | ||
} | ||
|
||
/** | ||
* Receive and handle an IPC message. | ||
* | ||
* Assume minimal context and keep external dependencies to a minimum. | ||
*/ | ||
export async function receive(msg: IPCMessage): Promise<void> { | ||
debug('Received %O IPC message', msg.type); | ||
|
||
if (msg.type === 'telemetry') { | ||
const now = new Date().toISOString(); | ||
const { data } = msg; | ||
|
||
// This request is only made if telemetry is on. | ||
const req = request( | ||
{ | ||
hostname: 'api.ionicjs.com', | ||
port: 443, | ||
path: '/events/metrics', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
response => { | ||
debug( | ||
'Sent %O metric to events service (status: %O)', | ||
data.name, | ||
response.statusCode, | ||
); | ||
|
||
if (response.statusCode !== 204) { | ||
response.on('data', chunk => { | ||
debug( | ||
'Bad response from events service. Request body: %O', | ||
chunk.toString(), | ||
); | ||
}); | ||
} | ||
}, | ||
); | ||
|
||
const body = { | ||
metrics: [data], | ||
sent_at: now, | ||
}; | ||
|
||
req.end(JSON.stringify(body)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { readJSON, writeJSON, mkdirp } from '@ionic/utils-fs'; | ||
import Debug from 'debug'; | ||
import { dirname, resolve } from 'path'; | ||
|
||
import { ENV_PATHS } from './util/cli'; | ||
import { uuidv4 } from './util/uuid'; | ||
|
||
const debug = Debug('capacitor:sysconfig'); | ||
|
||
const SYSCONFIG_FILE = 'sysconfig.json'; | ||
const SYSCONFIG_PATH = resolve(ENV_PATHS.config, SYSCONFIG_FILE); | ||
|
||
export interface SystemConfig { | ||
/** | ||
* A UUID that anonymously identifies this computer. | ||
*/ | ||
readonly machine: string; | ||
|
||
/** | ||
* Whether telemetry is enabled or not. | ||
* | ||
* If undefined, a choice has not yet been made. | ||
*/ | ||
readonly telemetry?: boolean; | ||
} | ||
|
||
export async function readConfig(): Promise<SystemConfig> { | ||
debug('Reading from %O', SYSCONFIG_PATH); | ||
|
||
try { | ||
return await readJSON(SYSCONFIG_PATH); | ||
} catch (e) { | ||
if (e.code !== 'ENOENT') { | ||
throw e; | ||
} | ||
|
||
const sysconfig: SystemConfig = { | ||
machine: uuidv4(), | ||
}; | ||
|
||
await writeConfig(sysconfig); | ||
|
||
return sysconfig; | ||
} | ||
} | ||
|
||
export async function writeConfig(sysconfig: SystemConfig): Promise<void> { | ||
debug('Writing to %O', SYSCONFIG_PATH); | ||
|
||
await mkdirp(dirname(SYSCONFIG_PATH)); | ||
await writeJSON(SYSCONFIG_PATH, sysconfig, { spaces: '\t' }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.