Skip to content

Commit

Permalink
dev: add tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed May 28, 2024
1 parent 5ed1d3f commit f25a44d
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 1 deletion.
151 changes: 151 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"oojs-ui": "^0.46.3",
"rollup": "^2.77.0",
"rollup-plugin-git-info": "^1.0.0",
"rollup-plugin-jscc": "^2.0.0",
"rollup-plugin-node-license": "^0.2.1",
"rollup-plugin-serve": "^2.0.1",
"rollup-plugin-sourcemaps": "^0.6.3",
Expand Down
6 changes: 6 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import jscc from 'rollup-plugin-jscc';
import typescript from 'rollup-plugin-typescript2';
import visualizer from 'rollup-plugin-visualizer';
import sourcemaps from 'rollup-plugin-sourcemaps';
Expand Down Expand Up @@ -88,6 +89,11 @@ function getPlugins() {
license(),
// Inserts sourcemaps
!production && sourcemaps(),
// Remove development-only code branches
jscc( {
values: { _DEV: process.env.NODE_ENV === 'development' },
asloader: false
} ),
// Makes Common.js imports possible
commonjs(),
// Handles Node-like resolution
Expand Down
21 changes: 21 additions & 0 deletions src/config/WikiConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '../wiki/TemplatePolyfills';
import ConfigurationReloadBanner from '../ui/config/ConfigurationReloadBanner';
import changeTag from './changeTag';
import applyOverrides from '../util/applyOverrides';

export type WikiPageConfiguration = {
title: mw.Title,
Expand Down Expand Up @@ -373,6 +374,26 @@ export default class WikiConfiguration extends ConfigurationBase {
) {
super();
if ( serializedData ) {
// #if _DEV
if ( window.deputyWikiConfigOverride ) {
console.warn(
'[deputy] Configuration overrides found for Deputy. This may be bad!'
);
applyOverrides(
this.serializedData,
window.deputyWikiConfigOverride,
( key, oldVal, newVal ) => {

console.warn( `[deputy] ${key}: ${
JSON.stringify( oldVal )
}${
JSON.stringify( newVal )
}` );
}
);
}
// #endif

this.deserialize( serializedData );
}

Expand Down
14 changes: 13 additions & 1 deletion src/modules/DeputyModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ export default abstract class DeputyModule {
*/
abstract getName(): string;

/**
* Get the module key for this module. Allows modules to be identified with a different
* configuration key.
*
* @return The module key. the module name by default.
*/
getModuleKey(): string {
return this.getName();
}

/**
* Load the language pack for this module, with a fallback in case one could not be
* loaded.
Expand All @@ -111,7 +121,9 @@ export default abstract class DeputyModule {
async preInit( languageFallback: Record<string, string> ): Promise<boolean> {
await this.getWikiConfig();

if ( this.wikiConfig[ this.getName() as 'ia' | 'ante' ]?.enabled.get() !== true ) {
if ( this.wikiConfig[
this.getModuleKey() as 'cci' | 'ia' | 'ante'
]?.enabled.get() !== true ) {
// Stop loading here.
warn( `Preinit for ${
this.getName()
Expand Down
15 changes: 15 additions & 0 deletions src/modules/dynamicDevModuleLoad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// #if _DEV
import warn from '../util/warn';

/**
* Loads a module in development. Only works in development contexts.
*
* @param moduleCode
*/
export default async function dynamicDevModuleLoad( moduleCode: string ) {
warn( 'Dynamically loading a module from localhost. This may be bad!' );
return new Promise( ( res, rej ) => mw.loader.getScript(
`http://localhost:45000/deputy-${ moduleCode }.js`
).then( res, rej ) );
}
// #endif _DEV
43 changes: 43 additions & 0 deletions src/modules/dynamicModuleLoad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import getPageExists from '../wiki/util/getPageExists';

/**
* Load a module from a string.
*
* @param moduleName
*/
export default async function dynamicModuleLoad( moduleName: string ) {
/**
* This is a very sensitive variable!
*
* Ensure that not anyone can modify these. This usually comes as interface admin
* protection or userspace protection.
*
* LOADING FROM PAGES THAT CAN BE EDITED BY ANYONE CAN LEAD TO SECURITY
* IMPLICATIONS!
*/
const expectedLocations = [
`MediaWiki:Gadget-${ moduleName }-core.js`,
`MediaWiki:Gadget-${ moduleName }.js`,
`MediaWiki:Gadget-Deputy-${ moduleName }-core.js`,
`MediaWiki:Gadget-Deputy-${ moduleName }.js`,
`MediaWiki:${ moduleName }-core.js`,
`MediaWiki:${ moduleName }.js`,
`User:Chlod/Scripts/Deputy/${ moduleName }-core.js`,
`User:Chlod/Scripts/Deputy/${ moduleName }.js`
// Security implications of this TBD.
// `Gadget:${ moduleName }-core.js`,
// `Gadget:${ moduleName }.js`
];

// Check which of those has something in it.
const ape = await getPageExists( expectedLocations );
if ( ape.length > 0 ) {
// Page exists! Load it.
return new Promise( ( res, rej ) => {
mw.loader.getScript( mw.util.getUrl( ape[ 0 ] ) ).then( res, rej );
} );
} else {
// Nothing got loaded.
return Promise.reject( new Error( 'Could not find a script to load.' ) );
}
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare global {
interface Window {
deputy: Deputy;
deputyLang?: string;
deputyWikiConfigOverride: Record<string, any>;
moment: moment.Moment;
}
}
Expand Down
Loading

0 comments on commit f25a44d

Please sign in to comment.