diff --git a/addon/index.js b/addon/index.js new file mode 100644 index 0000000..cf182e9 --- /dev/null +++ b/addon/index.js @@ -0,0 +1,126 @@ +import { registerDeprecationHandler } from '@ember/debug'; + +const LOG_LIMIT = 100; + +let hasSetup = false; + +export function setup(scope) { + if (hasSetup) { + throw new Error( + 'setup of ember-cli-deprecation-workflow must only be called once' + ); + } + hasSetup = true; + + scope.deprecationWorkflow = scope.deprecationWorkflow || {}; + scope.deprecationWorkflow.deprecationLog = { + messages: {}, + }; + scope.deprecationWorkflow.logCounts = {}; + + function detectWorkflow(config, message, options) { + if (!config || !config.workflow) { + return; + } + + let i, workflow, matcher, idMatcher; + for (i = 0; i < config.workflow.length; i++) { + workflow = config.workflow[i]; + matcher = workflow.matchMessage; + idMatcher = workflow.matchId; + + if ( + typeof idMatcher === 'string' && + options && + idMatcher === options.id + ) { + return workflow; + } else if (typeof matcher === 'string' && matcher === message) { + return workflow; + } else if (matcher instanceof RegExp && matcher.exec(message)) { + return workflow; + } + } + } + + registerDeprecationHandler(function handleDeprecationWorkflow( + message, + options, + next + ) { + let config = scope.deprecationWorkflow.config || {}; + + let matchingWorkflow = detectWorkflow(config, message, options); + if (!matchingWorkflow) { + if (config && config.throwOnUnhandled) { + throw new Error(message); + } else { + next(message, options); + } + } else { + switch (matchingWorkflow.handler) { + case 'silence': + // no-op + break; + case 'log': { + let key = (options && options.id) || message; + let count = scope.deprecationWorkflow.logCounts[key] || 0; + scope.deprecationWorkflow.logCounts[key] = count + 1; + + if (count <= LOG_LIMIT) { + console.warn('DEPRECATION: ' + message); + if (count === LOG_LIMIT) { + console.warn( + 'To avoid console overflow, this deprecation will not be logged any more in this run.' + ); + } + } + + break; + } + case 'throw': + throw new Error(message); + default: + next(message, options); + break; + } + } + }); + + registerDeprecationHandler(function deprecationCollector( + message, + options, + next + ) { + let key = (options && options.id) || message; + let matchKey = options && key === options.id ? 'matchId' : 'matchMessage'; + + scope.deprecationWorkflow.deprecationLog.messages[key] = + ' { handler: "silence", ' + + matchKey + + ': ' + + JSON.stringify(key) + + ' }'; + next(message, options); + }); + + let preamble = [ + 'self.deprecationWorkflow = self.deprecationWorkflow || {};', + 'self.deprecationWorkflow.config = {\n workflow: [\n', + ].join('\n'); + + let postamble = [' ]\n};'].join('\n'); + + scope.deprecationWorkflow.flushDeprecations = function flushDeprecations() { + let messages = scope.deprecationWorkflow.deprecationLog.messages; + let logs = []; + + for (let message in messages) { + logs.push(messages[message]); + } + + let deprecations = logs.join(',\n') + '\n'; + + return preamble + deprecations + postamble; + }; +} diff --git a/index.js b/index.js index 89e8376..ae38fa8 100644 --- a/index.js +++ b/index.js @@ -27,7 +27,6 @@ module.exports = { app.import( 'vendor/ember-cli-deprecation-workflow/deprecation-workflow.js' ); - app.import('vendor/ember-cli-deprecation-workflow/main.js'); } }, diff --git a/package.json b/package.json index 7845b68..09f5d29 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "broccoli-funnel": "^3.0.3", "broccoli-merge-trees": "^4.2.0", "broccoli-plugin": "^4.0.5", + "ember-cli-babel": "^7.26.6", "ember-cli-htmlbars": "^5.3.2" }, "devDependencies": { @@ -39,7 +40,6 @@ "broccoli-asset-rev": "^3.0.0", "ember-auto-import": "^1.10.1", "ember-cli": "~3.25.0", - "ember-cli-babel": "^7.23.1", "ember-cli-dependency-checker": "^3.2.0", "ember-cli-inject-live-reload": "^2.0.2", "ember-cli-sri": "^2.1.1", diff --git a/tests/test-helper.js b/tests/test-helper.js index 4eae06f..0da103e 100644 --- a/tests/test-helper.js +++ b/tests/test-helper.js @@ -2,7 +2,10 @@ import Application from 'dummy/app'; import config from 'dummy/config/environment'; import { setApplication } from '@ember/test-helpers'; import { start } from 'ember-qunit'; +import { setup as setupDeprecationWorkflow } from 'ember-cli-deprecation-workflow'; setApplication(Application.create(config.APP)); +setupDeprecationWorkflow(self); + start(); diff --git a/vendor/ember-cli-deprecation-workflow/main.js b/vendor/ember-cli-deprecation-workflow/main.js deleted file mode 100644 index 0c0125a..0000000 --- a/vendor/ember-cli-deprecation-workflow/main.js +++ /dev/null @@ -1,100 +0,0 @@ -/* globals self */ - -const LOG_LIMIT = 100; - -(function(){ - self.deprecationWorkflow = self.deprecationWorkflow || {}; - self.deprecationWorkflow.deprecationLog = { - messages: { } - }; - self.deprecationWorkflow.logCounts = {}; - - function detectWorkflow(config, message, options) { - if (!config || !config.workflow) { - return; - } - - var i, workflow, regex, matcher, idMatcher; - for (i=0; i