This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
plugins.js
55 lines (46 loc) · 1.77 KB
/
plugins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const fs = require( 'fs' )
const path = require( 'path' )
const winston = require( './config/logger' )
const URL = require( 'url' ).URL
const isDirectory = source => fs.lstatSync( source ).isDirectory( )
const getDirectories = source => fs.readdirSync( source ).map( name => path.join( source, name ) ).filter( isDirectory )
module.exports = ( ) => {
winston.debug( 'Scanning for speckle plugins...' )
// gather potential plugin subdirectories
const rootDirs = process.env.PLUGIN_DIRS.split( ',' )
let pluginDirs = [ ]
rootDirs.forEach( dir => {
if ( fs.existsSync( dir ) ) {
let dirs = getDirectories( dir )
pluginDirs.push( ...dirs, dir )
} else
winston.warn( `specified plugin directory does not exist: ${dir}` )
} )
// read in manifest files
let plugins = [ ]
pluginDirs.forEach( dir => {
let file = path.join( dir, 'speckle-plugin-manifest.json' )
if ( fs.existsSync( file ) ) {
let obj = JSON.parse( fs.readFileSync( file, 'utf8' ) )
obj.sourceDir = dir
if ( obj.serveSource )
obj.serveSource = path.join( dir, obj.serveSource )
plugins.push( obj )
} else
winston.warn( `No plugin manifest file found in ${dir}.` )
} )
// check for conflicts
let serveLocations = [ ]
plugins.forEach( pl => {
if ( !serveLocations.includes( pl.serveFrom ) )
serveLocations.push( pl.serveFrom )
else {
winston.warn( `Conflicting plugin endpoint found at: ${pl.serveFrom} in folder ${pl.sourceDir}.
It will load from ${pl.serveFrom + '-dupe'} instead.` )
pl.serveFrom += '-dupe'
}
pl.canonicalUrl = new URL( pl.serveFrom, process.env.CANONICAL_URL )
} )
winston.debug( `Found ${plugins.length} plugin(s): ${plugins.map( p => p.name ).join( ', ' )}` )
return plugins
}