|
1 | | -/* eslint-disable ember/new-module-imports, prettier/prettier */ |
2 | | -/* global require Ember */ |
| 1 | +/* eslint-disable no-empty */ |
| 2 | +/* global require */ |
3 | 3 |
|
4 | | -const LOG_LIMIT = 100; |
5 | | - |
6 | | -(function(){ |
7 | | - self.deprecationWorkflow = self.deprecationWorkflow || {}; |
8 | | - self.deprecationWorkflow.deprecationLog = { |
9 | | - messages: { } |
10 | | - }; |
11 | | - self.deprecationWorkflow.logCounts = {}; |
12 | | - |
13 | | - function detectWorkflow(config, message, options) { |
14 | | - if (!config || !config.workflow) { |
15 | | - return; |
16 | | - } |
17 | | - |
18 | | - let i, workflow, matcher, idMatcher; |
19 | | - for (i=0; i<config.workflow.length; i++) { |
20 | | - workflow = config.workflow[i]; |
21 | | - matcher = workflow.matchMessage; |
22 | | - idMatcher = workflow.matchId; |
23 | | - |
24 | | - if (typeof idMatcher === 'string' && options && idMatcher === options.id) { |
25 | | - return workflow; |
26 | | - } else if (typeof matcher === 'string' && matcher === message) { |
27 | | - return workflow; |
28 | | - } else if (matcher instanceof RegExp && matcher.exec(message)) { |
29 | | - return workflow; |
30 | | - } |
31 | | - } |
32 | | - } |
33 | | - |
34 | | - let registerDeprecationHandler = require.has('@ember/debug') ? require('@ember/debug').registerDeprecationHandler : Ember.Debug.registerDeprecationHandler; |
35 | | - |
36 | | - registerDeprecationHandler(function handleDeprecationWorkflow(message, options, next){ |
37 | | - let config = self.deprecationWorkflow.config || {}; |
38 | | - |
39 | | - let matchingWorkflow = detectWorkflow(config, message, options); |
40 | | - if (!matchingWorkflow) { |
41 | | - if (config && config.throwOnUnhandled) { |
42 | | - throw new Error(message); |
43 | | - } else { |
44 | | - next(message, options); |
45 | | - } |
46 | | - } else { |
47 | | - switch(matchingWorkflow.handler) { |
48 | | - case 'silence': |
49 | | - // no-op |
50 | | - break; |
51 | | - case 'log': { |
52 | | - let key = (options && options.id) || message; |
53 | | - let count = self.deprecationWorkflow.logCounts[key] || 0; |
54 | | - self.deprecationWorkflow.logCounts[key] = ++count; |
55 | | - |
56 | | - if (count <= LOG_LIMIT) { |
57 | | - console.warn('DEPRECATION: ' + message); |
58 | | - if (count === LOG_LIMIT) { |
59 | | - console.warn('To avoid console overflow, this deprecation will not be logged any more in this run.'); |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - break; |
64 | | - } |
65 | | - case 'throw': |
66 | | - throw new Error(message); |
67 | | - default: |
68 | | - next(message, options); |
69 | | - break; |
70 | | - } |
71 | | - } |
72 | | - }); |
73 | | - |
74 | | - registerDeprecationHandler(function deprecationCollector(message, options, next){ |
75 | | - let key = options && options.id || message; |
76 | | - let matchKey = options && key === options.id ? 'matchId' : 'matchMessage'; |
77 | | - |
78 | | - self.deprecationWorkflow.deprecationLog.messages[key] = ' { handler: "silence", ' + matchKey + ': ' + JSON.stringify(key) + ' }'; |
79 | | - next(message, options); |
80 | | - }); |
81 | | - |
82 | | - let preamble = [ |
83 | | - 'self.deprecationWorkflow = self.deprecationWorkflow || {};', |
84 | | - 'self.deprecationWorkflow.config = {\n workflow: [\n', |
85 | | - ].join('\n'); |
86 | | - |
87 | | - let postamble = [ |
88 | | - ' ]\n};' |
89 | | - ].join('\n'); |
90 | | - |
91 | | - self.deprecationWorkflow.flushDeprecations = function flushDeprecations() { |
92 | | - let messages = self.deprecationWorkflow.deprecationLog.messages; |
93 | | - let logs = []; |
94 | | - |
95 | | - for (let message in messages) { |
96 | | - logs.push(messages[message]); |
97 | | - } |
98 | | - |
99 | | - let deprecations = logs.join(',\n') + '\n'; |
100 | | - |
101 | | - return preamble + deprecations + postamble; |
102 | | - }; |
| 4 | +(function () { |
| 5 | + try { |
| 6 | + require('ember-cli-deprecation-workflow').default(); |
| 7 | + } catch (e) {} |
103 | 8 | })(); |
0 commit comments