@@ -11,6 +11,7 @@ const helpers = require('./outputHelpers');
1111const logger = require ( "../../../lib/logger" ) ;
1212const mime = require ( 'mime' ) ;
1313const path = require ( "path" ) ;
14+ const { promisify } = require ( 'util' ) ;
1415
1516function ImportSource ( req , done ) {
1617 var dbInstance ;
@@ -340,6 +341,31 @@ function ImportSource(req, done) {
340341 } , cb2 ) ;
341342 } , cb ) ;
342343 } ,
344+ async function populateGlobals ( ) {
345+ const dbRetrieve = promisify ( dbInstance . retrieve . bind ( dbInstance ) ) ;
346+ const dbUpdate = promisify ( dbInstance . update . bind ( dbInstance ) ) ;
347+ const courseQuery = await dbRetrieve ( 'course' , { _id : courseId } ) ;
348+ const courseGlobals = courseQuery [ 0 ] . _doc . _globals || { } ;
349+ await Promise . all ( plugindata . pluginIncludes . map ( async ( { type, name } ) => {
350+ const pluginQuery = await dbRetrieve ( `${ type } type` , { name } ) ;
351+ const plugin = pluginQuery [ 0 ] . _doc ;
352+ const schemaGlobals = plugin . globals ;
353+ if ( ! schemaGlobals ) return ;
354+ const schemaDefaults = { } ;
355+ const typeKey = type === 'component' || type === 'extension' ?
356+ `_${ type } s` :
357+ `_${ type } ` ;
358+ const pluginKey = `_${ plugin [ type ] } ` ;
359+ if ( ! courseGlobals [ typeKey ] ) {
360+ courseGlobals [ typeKey ] = { } ;
361+ }
362+ Object . entries ( schemaGlobals ) . forEach ( ( [ key , value ] ) => {
363+ schemaDefaults [ key ] = value . default ;
364+ } ) ;
365+ courseGlobals [ typeKey ] [ pluginKey ] = _ . defaults ( courseGlobals [ typeKey ] [ pluginKey ] , schemaDefaults ) ;
366+ } ) ) ;
367+ await dbUpdate ( 'course' , { _id : courseId } , { _globals : courseGlobals } ) ;
368+ } ,
343369 function checkDetachedContent ( cb ) {
344370 const detachedIds = Object . keys ( detachedElementsMap ) ;
345371 if ( detachedIds . length === 0 ) return cb ( ) ;
0 commit comments