From dd0e72a80b0428e4e435c05c14ab6f0a09f3a1d2 Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Sat, 17 Feb 2024 11:58:34 +0000 Subject: [PATCH] Fix initialiser location --- .../addon/initializers/ember-cli-mirage.js | 62 +++++++++++++++++++ .../app/initializers/ember-cli-mirage.js | 62 ++----------------- packages/ember-cli-mirage/index.js | 6 +- 3 files changed, 69 insertions(+), 61 deletions(-) create mode 100644 packages/ember-cli-mirage/addon/initializers/ember-cli-mirage.js diff --git a/packages/ember-cli-mirage/addon/initializers/ember-cli-mirage.js b/packages/ember-cli-mirage/addon/initializers/ember-cli-mirage.js new file mode 100644 index 000000000..5630899f4 --- /dev/null +++ b/packages/ember-cli-mirage/addon/initializers/ember-cli-mirage.js @@ -0,0 +1,62 @@ +import getRfc232TestContext from 'ember-cli-mirage/get-rfc232-test-context'; +import startMirageImpl from 'ember-cli-mirage/start-mirage'; +import * as config from '../mirage/config'; +const { default: makeServer } = config; + +// +// This initializer does two things: +// +// 1. Pulls the mirage config objects from the application's config and +// registers them in the container so `ember-cli-mirage/start-mirage` can +// find them (since it doesn't have access to the app's namespace). +// 2. Provides legacy support for auto-starting mirage in pre-rfc268 acceptance +// tests. +// +export function initialize(application) { + if (makeServer) { + application.register('mirage:make-server', makeServer, { + instantiate: false, + }); + } + + const ENV = application.resolveRegistration('config:environment'); + + ENV['ember-cli-mirage'] = ENV['ember-cli-mirage'] || {}; + if (_shouldUseMirage(ENV.environment, ENV['ember-cli-mirage'])) { + startMirage(application.__container__, ENV); + } +} + +export default { + name: 'ember-cli-mirage', + initialize, +}; + + +function startMirage(owner, env = ENV) { + return startMirageImpl(owner, { env, makeServer }); +} + +function _shouldUseMirage(env, addonConfig) { + if (typeof FastBoot !== 'undefined') { + return false; + } + if (getRfc232TestContext()) { + return false; + } + let userDeclaredEnabled = typeof addonConfig.enabled !== 'undefined'; + let defaultEnabled = _defaultEnabled(env, addonConfig); + + return userDeclaredEnabled ? addonConfig.enabled : defaultEnabled; +} + +/* + Returns a boolean specifying the default behavior for whether + to initialize Mirage. +*/ +function _defaultEnabled(env, addonConfig) { + let usingInDev = env === 'development' && !addonConfig.usingProxy; + let usingInTest = env === 'test'; + + return usingInDev || usingInTest; +} diff --git a/packages/ember-cli-mirage/app/initializers/ember-cli-mirage.js b/packages/ember-cli-mirage/app/initializers/ember-cli-mirage.js index 82e4f0fb7..844d5497d 100644 --- a/packages/ember-cli-mirage/app/initializers/ember-cli-mirage.js +++ b/packages/ember-cli-mirage/app/initializers/ember-cli-mirage.js @@ -1,58 +1,4 @@ -import ENV from '../config/environment'; -import getRfc232TestContext from 'ember-cli-mirage/get-rfc232-test-context'; -import startMirageImpl from 'ember-cli-mirage/start-mirage'; -import * as config from '../mirage/config'; -const { default: makeServer } = config; - -// -// This initializer does two things: -// -// 1. Pulls the mirage config objects from the application's config and -// registers them in the container so `ember-cli-mirage/start-mirage` can -// find them (since it doesn't have access to the app's namespace). -// 2. Provides legacy support for auto-starting mirage in pre-rfc268 acceptance -// tests. -// -export default { - name: 'ember-cli-mirage', - initialize(application) { - if (makeServer) { - application.register('mirage:make-server', makeServer, { - instantiate: false, - }); - } - - ENV['ember-cli-mirage'] = ENV['ember-cli-mirage'] || {}; - if (_shouldUseMirage(ENV.environment, ENV['ember-cli-mirage'])) { - startMirage(application.__container__, ENV); - } - }, -}; - -function startMirage(owner, env = ENV) { - return startMirageImpl(owner, { env, makeServer }); -} - -function _shouldUseMirage(env, addonConfig) { - if (typeof FastBoot !== 'undefined') { - return false; - } - if (getRfc232TestContext()) { - return false; - } - let userDeclaredEnabled = typeof addonConfig.enabled !== 'undefined'; - let defaultEnabled = _defaultEnabled(env, addonConfig); - - return userDeclaredEnabled ? addonConfig.enabled : defaultEnabled; -} - -/* - Returns a boolean specifying the default behavior for whether - to initialize Mirage. -*/ -function _defaultEnabled(env, addonConfig) { - let usingInDev = env === 'development' && !addonConfig.usingProxy; - let usingInTest = env === 'test'; - - return usingInDev || usingInTest; -} +export { + default, + initialize, +} from 'ember-cli-mirage/initializers/ember-cli-mirage'; diff --git a/packages/ember-cli-mirage/index.js b/packages/ember-cli-mirage/index.js index 7b9fcd31c..9fd570a64 100644 --- a/packages/ember-cli-mirage/index.js +++ b/packages/ember-cli-mirage/index.js @@ -106,8 +106,8 @@ module.exports = { }); }, - treeForApp(appTree) { - let trees = [appTree]; + treeForAddon(addonTree) { + let trees = [addonTree]; let mirageFilesTree = new Funnel(this.mirageDirectory, { destDir: 'mirage', }); @@ -117,7 +117,7 @@ module.exports = { trees.push(this._lintMirageTree(mirageFilesTree)); } - return mergeTrees(trees); + return this._super(mergeTrees(trees)); }, _shouldIncludeFiles() {