-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
442 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { registerDeprecationHandler } from '@ember/debug'; | ||
import { macroCondition, isDevelopingApp, isTesting } from '@embroider/macros'; | ||
|
||
export function initialize() { | ||
if (macroCondition(isDevelopingApp() || isTesting())) { | ||
setupDeprecationWorkflow(); | ||
} | ||
} | ||
|
||
export default { | ||
initialize, | ||
}; | ||
|
||
function setupDeprecationWorkflow() { | ||
self.deprecationWorkflow = self.deprecationWorkflow || {}; | ||
self.deprecationWorkflow.deprecationLog = { | ||
messages: {}, | ||
}; | ||
|
||
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 = 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': | ||
console.warn('DEPRECATION: ' + message); | ||
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { | ||
default, | ||
initialize, | ||
} from 'ember-cli-deprecation-workflow/initializers/ember-cli-deprecation-workflow'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.