Remove discourse constants (#9958)

* DEV: `Discourse.baseUri` does not exist

This never could have worked - should have been `Discourse.BaseUri` if
anything.

* DEV: Remove Discourse.Environment

* DEV: Remove `Discourse.disableMissingIconWarning`

* DEV: A bunch more missing environment checks
This commit is contained in:
Robin Ward 2020-06-01 16:33:43 -04:00 committed by GitHub
parent 9162cd8f3d
commit a95826f60c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 92 additions and 62 deletions

View File

@ -9,8 +9,8 @@ import ReportLoader from "discourse/lib/reports-loader";
import { exportEntity } from "discourse/lib/export-csv";
import { outputExportResult } from "discourse/lib/export-result";
import Report, { SCHEMA_VERSION } from "admin/models/report";
import ENV from "discourse-common/config/environment";
import { isPresent } from "@ember/utils";
import { isTesting } from "discourse-common/config/environment";
const TABLE_OPTIONS = {
perPage: 8,
@ -167,8 +167,8 @@ export default Component.extend({
let reportKey = "reports:";
reportKey += [
dataSourceName,
ENV.environment === "test" ? "start" : startDate.replace(/-/g, ""),
ENV.environment === "test" ? "end" : endDate.replace(/-/g, ""),
isTesting() ? "start" : startDate.replace(/-/g, ""),
isTesting() ? "end" : endDate.replace(/-/g, ""),
"[:prev_period]",
this.get("reportOptions.table.limit"),
// Convert all filter values to strings to ensure unique serialization

View File

@ -4,7 +4,7 @@ import Component from "@ember/component";
import discourseComputed, { observes } from "discourse-common/utils/decorators";
import { iconHTML } from "discourse-common/lib/icon-library";
import { escape } from "pretty-text/sanitizer";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
const MAX_COMPONENTS = 4;
@ -41,7 +41,7 @@ export default Component.extend({
animate(isInitial) {
const $container = $(this.element);
const $list = $(this.element.querySelector(".components-list"));
if ($list.length === 0 || ENV.environment === "test") {
if ($list.length === 0 || isTesting()) {
return;
}
const duration = 300;

View File

@ -1,9 +1,23 @@
export const INPUT_DELAY = 250;
let environment = Ember.testing ? "test" : "development";
let environment = "unknown";
export function isTesting() {
return environment === "test";
export function setEnvironment(e) {
if (isTesting()) {
environment = "testing";
} else {
environment = e;
}
}
export default { environment };
export function isTesting() {
return Ember.testing;
}
export function isDevelopment() {
return environment === "development";
}
export function isProduction() {
return environment === "production";
}

View File

@ -1,10 +1,13 @@
import I18n from "I18n";
import { h } from "virtual-dom";
import attributeHook from "discourse-common/lib/attribute-hook";
import { isDevelopment } from "discourse-common/config/environment";
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
let _renderers = [];
let warnMissingIcons = true;
const REPLACEMENTS = {
"d-tracking": "bell",
"d-muted": "discourse-bell-slash",
@ -45,6 +48,14 @@ export function replaceIcon(source, destination) {
REPLACEMENTS[source] = destination;
}
export function disableMissingIconWarning() {
warnMissingIcons = false;
}
export function enableMissingIconWarning() {
warnMissingIcons = false;
}
export function renderIcon(renderType, id, params) {
for (let i = 0; i < _renderers.length; i++) {
let renderer = _renderers[i];
@ -105,8 +116,8 @@ function iconClasses(icon, params) {
function warnIfMissing(id) {
if (
typeof Discourse !== "undefined" &&
Discourse.Environment === "development" &&
!Discourse.disableMissingIconWarning &&
isDevelopment() &&
warnMissingIcons &&
Discourse.SvgIconList &&
Discourse.SvgIconList.indexOf(id) === -1
) {

View File

@ -44,7 +44,7 @@ import {
cacheShortUploadUrl,
resolveAllShortUrls
} from "pretty-text/upload-short-url";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
const REBUILD_SCROLL_MAP_EVENTS = ["composer:resized", "composer:typed-reply"];
@ -847,7 +847,7 @@ export default Component.extend({
// need to wait a bit for the "slide down" transition of the composer
later(
() => this.appEvents.trigger("composer:closed"),
ENV.environment === "test" ? 0 : 400
isTesting() ? 0 : 400
);
});

View File

@ -6,7 +6,7 @@ import discourseComputed, { observes } from "discourse-common/utils/decorators";
import { load } from "pretty-text/oneboxer";
import { lookupCache } from "pretty-text/oneboxer-cache";
import { ajax } from "discourse/lib/ajax";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
import EmberObject from "@ember/object";
import putCursorAtEnd from "discourse/lib/put-cursor-at-end";
@ -76,7 +76,7 @@ export default Component.extend({
return;
}
if (ENV.environment === "test") {
if (isTesting()) {
next(() =>
// not ideal but we don't want to run this in current
// runloop to avoid an error in console

View File

@ -28,7 +28,7 @@ import { emojiSearch, isSkinTonableEmoji } from "pretty-text/emoji";
import { emojiUrlFor } from "discourse/lib/text";
import showModal from "discourse/lib/show-modal";
import { Promise } from "rsvp";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
// Our head can be a static string or a function that returns a string
// based on input (like for numbered lists).
@ -378,7 +378,7 @@ export default Component.extend({
}
// Debouncing in test mode is complicated
if (ENV.environment === "test") {
if (isTesting()) {
this._updatePreview();
} else {
debounce(this, this._updatePreview, 30);

View File

@ -10,7 +10,7 @@ import {
emojiSearch
} from "pretty-text/emoji";
import { safariHacksDisabled } from "discourse/lib/utilities";
import ENV, { INPUT_DELAY } from "discourse-common/config/environment";
import { isTesting, INPUT_DELAY } from "discourse-common/config/environment";
const PER_ROW = 11;
function customEmojis() {
@ -525,7 +525,7 @@ export default Component.extend({
this.$picker.css(_.merge(attributes, options));
};
if (ENV.environment === "test" || !this.automaticPositioning) {
if (isTesting() || !this.automaticPositioning) {
desktopPositioning();
return;
}

View File

@ -8,7 +8,7 @@ import {
REMINDER_TYPE,
DELETE_REPLIES_TYPE
} from "discourse/controllers/edit-topic-timer";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
export default Component.extend({
classNames: ["topic-status-info"],
@ -92,7 +92,7 @@ export default Component.extend({
});
// TODO Sam: concerned this can cause a heavy rerender loop
if (ENV.environment !== "test") {
if (!isTesting()) {
this._delayedRerender = later(() => {
this.renderTopicTimer();
}, rerenderDelay);

View File

@ -23,7 +23,7 @@ import { emojiUnescape } from "discourse/lib/text";
import { shortDate } from "discourse/lib/formatter";
import { SAVE_LABELS, SAVE_ICONS } from "discourse/models/composer";
import { Promise } from "rsvp";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
import EmberObject, { computed, action } from "@ember/object";
import deprecated from "discourse-common/lib/deprecated";
@ -71,7 +71,7 @@ function loadDraft(store, opts) {
const _popupMenuOptionsCallbacks = [];
let _checkDraftPopup = ENV.environment !== "test";
let _checkDraftPopup = !isTesting();
export function toggleCheckDraftPopup(enabled) {
_checkDraftPopup = enabled;
@ -1109,7 +1109,7 @@ export default Controller.extend({
if (model.draftSaving) {
// in test debounce is Ember.run, this will cause
// an infinite loop
if (ENV.environment !== "test") {
if (!isTesting()) {
this._saveDraftDebounce = debounce(this, this._saveDraft, 2000);
}
} else {

View File

@ -1,6 +1,6 @@
import DiscourseURL from "discourse/lib/url";
import { currentThemeIds, refreshCSS } from "discourse/lib/theme-selector";
import ENV from "discourse-common/config/environment";
import { isDevelopment } from "discourse-common/config/environment";
import Handlebars from "handlebars";
// Use the message bus for live reloading of components for faster development.
@ -48,7 +48,7 @@ export default {
});
// Useful to export this for debugging purposes
if (Discourse.Environment === "development" && ENV.environment !== "test") {
if (isDevelopment()) {
window.DiscourseURL = DiscourseURL;
}

View File

@ -1,6 +1,7 @@
// Initialize the message bus to receive messages.
import userPresent from "discourse/lib/user-presence";
import { handleLogoff } from "discourse/lib/ajax";
import { isProduction } from "discourse-common/config/environment";
const LONG_POLL_AFTER_UNSEEN_TIME = 1200000; // 20 minutes
@ -32,7 +33,7 @@ export default {
user = container.lookup("current-user:main"),
siteSettings = container.lookup("site-settings:main");
messageBus.alwaysLongPoll = Discourse.Environment === "development";
messageBus.alwaysLongPoll = !isProduction();
messageBus.shouldLongPollCallback = () =>
userPresent(LONG_POLL_AFTER_UNSEEN_TIME);

View File

@ -11,7 +11,7 @@ import {
unsubscribe as unsubscribePushNotifications,
isPushNotificationsEnabled
} from "discourse/lib/push-notifications";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
export default {
name: "subscribe-user-notifications",
@ -130,7 +130,7 @@ export default {
Discourse.set("assetVersion", data)
);
if (ENV.environment !== "test") {
if (!isTesting()) {
bus.subscribe(alertChannel(user), data => onNotification(data, user));
initDesktopNotifications(bus, appEvents);

View File

@ -5,7 +5,7 @@ import DiscourseURL from "discourse/lib/url";
import { wantsNewWindow } from "discourse/lib/intercept-click";
import { selectedText } from "discourse/lib/utilities";
import { Promise } from "rsvp";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
import User from "discourse/models/user";
export function isValidLink($link) {
@ -101,7 +101,7 @@ export default {
let trackPromise = Promise.resolve();
if (tracking) {
if (ENV.environment !== "test" && navigator.sendBeacon) {
if (!isTesting() && navigator.sendBeacon) {
const data = new FormData();
data.append("url", href);
data.append("post_id", postId);

View File

@ -1,4 +1,4 @@
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
import AppEvents from "discourse/services/app-events";
let _skipUpdate;
@ -9,7 +9,7 @@ export function configureEyeline(opts) {
_skipUpdate = opts.skipUpdate;
_rootElement = opts.rootElement;
} else {
_skipUpdate = ENV.environment === "test";
_skipUpdate = isTesting();
_rootElement = null;
}
}

View File

@ -1,4 +1,4 @@
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
let mobileForced = false;
@ -12,7 +12,7 @@ const Mobile = {
this.isMobileDevice = mobileForced || $html.hasClass("mobile-device");
this.mobileView = mobileForced || $html.hasClass("mobile-view");
if (ENV.environment === "test" || mobileForced) {
if (isTesting() || mobileForced) {
return;
}

View File

@ -1,14 +1,14 @@
import EmberRouter from "@ember/routing/router";
import { defaultHomepage } from "discourse/lib/utilities";
import { rewritePath } from "discourse/lib/url";
import ENV from "discourse-common/config/environment";
import Site from "discourse/models/site";
import { isTesting } from "discourse-common/config/environment";
const rootURL = Discourse.BaseUri;
const BareRouter = EmberRouter.extend({
rootURL,
location: ENV.environment === "test" ? "none" : "discourse-location",
location: isTesting() ? "none" : "discourse-location",
handleURL(url) {
url = rewritePath(url);

View File

@ -2,11 +2,11 @@ import { scheduleOnce } from "@ember/runloop";
import DiscourseURL from "discourse/lib/url";
import { deprecated } from "discourse/mixins/scroll-top";
import Mixin from "@ember/object/mixin";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
const context = {
_scrollTop() {
if (ENV.environment === "test") {
if (isTesting()) {
return;
}
$(document).scrollTop(0);

View File

@ -80,11 +80,6 @@ const Site = RestModel.extend({
return result;
},
@discourseComputed
baseUri() {
return Discourse.baseUri;
},
// Returns it in the correct order, by setting
@discourseComputed("categories.[]")
categoriesList() {

View File

@ -2,7 +2,11 @@ import PreloadStore from "discourse/lib/preload-store";
import I18n from "I18n";
import Session from "discourse/models/session";
import RSVP from "rsvp";
import { isTesting } from "discourse-common/config/environment";
import {
setEnvironment,
isTesting,
isProduction
} from "discourse-common/config/environment";
export default {
name: "discourse-bootstrap",
@ -33,7 +37,7 @@ export default {
app.CDN = setupData.cdn;
app.BaseUrl = setupData.baseUrl;
app.BaseUri = setupData.baseUri;
app.Environment = setupData.environment;
setEnvironment(setupData.environment);
app.SiteSettings = PreloadStore.get("siteSettings");
app.ThemeSettings = PreloadStore.get("themeSettings");
app.LetterAvatarVersion = setupData.letterAvatarVersion;
@ -73,7 +77,7 @@ export default {
return;
}
if (Discourse.Environment === "development") {
if (!isProduction()) {
/* eslint-disable no-console */
if (e) {
if (e.message || e.stack) {

View File

@ -3,7 +3,7 @@ import { schedule } from "@ember/runloop";
import DiscourseRoute from "discourse/routes/discourse";
import DiscourseURL from "discourse/lib/url";
import Draft from "discourse/models/draft";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
// This route is used for retrieving a topic based on params
export default DiscourseRoute.extend({
@ -81,7 +81,7 @@ export default DiscourseRoute.extend({
}
})
.catch(e => {
if (ENV.environment !== "test") {
if (!isTesting()) {
// eslint-disable-next-line no-console
console.log("Could not view topic", e);
}

View File

@ -2,7 +2,7 @@ import { cancel, scheduleOnce } from "@ember/runloop";
import { diff, patch } from "virtual-dom";
import { queryRegistry } from "discourse/widgets/widget";
import DirtyKeys from "discourse/lib/dirty-keys";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
export default class WidgetGlue {
constructor(name, register, attrs) {
@ -34,7 +34,7 @@ export default class WidgetGlue {
cancel(this._timeout);
// in test mode return early if store cannot be found
if (ENV.environment === "test") {
if (isTesting()) {
try {
this.register.lookup("service:store");
} catch (e) {

View File

@ -4,14 +4,14 @@ import { avatarAtts } from "discourse/widgets/actions-summary";
import { h } from "virtual-dom";
import showModal from "discourse/lib/show-modal";
import { Promise } from "rsvp";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
import { formattedReminderTime } from "discourse/lib/bookmark";
const LIKE_ACTION = 2;
const VIBRATE_DURATION = 5;
function animateHeart($elem, start, end, complete) {
if (ENV.environment === "test") {
if (isTesting()) {
return run(this, complete);
}

View File

@ -16,7 +16,7 @@ import {
import { h } from "virtual-dom";
import DecoratorHelper from "discourse/widgets/decorator-helper";
import { Promise } from "rsvp";
import ENV from "discourse-common/config/environment";
import { isProduction } from "discourse-common/config/environment";
import { get } from "@ember/object";
const _registry = {};
@ -127,7 +127,7 @@ export default class Widget {
this.init(this.attrs);
// Helps debug widgets
if (Discourse.Environment === "development" || ENV.environment === "test") {
if (!isProduction()) {
const ds = this.defaultState(attrs);
if (typeof ds !== "object") {
throw new Error(`defaultState must return an object`);

View File

@ -2,7 +2,12 @@ import MultiSelectComponent from "select-kit/components/multi-select";
import { computed } from "@ember/object";
import { ajax } from "discourse/lib/ajax";
import { makeArray } from "discourse-common/lib/helpers";
import { convertIconClass } from "discourse-common/lib/icon-library";
import {
convertIconClass,
disableMissingIconWarning,
enableMissingIconWarning
} from "discourse-common/lib/icon-library";
import { isDevelopment } from "discourse-common/config/environment";
export default MultiSelectComponent.extend({
pluginApiIdentifiers: ["icon-picker"],
@ -13,8 +18,8 @@ export default MultiSelectComponent.extend({
this._cachedIconsList = null;
if (Discourse.Environment === "development") {
Discourse.disableMissingIconWarning = true;
if (isDevelopment()) {
disableMissingIconWarning();
}
},
@ -75,8 +80,8 @@ export default MultiSelectComponent.extend({
this._cachedIconsList = null;
if (Discourse.Environment === "development") {
delete Discourse.disableMissingIconWarning;
if (isDevelopment()) {
enableMissingIconWarning();
}
},

View File

@ -1,10 +1,10 @@
import EmberRouter from "@ember/routing/router";
import getUrl from "discourse-common/lib/get-url";
import ENV from "discourse-common/config/environment";
import { isTesting } from "discourse-common/config/environment";
const Router = EmberRouter.extend({
rootURL: getUrl("/wizard/"),
location: ENV.environment === "test" ? "none" : "history"
location: isTesting() ? "none" : "history"
});
Router.map(function() {