Skip to content

Commit cf03daf

Browse files
committed
fix(@angular/cli): generate new random user ID when passing empty string to uuid
`ng config cli.analyticsSharing.uuid ""` should generate new random user ID. See: https://angular.io/cli/usage-analytics-gathering#per-user-tracking (cherry picked from commit 3f72ccc)
1 parent ded5105 commit cf03daf

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

packages/angular/cli/commands/config-impl.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const validCliPaths = new Map<
2424
['cli.analytics', undefined],
2525

2626
['cli.analyticsSharing.tracking', undefined],
27-
['cli.analyticsSharing.uuid', (v) => (v ? `${v}` : uuidV4())],
27+
['cli.analyticsSharing.uuid', (v) => (v === '' ? uuidV4() : `${v}`)],
2828

2929
['cli.cache.enabled', undefined],
3030
['cli.cache.environment', undefined],
@@ -84,7 +84,15 @@ function normalizeValue(value: string | undefined | boolean | number): JsonValue
8484
return +valueString;
8585
}
8686

87-
return parseJson(valueString) ?? value ?? undefined;
87+
try {
88+
// We use `JSON.parse` instead of `parseJson` because the latter will parse UUIDs
89+
// and convert them into a numberic entities.
90+
// Example: 73b61974-182c-48e4-b4c6-30ddf08c5c98 -> 73.
91+
// These values should never contain comments, therefore using `JSON.parse` is safe.
92+
return JSON.parse(valueString);
93+
} catch {
94+
return value;
95+
}
8896
}
8997

9098
export class ConfigCommand extends Command<ConfigCommandSchema> {

packages/angular/cli/lib/config/workspace-schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
},
7474
"uuid": {
7575
"description": "Analytics sharing info universally unique identifier.",
76-
"type": "string"
76+
"type": "string",
77+
"format": "uuid"
7778
}
7879
}
7980
},

tests/legacy-cli/e2e/tests/commands/config/config-set.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,17 @@ export default async function () {
2424
await ng('config', 'schematics');
2525
await ng('config', 'schematics', 'undefined');
2626
await expectToFail(() => ng('config', 'schematics'));
27+
28+
/**
29+
* `ng config cli.analyticsSharing.uuid ""` should generate new random user ID.
30+
* @see: https://angular.io/cli/usage-analytics-gathering#per-user-tracking
31+
*/
32+
await ng('config', 'cli.analyticsSharing.uuid', '');
33+
const { stdout: stdout4 } = await ng('config', 'cli.analyticsSharing.uuid');
34+
console.log(stdout4);
35+
if (!/(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}/i.test(stdout4)) {
36+
throw new Error(
37+
`Expected "cli.analyticsSharing.uuid" to be a UUID, received "${JSON.stringify(stdout4)}".`,
38+
);
39+
}
2740
}

0 commit comments

Comments
 (0)