-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathplugins.js
More file actions
180 lines (169 loc) · 5.28 KB
/
plugins.js
File metadata and controls
180 lines (169 loc) · 5.28 KB
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
180
const path = require('path')
const colors = require('colors/safe')
const builtinPlugins = require('./builtin_plugins')
const fs = require('fs')
const { execSync } = require('child_process')
exports.builtinDefaultPlugins = builtinPlugins.defaultPlugins
// Function to get the global npm root directory
var getGlobalNpmRoot = function () {
try {
return execSync('npm root -g', { encoding: 'utf8', stdio: 'pipe' }).trim()
} catch (error) {
// Fallback to common paths if npm command fails
return null
}
}
var createConfigPlugin = async function (pluginName, parameters, localPath) {
// for each plugin, look for a local definition, a built-in definition, or
// a module-provided definition (module relaxed-pluginName)
var origin
var plugin = builtinPlugins.plugins[pluginName]
if (plugin) {
origin = `ReLaXed ${pluginName} built-in plugin`
} else {
var possiblePaths = [
{
location: path.join(localPath, `${pluginName}.plugin.js`),
origin: `local file ${pluginName}.plugin.js`
},
{
location: path.join(localPath, pluginName),
origin: `local plugin ${pluginName}`
},
{
location: path.join(localPath, `relaxed-${pluginName}`),
origin: `local relaxed-${pluginName}`
},
{
location: `relaxed-${pluginName}`,
origin: `relaxed-${pluginName}`
}
]
// Add global npm root path if available
var globalNpmRoot = getGlobalNpmRoot()
if (globalNpmRoot) {
possiblePaths.push({
location: path.join(globalNpmRoot, `relaxed-${pluginName}`),
origin: `global relaxed-${pluginName}`
})
}
// Add common fallback paths
var fallbackPaths = [
{
// macOS homebrew (Apple Silicon)
location: `/opt/homebrew/lib/node_modules/relaxed-${pluginName}`,
origin: `relaxed-${pluginName}`
},
{
// linux & macOS homebrew (Intel)
location: `/usr/local/lib/node_modules/relaxed-${pluginName}`,
origin: `relaxed-${pluginName}`
},
{
// travis
location: `/home/travis/build/RelaxedJS/relaxed-${pluginName}`,
origin: `relaxed-${pluginName}`
}
]
possiblePaths = possiblePaths.concat(fallbackPaths)
for (var possiblePath of possiblePaths) {
try {
plugin = require(possiblePath.location)
origin = possiblePath.origin
break
} catch (error) {
var expected = `Cannot find module '${possiblePath.location}'`
if (error.message.indexOf(expected) === -1) {
console.error(error)
}
}
}
}
if (!plugin) {
throw Error(`Plugin ${pluginName} not found !`)
}
var configuratedPlugin = await plugin.constructor(parameters)
configuratedPlugin.origin = origin
return configuratedPlugin
}
var listPluginHooks = function (pluginList) {
var pluginHooks = {}
var hooks = [
'watchers',
'pugHeaders',
'pugFilters',
'headElements',
'htmlModifiers',
'pageModifiers',
'page2ndModifiers',
'postPDF'
]
for (var hook of hooks) {
var hookInstances = []
for (var plugin of pluginList) {
try {
var thisPluginHooks = plugin[hook]
if (thisPluginHooks) {
if (!Array.isArray(thisPluginHooks)) {
thisPluginHooks = [thisPluginHooks]
}
for (var pluginHook of thisPluginHooks) {
hookInstances.push({
instance: pluginHook,
origin: plugin.origin
})
}
}
} catch (error) {
console.log(`In hook ${hook} of plugin [${plugin.origin}]:`)
console.log(error.message)
throw error
}
}
pluginHooks[hook] = hookInstances
}
// TODO: order watchers by watched extension inclusion.
return pluginHooks
}
var updateRegisteredPlugins = async function (relaxedGlobals, inputDir) {
if (relaxedGlobals.config.plugins) {
console.log(colors.magenta('... Loading config plugins'))
var plugin, pluginName, params
for (var pluginDefinition of relaxedGlobals.config.plugins) {
try {
if (typeof (pluginDefinition) === 'string') {
[pluginName, params] = [pluginDefinition, {}]
} else {
[pluginName, params] = Object.entries(pluginDefinition)[0]
}
console.log(colors.magenta(` - ${pluginName} plugin`))
plugin = await createConfigPlugin(pluginName, params, inputDir)
relaxedGlobals.configPlugins.push(plugin)
} catch (error) {
console.log(error.message)
console.error(colors.bold.red(`Could not load plugin ${pluginName}`))
}
}
}
var allPlugins = relaxedGlobals.configPlugins.concat(builtinPlugins.defaultPlugins)
relaxedGlobals.pluginHooks = listPluginHooks(allPlugins)
// TODO: remove some of these extensions as they get covered by plugins.
relaxedGlobals.watchedExtensions = [
'.pug',
'.md',
'.html',
'.css',
'.scss',
'.svg',
'.png',
'.jpeg',
'.jpg'
]
for (var watcher of relaxedGlobals.pluginHooks.watchers) {
var exts = watcher.instance.extensions
relaxedGlobals.watchedExtensions = relaxedGlobals.watchedExtensions.concat(exts)
}
}
exports.updateRegisteredPlugins = updateRegisteredPlugins
exports.listPluginHooks = listPluginHooks
exports.createConfigPlugin = createConfigPlugin