Skip to content

Commit 92ff135

Browse files
committed
[BREAKING] Convert to a module, drops support for Ember < 3.28
1 parent ea150f5 commit 92ff135

File tree

9 files changed

+526
-417
lines changed

9 files changed

+526
-417
lines changed

CHANGELOG.md

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
## v2.0.0 (2021-07-04)
2-
3-
41
## v2.0.0-beta.5 (2021-07-04)
52

63
#### :bug: Bug Fix

README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ addressing a single deprecation at a time, and prevents backsliding
4040
The initial steps needed to get started:
4141

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

4852
Once this initial setup is completed the "deprecation spew" should be largely
4953
"fixed". Only unhandled deprecations will be displayed in your console.

addon/index.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

index.js

-50
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,4 @@
22

33
module.exports = {
44
name: require('./package').name,
5-
6-
_shouldInclude() {
7-
// the presence of `this.app.tests` shows that we are in one of:
8-
//
9-
// * running non-production build
10-
// * running tests against production
11-
//
12-
var app = this.app || this._findHost();
13-
let addonOptions = app.options['ember-cli-deprecation-workflow'];
14-
15-
if (addonOptions) {
16-
return addonOptions.enabled;
17-
} else {
18-
return app.tests;
19-
}
20-
},
21-
22-
included() {
23-
// From https://github.com/rwjblue/ember-debug-handlers-polyfill/blob/master/index.js
24-
var app = this.app || this._findHost();
25-
26-
if (this._shouldInclude()) {
27-
app.import(
28-
'vendor/ember-cli-deprecation-workflow/deprecation-workflow.js'
29-
);
30-
app.import('vendor/ember-cli-deprecation-workflow/main.js');
31-
}
32-
},
33-
34-
treeForVendor(tree) {
35-
var root = process.env._DUMMY_CONFIG_ROOT_PATH || this.project.root;
36-
var configDir = '/config';
37-
38-
if (
39-
this.project.pkg['ember-addon'] &&
40-
this.project.pkg['ember-addon']['configPath']
41-
) {
42-
configDir = '/' + this.project.pkg['ember-addon']['configPath'];
43-
}
44-
45-
var mergeTrees = require('broccoli-merge-trees');
46-
var Funnel = require('broccoli-funnel');
47-
var configTree = new Funnel(root + configDir, {
48-
include: ['deprecation-workflow.js'],
49-
50-
destDir: 'ember-cli-deprecation-workflow',
51-
});
52-
53-
return mergeTrees([tree, configTree], { overwrite: true });
54-
},
555
};

tests/dummy/app/app.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Application from '@ember/application';
22
import Resolver from 'ember-resolver';
33
import loadInitializers from 'ember-load-initializers';
44
import config from 'dummy/config/environment';
5+
import './deprecation-workflow';
56

67
export default class App extends Application {
78
modulePrefix = config.modulePrefix;

tests/dummy/config/deprecation-workflow.js tests/dummy/app/deprecation-workflow.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/* globals self */
2-
self.deprecationWorkflow = self.deprecationWorkflow || {};
3-
self.deprecationWorkflow.config = {
1+
import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
2+
3+
setupDeprecationWorkflow({
44
throwOnUnhandled: true,
55
workflow: [
66
/*
@@ -26,4 +26,4 @@ self.deprecationWorkflow.config = {
2626

2727
{ matchMessage: 'throw-strict', handler: 'throw' },
2828
],
29-
};
29+
});

0 commit comments

Comments
 (0)