From 9e5cce37da172770f3336237cdf0761e82dbe139 Mon Sep 17 00:00:00 2001 From: Cody Jackson Date: Tue, 19 Mar 2024 15:20:31 -0700 Subject: [PATCH] Factoring plugin installation out of index.js. One of a series of changes to simplify the bootstrapping and authentication code. --- shell/initialize/index.js | 99 ++--------------------------------- shell/initialize/plugins.js | 39 ++++++++++++++ shell/plugins/ember-cookie.js | 13 +++++ 3 files changed, 55 insertions(+), 96 deletions(-) create mode 100644 shell/initialize/plugins.js create mode 100644 shell/plugins/ember-cookie.js diff --git a/shell/initialize/index.js b/shell/initialize/index.js index 6f3f743d3ca..ab4d1a4e441 100644 --- a/shell/initialize/index.js +++ b/shell/initialize/index.js @@ -7,37 +7,9 @@ import NuxtChild from '../components/nuxt/nuxt-child.js'; import App from './App.js'; import { setContext, getLocation, getRouteData, normalizeError } from '../utils/nuxt'; import { createStore } from '../config/store.js'; - -/* Plugins */ -import { loadDirectives } from '@shell/plugins'; -import '../plugins/portal-vue.js'; -import cookieUniversalNuxt from '../utils/cookie-universal-nuxt.js'; -import axios from '../utils/axios.js'; -import plugins from '../core/plugins.js'; -import pluginsLoader from '../core/plugins-loader.js'; -import axiosShell from '../plugins/axios'; -import '../plugins/tooltip'; -import '../plugins/v-select'; -import '../plugins/js-yaml'; -import '../plugins/resize'; -import '../plugins/shortkey'; -import '../plugins/i18n'; -import '../plugins/global-formatters'; -import '../plugins/trim-whitespace'; -import '../plugins/extend-router'; - -import intNumber from '../plugins/int-number'; -import positiveIntNumber from '../plugins/positive-int-number.js'; -import nuxtClientInit from '../plugins/nuxt-client-init'; -import replaceAll from '../plugins/replaceall'; -import backButton from '../plugins/back-button'; -import plugin from '../plugins/plugin'; -import codeMirror from '../plugins/codemirror-loader'; -import '../plugins/formatters'; -import version from '../plugins/version'; -import steveCreateWorker from '../plugins/steve-create-worker'; -import { REDIRECTED } from '@shell/config/cookies'; import { UPGRADED, _FLAGGED, _UNFLAG } from '@shell/config/query-params'; +import { loadDirectives } from '@shell/plugins'; +import { installPlugins } from '@shell/initialize/plugins'; // Prevent extensions from overriding existing directives // Hook into Vue.directive and keep track of the directive names that have been added @@ -165,62 +137,7 @@ async function createApp(config = {}) { // Inject runtime config as $config inject('config', config); - // Plugin execution - if (typeof cookieUniversalNuxt === 'function') { - await cookieUniversalNuxt(app.context, inject); - } - - if (typeof axios === 'function') { - await axios(app.context, inject); - } - - if (typeof plugins === 'function') { - await plugins(app.context, inject); - } - - if (typeof pluginsLoader === 'function') { - await pluginsLoader(app.context, inject); - } - - if (typeof axiosShell === 'function') { - await axiosShell(app.context, inject); - } - - if (typeof intNumber === 'function') { - await intNumber(app.context, inject); - } - - if (typeof positiveIntNumber === 'function') { - await positiveIntNumber(app.context, inject); - } - - if (typeof nuxtClientInit === 'function') { - await nuxtClientInit(app.context, inject); - } - - if (typeof replaceAll === 'function') { - await replaceAll(app.context, inject); - } - - if (typeof backButton === 'function') { - await backButton(app.context, inject); - } - - if (typeof plugin === 'function') { - await plugin(app.context, inject); - } - - if (typeof codeMirror === 'function') { - await codeMirror(app.context, inject); - } - - if (typeof version === 'function') { - await version(app.context, inject); - } - - if (typeof steveCreateWorker === 'function') { - await steveCreateWorker(app.context, inject); - } + await installPlugins(app, inject); // Wait for async component to be resolved first await new Promise((resolve, reject) => { @@ -265,16 +182,6 @@ async function createApp(config = {}) { }); }); - // This tells Ember not to redirect back to us once you've already been to dashboard once. - // TODO: Remove this once the ember portion of the app is no longer needed - if ( !app.context.$cookies.get(REDIRECTED) ) { - app.context.$cookies.set(REDIRECTED, 'true', { - path: '/', - sameSite: true, - secure: true, - }); - } - return { store, app, diff --git a/shell/initialize/plugins.js b/shell/initialize/plugins.js new file mode 100644 index 00000000000..f666793aaf9 --- /dev/null +++ b/shell/initialize/plugins.js @@ -0,0 +1,39 @@ +import '@shell/plugins/extend-router'; +import '@shell/plugins/formatters'; +import '@shell/plugins/global-formatters'; +import '@shell/plugins/i18n'; +import '@shell/plugins/js-yaml'; +import '@shell/plugins/portal-vue.js'; +import '@shell/plugins/resize'; +import '@shell/plugins/shortkey'; +import '@shell/plugins/tooltip'; +import '@shell/plugins/trim-whitespace'; +import '@shell/plugins/v-select'; + +import axios from '../utils/axios.js'; +import axiosShell from '@shell/plugins/axios'; +import backButton from '@shell/plugins/back-button'; +import codeMirror from '@shell/plugins/codemirror-loader'; +import cookieUniversalNuxt from '../utils/cookie-universal-nuxt.js'; +import * as intNumber from '@shell/plugins/int-number'; +import nuxtClientInit from '@shell/plugins/nuxt-client-init'; +import plugin from '@shell/plugins/plugin'; +import plugins from '@shell/core/plugins.js'; +import pluginsLoader from '../core/plugins-loader.js'; +import * as positiveIntNumber from '@shell/plugins/positive-int-number.js'; +import replaceAll from '@shell/plugins/replaceall'; +import steveCreateWorker from '@shell/plugins/steve-create-worker'; +import version from '@shell/plugins/version'; +import emberCookie from '@shell/plugins/ember-cookie'; + +export async function installPlugins(app, inject) { + const pluginDefinitions = [cookieUniversalNuxt, axios, plugins, pluginsLoader, axiosShell, intNumber, positiveIntNumber, nuxtClientInit, replaceAll, backButton, plugin, codeMirror, version, steveCreateWorker, emberCookie]; + + const installations = pluginDefinitions.map(async(pluginDefinition) => { + if (typeof pluginDefinition === 'function') { + await pluginDefinition(app.context, inject); + } + }); + + await Promise.all(installations); +} diff --git a/shell/plugins/ember-cookie.js b/shell/plugins/ember-cookie.js new file mode 100644 index 00000000000..65c7f08fffc --- /dev/null +++ b/shell/plugins/ember-cookie.js @@ -0,0 +1,13 @@ +import { REDIRECTED } from '@shell/config/cookies'; + +export default function({ $cookies }) { + // This tells Ember not to redirect back to us once you've already been to dashboard once. + // TODO: Remove this once the ember portion of the app is no longer needed + if ( !$cookies.get(REDIRECTED) ) { + $cookies.set(REDIRECTED, 'true', { + path: '/', + sameSite: true, + secure: true, + }); + } +}