UNPKG

14.3 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.getSharedAnalytics = exports.getWorkspaceAnalytics = exports.hasWorkspaceAnalyticsConfiguration = exports.getGlobalAnalytics = exports.hasGlobalAnalyticsConfiguration = exports.promptProjectAnalytics = exports.promptGlobalAnalytics = exports.setAnalyticsConfig = exports.isPackageNameSafeForAnalytics = exports.analyticsPackageSafelist = exports.AnalyticsProperties = void 0;
11const core_1 = require("@angular-devkit/core");
12const debug = require("debug");
13const inquirer = require("inquirer");
14const uuid_1 = require("uuid");
15const color_1 = require("../utilities/color");
16const config_1 = require("../utilities/config");
17const tty_1 = require("../utilities/tty");
18const analytics_collector_1 = require("./analytics-collector");
19/* eslint-disable no-console */
20const analyticsDebug = debug('ng:analytics'); // Generate analytics, including settings and users.
21let _defaultAngularCliPropertyCache;
22exports.AnalyticsProperties = {
23 AngularCliProd: 'UA-8594346-29',
24 AngularCliStaging: 'UA-8594346-32',
25 get AngularCliDefault() {
26 if (_defaultAngularCliPropertyCache) {
27 return _defaultAngularCliPropertyCache;
28 }
29 const v = require('../package.json').version;
30 // The logic is if it's a full version then we should use the prod GA property.
31 if (/^\d+\.\d+\.\d+$/.test(v) && v !== '0.0.0') {
32 _defaultAngularCliPropertyCache = exports.AnalyticsProperties.AngularCliProd;
33 }
34 else {
35 _defaultAngularCliPropertyCache = exports.AnalyticsProperties.AngularCliStaging;
36 }
37 return _defaultAngularCliPropertyCache;
38 },
39};
40/**
41 * This is the ultimate safelist for checking if a package name is safe to report to analytics.
42 */
43exports.analyticsPackageSafelist = [
44 /^@angular\//,
45 /^@angular-devkit\//,
46 /^@ngtools\//,
47 '@schematics/angular',
48];
49function isPackageNameSafeForAnalytics(name) {
50 return exports.analyticsPackageSafelist.some((pattern) => {
51 if (typeof pattern == 'string') {
52 return pattern === name;
53 }
54 else {
55 return pattern.test(name);
56 }
57 });
58}
59exports.isPackageNameSafeForAnalytics = isPackageNameSafeForAnalytics;
60/**
61 * Set analytics settings. This does not work if the user is not inside a project.
62 * @param level Which config to use. "global" for user-level, and "local" for project-level.
63 * @param value Either a user ID, true to generate a new User ID, or false to disable analytics.
64 */
65function setAnalyticsConfig(level, value) {
66 analyticsDebug('setting %s level analytics to: %s', level, value);
67 const [config, configPath] = config_1.getWorkspaceRaw(level);
68 if (!config || !configPath) {
69 throw new Error(`Could not find ${level} workspace.`);
70 }
71 const cli = config.get(['cli']);
72 if (cli !== undefined && !core_1.json.isJsonObject(cli)) {
73 throw new Error(`Invalid config found at ${configPath}. CLI should be an object.`);
74 }
75 if (value === true) {
76 value = uuid_1.v4();
77 }
78 config.modify(['cli', 'analytics'], value);
79 config.save();
80 analyticsDebug('done');
81}
82exports.setAnalyticsConfig = setAnalyticsConfig;
83/**
84 * Prompt the user for usage gathering permission.
85 * @param force Whether to ask regardless of whether or not the user is using an interactive shell.
86 * @return Whether or not the user was shown a prompt.
87 */
88async function promptGlobalAnalytics(force = false) {
89 analyticsDebug('prompting global analytics.');
90 if (force || tty_1.isTTY()) {
91 const answers = await inquirer.prompt([
92 {
93 type: 'confirm',
94 name: 'analytics',
95 message: core_1.tags.stripIndents `
96 Would you like to share anonymous usage data with the Angular Team at Google under
97 Google’s Privacy Policy at https://policies.google.com/privacy? For more details and
98 how to change this setting, see https://angular.io/analytics.
99 `,
100 default: false,
101 },
102 ]);
103 setAnalyticsConfig('global', answers.analytics);
104 if (answers.analytics) {
105 console.log('');
106 console.log(core_1.tags.stripIndent `
107 Thank you for sharing anonymous usage data. If you change your mind, the following
108 command will disable this feature entirely:
109
110 ${color_1.colors.yellow('ng analytics off')}
111 `);
112 console.log('');
113 // Send back a ping with the user `optin`.
114 const ua = new analytics_collector_1.AnalyticsCollector(exports.AnalyticsProperties.AngularCliDefault, 'optin');
115 ua.pageview('/telemetry/optin');
116 await ua.flush();
117 }
118 else {
119 // Send back a ping with the user `optout`. This is the only thing we send.
120 const ua = new analytics_collector_1.AnalyticsCollector(exports.AnalyticsProperties.AngularCliDefault, 'optout');
121 ua.pageview('/telemetry/optout');
122 await ua.flush();
123 }
124 return true;
125 }
126 else {
127 analyticsDebug('Either STDOUT or STDIN are not TTY and we skipped the prompt.');
128 }
129 return false;
130}
131exports.promptGlobalAnalytics = promptGlobalAnalytics;
132/**
133 * Prompt the user for usage gathering permission for the local project. Fails if there is no
134 * local workspace.
135 * @param force Whether to ask regardless of whether or not the user is using an interactive shell.
136 * @return Whether or not the user was shown a prompt.
137 */
138async function promptProjectAnalytics(force = false) {
139 analyticsDebug('prompting user');
140 const [config, configPath] = config_1.getWorkspaceRaw('local');
141 if (!config || !configPath) {
142 throw new Error(`Could not find a local workspace. Are you in a project?`);
143 }
144 if (force || tty_1.isTTY()) {
145 const answers = await inquirer.prompt([
146 {
147 type: 'confirm',
148 name: 'analytics',
149 message: core_1.tags.stripIndents `
150 Would you like to share anonymous usage data about this project with the Angular Team at
151 Google under Google’s Privacy Policy at https://policies.google.com/privacy? For more
152 details and how to change this setting, see https://angular.io/analytics.
153
154 `,
155 default: false,
156 },
157 ]);
158 setAnalyticsConfig('local', answers.analytics);
159 if (answers.analytics) {
160 console.log('');
161 console.log(core_1.tags.stripIndent `
162 Thank you for sharing anonymous usage data. Would you change your mind, the following
163 command will disable this feature entirely:
164
165 ${color_1.colors.yellow('ng analytics project off')}
166 `);
167 console.log('');
168 // Send back a ping with the user `optin`.
169 const ua = new analytics_collector_1.AnalyticsCollector(exports.AnalyticsProperties.AngularCliDefault, 'optin');
170 ua.pageview('/telemetry/project/optin');
171 await ua.flush();
172 }
173 else {
174 // Send back a ping with the user `optout`. This is the only thing we send.
175 const ua = new analytics_collector_1.AnalyticsCollector(exports.AnalyticsProperties.AngularCliDefault, 'optout');
176 ua.pageview('/telemetry/project/optout');
177 await ua.flush();
178 }
179 return true;
180 }
181 return false;
182}
183exports.promptProjectAnalytics = promptProjectAnalytics;
184async function hasGlobalAnalyticsConfiguration() {
185 try {
186 const globalWorkspace = await config_1.getWorkspace('global');
187 const analyticsConfig = globalWorkspace && globalWorkspace.getCli() && globalWorkspace.getCli()['analytics'];
188 if (analyticsConfig !== null && analyticsConfig !== undefined) {
189 return true;
190 }
191 }
192 catch { }
193 return false;
194}
195exports.hasGlobalAnalyticsConfiguration = hasGlobalAnalyticsConfiguration;
196/**
197 * Get the global analytics object for the user. This returns an instance of UniversalAnalytics,
198 * or undefined if analytics are disabled.
199 *
200 * If any problem happens, it is considered the user has been opting out of analytics.
201 */
202async function getGlobalAnalytics() {
203 analyticsDebug('getGlobalAnalytics');
204 const propertyId = exports.AnalyticsProperties.AngularCliDefault;
205 if ('NG_CLI_ANALYTICS' in process.env) {
206 if (process.env['NG_CLI_ANALYTICS'] == 'false' || process.env['NG_CLI_ANALYTICS'] == '') {
207 analyticsDebug('NG_CLI_ANALYTICS is false');
208 return undefined;
209 }
210 if (process.env['NG_CLI_ANALYTICS'] === 'ci') {
211 analyticsDebug('Running in CI mode');
212 return new analytics_collector_1.AnalyticsCollector(propertyId, 'ci');
213 }
214 }
215 // If anything happens we just keep the NOOP analytics.
216 try {
217 const globalWorkspace = await config_1.getWorkspace('global');
218 const analyticsConfig = globalWorkspace && globalWorkspace.getCli() && globalWorkspace.getCli()['analytics'];
219 analyticsDebug('Client Analytics config found: %j', analyticsConfig);
220 if (analyticsConfig === false) {
221 analyticsDebug('Analytics disabled. Ignoring all analytics.');
222 return undefined;
223 }
224 else if (analyticsConfig === undefined || analyticsConfig === null) {
225 analyticsDebug('Analytics settings not found. Ignoring all analytics.');
226 // globalWorkspace can be null if there is no file. analyticsConfig would be null in this
227 // case. Since there is no file, the user hasn't answered and the expected return value is
228 // undefined.
229 return undefined;
230 }
231 else {
232 let uid = undefined;
233 if (typeof analyticsConfig == 'string') {
234 uid = analyticsConfig;
235 }
236 else if (typeof analyticsConfig == 'object' && typeof analyticsConfig['uid'] == 'string') {
237 uid = analyticsConfig['uid'];
238 }
239 analyticsDebug('client id: %j', uid);
240 if (uid == undefined) {
241 return undefined;
242 }
243 return new analytics_collector_1.AnalyticsCollector(propertyId, uid);
244 }
245 }
246 catch (err) {
247 analyticsDebug('Error happened during reading of analytics config: %s', err.message);
248 return undefined;
249 }
250}
251exports.getGlobalAnalytics = getGlobalAnalytics;
252async function hasWorkspaceAnalyticsConfiguration() {
253 try {
254 const globalWorkspace = await config_1.getWorkspace('local');
255 const analyticsConfig = globalWorkspace && globalWorkspace.getCli() && globalWorkspace.getCli()['analytics'];
256 if (analyticsConfig !== undefined) {
257 return true;
258 }
259 }
260 catch { }
261 return false;
262}
263exports.hasWorkspaceAnalyticsConfiguration = hasWorkspaceAnalyticsConfiguration;
264/**
265 * Get the workspace analytics object for the user. This returns an instance of AnalyticsCollector,
266 * or undefined if analytics are disabled.
267 *
268 * If any problem happens, it is considered the user has been opting out of analytics.
269 */
270async function getWorkspaceAnalytics() {
271 analyticsDebug('getWorkspaceAnalytics');
272 try {
273 const globalWorkspace = await config_1.getWorkspace('local');
274 const analyticsConfig = globalWorkspace === null || globalWorkspace === void 0 ? void 0 : globalWorkspace.getCli()['analytics'];
275 analyticsDebug('Workspace Analytics config found: %j', analyticsConfig);
276 if (analyticsConfig === false) {
277 analyticsDebug('Analytics disabled. Ignoring all analytics.');
278 return undefined;
279 }
280 else if (analyticsConfig === undefined || analyticsConfig === null) {
281 analyticsDebug('Analytics settings not found. Ignoring all analytics.');
282 return undefined;
283 }
284 else {
285 let uid = undefined;
286 if (typeof analyticsConfig == 'string') {
287 uid = analyticsConfig;
288 }
289 else if (typeof analyticsConfig == 'object' && typeof analyticsConfig['uid'] == 'string') {
290 uid = analyticsConfig['uid'];
291 }
292 analyticsDebug('client id: %j', uid);
293 if (uid == undefined) {
294 return undefined;
295 }
296 return new analytics_collector_1.AnalyticsCollector(exports.AnalyticsProperties.AngularCliDefault, uid);
297 }
298 }
299 catch (err) {
300 analyticsDebug('Error happened during reading of analytics config: %s', err.message);
301 return undefined;
302 }
303}
304exports.getWorkspaceAnalytics = getWorkspaceAnalytics;
305/**
306 * Return the usage analytics sharing setting, which is either a property string (GA-XXXXXXX-XX),
307 * or undefined if no sharing.
308 */
309async function getSharedAnalytics() {
310 analyticsDebug('getSharedAnalytics');
311 const envVarName = 'NG_CLI_ANALYTICS_SHARE';
312 if (envVarName in process.env) {
313 if (process.env[envVarName] == 'false' || process.env[envVarName] == '') {
314 analyticsDebug('NG_CLI_ANALYTICS is false');
315 return undefined;
316 }
317 }
318 // If anything happens we just keep the NOOP analytics.
319 try {
320 const globalWorkspace = await config_1.getWorkspace('global');
321 const analyticsConfig = globalWorkspace === null || globalWorkspace === void 0 ? void 0 : globalWorkspace.getCli()['analyticsSharing'];
322 if (!analyticsConfig || !analyticsConfig.tracking || !analyticsConfig.uuid) {
323 return undefined;
324 }
325 else {
326 analyticsDebug('Analytics sharing info: %j', analyticsConfig);
327 return new analytics_collector_1.AnalyticsCollector(analyticsConfig.tracking, analyticsConfig.uuid);
328 }
329 }
330 catch (err) {
331 analyticsDebug('Error happened during reading of analytics sharing config: %s', err.message);
332 return undefined;
333 }
334}
335exports.getSharedAnalytics = getSharedAnalytics;