-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): [STENCIL-12] Adding Telemetry and CLI for users to toggle …
…Telemetry
- Loading branch information
1 parent
744c9a2
commit 0ec5bbd
Showing
9 changed files
with
1,820 additions
and
42 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,35 @@ | ||
import type { Config} from '../declarations'; | ||
import { CoreCompiler } from './load-compiler'; | ||
import { checkTelemetry, disableTelemetry, enableTelemetry } from './telemetry/telemetry'; | ||
|
||
export const taskTelemetry = async (_coreCompiler: CoreCompiler, config: Config) => { | ||
const p = config.logger.dim(config.sys.details.platform === 'windows' ? '>' : '$'); | ||
const logger = config.logger; | ||
const isEnabling = config.flags.args.includes("on"); | ||
const isDisabling = config.flags.args.includes("off"); | ||
const hasTelemetry = await checkTelemetry(); | ||
const INFORMATION = `\n\nInformation about the data we collect is available on our website: ${logger.bold('https://stenciljs.com/telemetry')}` | ||
const THANK_YOU = `\n\nThank you for helping to make Stencil better! 💖` + INFORMATION; | ||
|
||
if (isEnabling) { | ||
await enableTelemetry() | ||
console.log(`${logger.bold('Telemetry:')} ${logger.dim('Enabled') + THANK_YOU}\n`) | ||
return; | ||
} | ||
|
||
if (isDisabling) { | ||
await disableTelemetry() | ||
console.log(`${logger.bold('Telemetry:')} ${logger.dim('Disabled') + INFORMATION}\n`) | ||
return; | ||
} | ||
|
||
|
||
console.log(` | ||
${logger.bold('Telemetry:')} ${hasTelemetry ? logger.dim('Enabled') + THANK_YOU : logger.dim('Disabled') + INFORMATION} | ||
${p} ${logger.green('stencil telemetry [off|on]')} | ||
${logger.cyan('off')} ${logger.dim('.............')} Disable sharing anonymous usage data | ||
${logger.cyan('on')} ${logger.dim('..............')} Enable sharing anonymous usage data | ||
`); | ||
}; |
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,23 @@ | ||
import envPaths from 'env-paths'; | ||
import { Logger } from 'src/declarations'; | ||
|
||
import { isFatal } from './errors'; | ||
|
||
export const ENV_PATHS = envPaths('capacitor', { suffix: '' }); | ||
|
||
export type CommanderAction = (...args: any[]) => void | Promise<void>; | ||
|
||
export function wrapAction(action: CommanderAction, logger: Logger): CommanderAction { | ||
return async (...args: any[]) => { | ||
try { | ||
await action(...args); | ||
} catch (e) { | ||
if (isFatal(e)) { | ||
process.exitCode = e.exitCode; | ||
logger.error(e.message); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}; | ||
} |
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,19 @@ | ||
export abstract class BaseException<T> extends Error { | ||
constructor (readonly message: string, readonly code: T) { | ||
super(message); | ||
} | ||
} | ||
|
||
export class FatalException extends BaseException<'FATAL'> { | ||
constructor (readonly message: string, readonly exitCode = 1) { | ||
super(message, 'FATAL'); | ||
} | ||
} | ||
|
||
export function fatal(message: string): never { | ||
throw new FatalException(message); | ||
} | ||
|
||
export function isFatal(e: any): e is FatalException { | ||
return e && e instanceof FatalException; | ||
} |
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,88 @@ | ||
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 './cli'; | ||
|
||
const debug = Debug('stencil: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)); | ||
} | ||
} |
Oops, something went wrong.