Skip to content

Commit

Permalink
[BREAKING] Convert to a module, drops support for Ember < 3.28
Browse files Browse the repository at this point in the history
  • Loading branch information
lolmaus committed Oct 26, 2023
1 parent ea150f5 commit 92ff135
Show file tree
Hide file tree
Showing 9 changed files with 526 additions and 417 deletions.
3 changes: 0 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
## v2.0.0 (2021-07-04)


## v2.0.0-beta.5 (2021-07-04)

#### :bug: Bug Fix
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ addressing a single deprecation at a time, and prevents backsliding
The initial steps needed to get started:

1. Install the ember-cli-deprecation-workflow addon (`ember install ember-cli-deprecation-workflow`).
2. Run your test suite\* with `ember test --server`.
3. Navigate to your tests (default: http://localhost:7357/)
4. Run `deprecationWorkflow.flushDeprecations()` from your browsers console.
5. Copy the string output into `config/deprecation-workflow.js` in your project.
3. Run your test suite\* with `ember test --server`.
4. Navigate to your tests (default: http://localhost:7357/)
5. Run `deprecationWorkflow.flushDeprecations()` from your browsers console.
6. Copy the string output into `app/deprecation-workflow.js` in your project.
7. In your `app/app.js`, do:
```js
import './deprecation-workflow';
```

Once this initial setup is completed the "deprecation spew" should be largely
"fixed". Only unhandled deprecations will be displayed in your console.
Expand Down
114 changes: 114 additions & 0 deletions addon/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { registerDeprecationHandler } from '@ember/debug';

const LOG_LIMIT = 100;

export default function setupDeprecationWorkflow(config) {
self.deprecationWorkflow = self.deprecationWorkflow || {};
self.deprecationWorkflow.deprecationLog = {
messages: {},
};

registerDeprecationHandler((message, options, next) =>
handleDeprecationWorkflow(config, message, options, next)
);

registerDeprecationHandler(deprecationCollector);

self.deprecationWorkflow.flushDeprecations = flushDeprecations;
}

let preamble = `import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
setupDeprecationWorkflow({
workflow: [
`;

let postamble = ` ]
});`;

export 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;
}
}
}

export function flushDeprecations() {
let messages = self.deprecationWorkflow.deprecationLog.messages;
let logs = [];

for (let message in messages) {
logs.push(messages[message]);
}

let deprecations = logs.join(',\n') + '\n';

return preamble + deprecations + postamble;
}

export function handleDeprecationWorkflow(config, message, options, next) {
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;

if (!self.deprecationWorkflow.logCounts) {
self.deprecationWorkflow.logCounts = {};
}

let count = self.deprecationWorkflow.logCounts[key] || 0;
self.deprecationWorkflow.logCounts[key] = ++count;

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;
}
}
}

export function deprecationCollector(message, options, next) {
let key = (options && options.id) || message;
let matchKey = options && key === options.id ? 'matchId' : 'matchMessage';

self.deprecationWorkflow.deprecationLog.messages[key] =
' { handler: "silence", ' + matchKey + ': ' + JSON.stringify(key) + ' }';

next(message, options);
}
50 changes: 0 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,4 @@

module.exports = {
name: require('./package').name,

_shouldInclude() {
// the presence of `this.app.tests` shows that we are in one of:
//
// * running non-production build
// * running tests against production
//
var app = this.app || this._findHost();
let addonOptions = app.options['ember-cli-deprecation-workflow'];

if (addonOptions) {
return addonOptions.enabled;
} else {
return app.tests;
}
},

included() {
// From https://github.com/rwjblue/ember-debug-handlers-polyfill/blob/master/index.js
var app = this.app || this._findHost();

if (this._shouldInclude()) {
app.import(
'vendor/ember-cli-deprecation-workflow/deprecation-workflow.js'
);
app.import('vendor/ember-cli-deprecation-workflow/main.js');
}
},

treeForVendor(tree) {
var root = process.env._DUMMY_CONFIG_ROOT_PATH || this.project.root;
var configDir = '/config';

if (
this.project.pkg['ember-addon'] &&
this.project.pkg['ember-addon']['configPath']
) {
configDir = '/' + this.project.pkg['ember-addon']['configPath'];
}

var mergeTrees = require('broccoli-merge-trees');
var Funnel = require('broccoli-funnel');
var configTree = new Funnel(root + configDir, {
include: ['deprecation-workflow.js'],

destDir: 'ember-cli-deprecation-workflow',
});

return mergeTrees([tree, configTree], { overwrite: true });
},
};
1 change: 1 addition & 0 deletions tests/dummy/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Application from '@ember/application';
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from 'dummy/config/environment';
import './deprecation-workflow';

export default class App extends Application {
modulePrefix = config.modulePrefix;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* globals self */
self.deprecationWorkflow = self.deprecationWorkflow || {};
self.deprecationWorkflow.config = {
import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';

setupDeprecationWorkflow({
throwOnUnhandled: true,
workflow: [
/*
Expand All @@ -26,4 +26,4 @@ self.deprecationWorkflow.config = {

{ matchMessage: 'throw-strict', handler: 'throw' },
],
};
});

0 comments on commit 92ff135

Please sign in to comment.