Skip to content

Commit

Permalink
Improve the Embroider and non-Embroider stories
Browse files Browse the repository at this point in the history
  • Loading branch information
lolmaus committed Jan 25, 2024
1 parent 2b37d81 commit c8e0206
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 108 deletions.
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,43 @@ addressing a single deprecation at a time, and prevents backsliding

### Getting started

The initial steps needed to get started:
#### Normal setup routine

1. Install the ember-cli-deprecation-workflow addon (`ember install ember-cli-deprecation-workflow`).
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:
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 a new `app/deprecation-workflow.js` file in your project.
6. In your `app/app.js`, do:

```js
import './deprecation-workflow';
```

#### Setup routine for Embroider in strict mode

1. Install the ember-cli-deprecation-workflow addon (`ember install ember-cli-deprecation-workflow`).
2. Create an `app/deprecation-workflow.js` file with the following content:

```js
import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
setupDeprecationWorkflow();
```

3. In your `app/app.js`, do:

```js
import './deprecation-workflow';
```

4. Run your test suite\* with `ember test --server`.
5. Navigate to your tests (default: http://localhost:7357/)
6. Run `deprecationWorkflow.flushDeprecations()` in your browsers console.
7. Copy the string output and overwrite the content of `app/deprecation-workflow.js`.

In Chrome, use right click → "Copy string contents" to avoid escape characters.

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

Expand Down
50 changes: 50 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,54 @@

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 });
},
};
106 changes: 4 additions & 102 deletions vendor/ember-cli-deprecation-workflow/main.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,5 @@
/* eslint-disable ember/new-module-imports, prettier/prettier */
/* global require Ember */

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

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

let registerDeprecationHandler = require.has('@ember/debug') ? require('@ember/debug').registerDeprecationHandler : Ember.Debug.registerDeprecationHandler;

registerDeprecationHandler(function handleDeprecationWorkflow(message, options, next){
let config = self.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 = 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;
}
}
});

registerDeprecationHandler(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);
});

let preamble = [
'self.deprecationWorkflow = self.deprecationWorkflow || {};',
'self.deprecationWorkflow.config = {\n workflow: [\n',
].join('\n');

let postamble = [
' ]\n};'
].join('\n');

self.deprecationWorkflow.flushDeprecations = 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;
};
(function () {
try {
require('ember-cli-deprecation-workflow').default();
} catch (e) {}
})();

0 comments on commit c8e0206

Please sign in to comment.