forked from fastify/fastify-autoload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (147 loc) · 4.64 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict'
const fs = require('fs')
const path = require('path')
const steed = require('steed')
module.exports = function (fastify, opts, next) {
const defaultPluginOptions = opts.options
function enrichError (err) {
// Hack SyntaxError message so that we provide
// the line number to the user, otherwise they
// will be left in the cold.
if (err instanceof SyntaxError) {
err.message += ' at ' + err.stack.split('\n')[0]
}
return err
}
fs.readdir(opts.dir, function (err, list) {
if (err) {
next(err)
return
}
steed.map(list, (file, cb) => {
if (opts.ignorePattern && file.match(opts.ignorePattern)) {
cb(null, { skip: true }) // skip files matching `ignorePattern`
return
}
const toLoad = path.join(opts.dir, file)
fs.stat(toLoad, (err, stat) => {
if (err) {
cb(err)
return
}
if (stat.isDirectory()) {
fs.readdir(toLoad, (err, files) => {
if (err) {
cb(err)
return
}
// if the directory contains files but no index.js, load them as independend plugins
if (
files.indexOf('index.js') === -1 &&
files.indexOf('package.json') === -1 &&
files.toString().indexOf('.js') > -1
) {
let plugins = []
for (let index = 0; index < files.length; index++) {
const file = files[index]
plugins.push({
skip: !file.match(/.js$/),
opts: {
prefix: toLoad.split(path.sep).pop()
},
file: path.join(toLoad, file)
})
}
cb(null, plugins)
} else {
cb(null, {
// skip directories without .js files inside
skip: files.every(name => !name.match(/.js$/)),
file: toLoad
})
}
})
} else {
cb(null, {
// only accept .js files
skip: !(stat.isFile() && file.match(/.js$/)),
file: toLoad
})
}
})
}, (err, files) => {
if (err) {
next(err)
return
}
const stats = [].concat(...files)
const allPlugins = {}
for (let i = 0; i < stats.length; i++) {
const { skip, file, opts } = stats[i]
if (skip) {
continue
}
try {
const plugin = require(file)
const pluginOptions = Object.assign({}, defaultPluginOptions)
const pluginMeta = plugin[Symbol.for('plugin-meta')] || {}
const pluginName = pluginMeta.name || file
if (opts && !plugin.autoPrefix) {
plugin.autoPrefix = opts.prefix
}
if (plugin.autoload === false) {
continue
}
if (plugin.autoPrefix) {
const prefix = pluginOptions.prefix || ''
pluginOptions.prefix = prefix + plugin.autoPrefix
}
if (plugin.prefixOverride !== void 0) {
pluginOptions.prefix = plugin.prefixOverride
}
if (allPlugins[pluginName]) {
throw new Error(`Duplicate plugin: ${pluginName}`)
}
allPlugins[pluginName] = {
plugin,
name: pluginName,
dependencies: pluginMeta.dependencies,
options: pluginOptions
}
} catch (err) {
next(enrichError(err))
return
}
}
const loadedPlugins = {}
function registerPlugin (name, plugin, options) {
if (loadedPlugins[name]) return
fastify.register(plugin.default || plugin, options)
loadedPlugins[name] = true
}
let cyclicDependencyCheck = {}
function loadPlugin ({ plugin, name, dependencies = [], options }) {
if (cyclicDependencyCheck[name]) throw new Error('Cyclic dependency')
if (dependencies.length) {
cyclicDependencyCheck[name] = true
dependencies.forEach((name) => allPlugins[name] && loadPlugin(allPlugins[name]))
}
registerPlugin(name, plugin, options)
}
const pluginKeys = Object.keys(allPlugins)
for (let i = 0; i < pluginKeys.length; i++) {
cyclicDependencyCheck = {}
try {
loadPlugin(allPlugins[pluginKeys[i]])
} catch (err) {
next(enrichError(err))
return
}
}
next()
})
})
}
// do not create a new context, do not encapsulate
// same as fastify-plugin
module.exports[Symbol.for('skip-override')] = true