Skip to content

Commit d2e24d3

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
1 parent 73a47af commit d2e24d3

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const validCliPaths = new Map<
2424

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

3030
/**
@@ -80,7 +80,15 @@ function normalizeValue(value: string | undefined | boolean | number): JsonValue
8080
return +valueString;
8181
}
8282

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

8694
export class ConfigCommand extends Command<ConfigCommandSchema> {

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

+2-1
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

+13
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)