Skip to content

Commit

Permalink
details what is changed in the copied files
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Aug 22, 2024
1 parent ae9e101 commit 502cb70
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
7 changes: 5 additions & 2 deletions packages/create-cloudflare/src/helpers/config-cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Copied from packages/wrangler/src/config-cache.ts
// Copied from packages/wrangler/src/config-cache.ts with the following changes:
// - Removed methods not required for c3
// - Commented out the debug message as it breaks the command line interface

import { readFileSync } from "node:fs";
import * as path from "node:path";
import { findUpSync } from "find-up";
Expand All @@ -16,7 +19,7 @@ function getCacheFolder() {
? path.join(closestNodeModulesDirectory, ".cache/wrangler")
: null;
if (!__cacheFolder) {
// console.debug("No folder available to cache configuration");
// logger.debug("No folder available to cache configuration");
}
return __cacheFolder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copied from packages/wrangler/src/global-wrangler-config-path.ts
// Copied from packages/wrangler/src/global-wrangler-config-path.ts with no modification
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
Expand Down
16 changes: 8 additions & 8 deletions packages/create-cloudflare/src/helpers/metrics-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// Copied from packages/wrangler/src/metrics/metrics-config.ts
// Copied from packages/wrangler/src/metrics/metrics-config.ts with the following changes:
// - Removed methods not required for c3
// - Added `c3permission` property to the `MetricsConfigFile` interface
// - Added a `getSessionId` helper that returns a random UUID
// - Exported both `getDeviceId` and `getUserId` helpers
// - Modified `getUserId` to return the id from the cache only without fetching it from the API

import { randomUUID } from "node:crypto";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
Expand Down Expand Up @@ -67,10 +73,8 @@ export interface MetricsConfigFile {
* Returns an ID that uniquely identifies Wrangler on this device to help collate events.
*
* Once created this ID is stored in the metrics config file.
* Note: This is modified to read the config directly.
*/
export function getDeviceId() {
const config = readMetricsConfig();
export function getDeviceId(config: MetricsConfigFile) {
// Get or create the deviceId.
const deviceId = config.deviceId ?? randomUUID();
if (config.deviceId === undefined) {
Expand All @@ -82,17 +86,13 @@ export function getDeviceId() {

/**
* Returns the ID of the current user, which will be sent with each event.
*
* Note: This is modified to look up the id from the cache only
* as we have no access to user auth token to fetch the data.
*/
export function getUserId() {
return getConfigCache<{ userId: string }>(USER_ID_CACHE_PATH).userId;
}

/**
* Generate a new session ID.
* @returns
*/
export function getSessionId() {
return randomUUID();
Expand Down
9 changes: 4 additions & 5 deletions packages/create-cloudflare/src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@ export function createReporter() {
appendMetricsData: AppendMetricsDataFn;
}>();

const telemetry = getC3Permission();
const sessionId = getSessionId();
const deviceId = getDeviceId();
const config = readMetricsConfig();
const telemetry = getC3Permission(config);
const deviceId = getDeviceId(config);
const os = {
platform: process.platform,
arch: process.arch,
Expand Down Expand Up @@ -320,9 +321,7 @@ export function initializeC3Permission(enabled = true) {
};
}

export function getC3Permission() {
const config = readMetricsConfig() ?? {};

export function getC3Permission(config = readMetricsConfig() ?? {}) {
if (!config.c3permission) {
config.c3permission = initializeC3Permission();

Expand Down

0 comments on commit 502cb70

Please sign in to comment.