2018-01-05 17:40:34 +00:00
"use strict" ;
2018-06-15 20:31:06 +00:00
const log = require ( "../../log" ) ;
2018-03-02 18:28:54 +00:00
const colors = require ( "chalk" ) ;
2018-02-08 05:19:44 +00:00
const path = require ( "path" ) ;
2018-01-05 17:40:34 +00:00
const Helper = require ( "../../helper" ) ;
const themes = require ( "./themes" ) ;
const packageMap = new Map ( ) ;
2019-07-02 16:02:02 +00:00
const inputs = require ( "../inputs" ) ;
2018-01-05 17:40:34 +00:00
const stylesheets = [ ] ;
module . exports = {
getStylesheets ,
getPackage ,
loadPackages ,
} ;
2019-07-05 14:02:57 +00:00
const packageApis = function ( packageName ) {
2018-01-05 17:40:34 +00:00
return {
Stylesheets : {
addFile : addStylesheet . bind ( this , packageName ) ,
} ,
2019-07-02 16:02:02 +00:00
Commands : {
2019-07-05 14:02:57 +00:00
add : inputs . addPluginCommand ,
runAsUser : ( command , targetId , client ) => client . inputLine ( { target : targetId , text : command } ) ,
2019-07-02 16:02:02 +00:00
} ,
2018-03-12 09:46:28 +00:00
Config : {
getConfig : ( ) => Helper . config ,
} ,
2018-01-05 17:40:34 +00:00
} ;
} ;
function addStylesheet ( packageName , filename ) {
stylesheets . push ( packageName + "/" + filename ) ;
}
function getStylesheets ( ) {
return stylesheets ;
}
function getPackage ( name ) {
return packageMap . get ( name ) ;
}
2019-07-05 14:02:57 +00:00
function loadPackages ( ) {
2018-02-08 05:19:44 +00:00
const packageJson = path . join ( Helper . getPackagesPath ( ) , "package.json" ) ;
let packages ;
2019-07-05 07:26:22 +00:00
let anyPlugins = false ;
2018-02-05 06:30:57 +00:00
2018-02-08 05:19:44 +00:00
try {
packages = Object . keys ( require ( packageJson ) . dependencies ) ;
} catch ( e ) {
packages = [ ] ;
}
2018-02-05 06:30:57 +00:00
2018-02-08 05:19:44 +00:00
packages . forEach ( ( packageName ) => {
const errorMsg = ` Package ${ colors . bold ( packageName ) } could not be loaded ` ;
let packageInfo ;
let packageFile ;
2018-02-05 06:30:57 +00:00
2018-02-08 05:19:44 +00:00
try {
packageInfo = require ( path . join ( Helper . getPackageModulePath ( packageName ) , "package.json" ) ) ;
packageFile = require ( Helper . getPackageModulePath ( packageName ) ) ;
} catch ( e ) {
log . warn ( errorMsg ) ;
return ;
}
2018-02-05 06:30:57 +00:00
2018-02-08 05:19:44 +00:00
if ( ! packageInfo . thelounge ) {
log . warn ( errorMsg ) ;
return ;
}
2018-02-05 06:30:57 +00:00
2018-02-13 11:57:13 +00:00
packageInfo = packageInfo . thelounge ;
2018-02-08 05:19:44 +00:00
packageMap . set ( packageName , packageFile ) ;
2018-01-05 17:40:34 +00:00
2018-02-08 05:19:44 +00:00
if ( packageInfo . type === "theme" ) {
themes . addTheme ( packageName , packageInfo ) ;
2019-07-05 07:26:22 +00:00
} else {
anyPlugins = true ;
2018-02-08 05:19:44 +00:00
}
2018-02-05 06:30:57 +00:00
2018-02-08 05:19:44 +00:00
if ( packageFile . onServerStart ) {
2019-07-05 14:02:57 +00:00
packageFile . onServerStart ( packageApis ( packageName ) ) ;
2018-02-08 05:19:44 +00:00
}
2018-02-05 06:30:57 +00:00
2018-02-08 05:19:44 +00:00
log . info ( ` Package ${ colors . bold ( packageName ) } loaded ` ) ;
} ) ;
2019-07-05 07:26:22 +00:00
if ( anyPlugins ) {
log . info ( "There are packages using the experimental plugin API. Be aware that this API is not yet stable and may change in future The Lounge releases." ) ;
}
2018-01-05 17:40:34 +00:00
}