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