1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.getSharedAnalytics = exports.getWorkspaceAnalytics = exports.hasWorkspaceAnalyticsConfiguration = exports.getGlobalAnalytics = exports.hasGlobalAnalyticsConfiguration = exports.promptProjectAnalytics = exports.promptGlobalAnalytics = exports.setAnalyticsConfig = exports.isPackageNameSafeForAnalytics = exports.analyticsPackageSafelist = exports.AnalyticsProperties = void 0;
|
11 | const core_1 = require("@angular-devkit/core");
|
12 | const debug = require("debug");
|
13 | const inquirer = require("inquirer");
|
14 | const uuid_1 = require("uuid");
|
15 | const color_1 = require("../utilities/color");
|
16 | const config_1 = require("../utilities/config");
|
17 | const tty_1 = require("../utilities/tty");
|
18 | const analytics_collector_1 = require("./analytics-collector");
|
19 |
|
20 | const analyticsDebug = debug('ng:analytics');
|
21 | let _defaultAngularCliPropertyCache;
|
22 | exports.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 |
|
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 |
|
42 |
|
43 | exports.analyticsPackageSafelist = [
|
44 | /^@angular\
|
45 | /^@angular-devkit\
|
46 | /^@ngtools\
|
47 | '@schematics/angular',
|
48 | ];
|
49 | function 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 | }
|
59 | exports.isPackageNameSafeForAnalytics = isPackageNameSafeForAnalytics;
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function 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 | }
|
82 | exports.setAnalyticsConfig = setAnalyticsConfig;
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | async 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 |
|
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 |
|
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 | }
|
131 | exports.promptGlobalAnalytics = promptGlobalAnalytics;
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | async 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 |
|
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 |
|
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 | }
|
183 | exports.promptProjectAnalytics = promptProjectAnalytics;
|
184 | async 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 | }
|
195 | exports.hasGlobalAnalyticsConfiguration = hasGlobalAnalyticsConfiguration;
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | async 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 |
|
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 |
|
227 |
|
228 |
|
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 | }
|
251 | exports.getGlobalAnalytics = getGlobalAnalytics;
|
252 | async 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 | }
|
263 | exports.hasWorkspaceAnalyticsConfiguration = hasWorkspaceAnalyticsConfiguration;
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | async 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 | }
|
304 | exports.getWorkspaceAnalytics = getWorkspaceAnalytics;
|
305 |
|
306 |
|
307 |
|
308 |
|
309 | async 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 |
|
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 | }
|
335 | exports.getSharedAnalytics = getSharedAnalytics;
|