/
plugin.js
210 lines (182 loc) · 4.02 KB
/
plugin.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const fs = require('fs')
const path = require('path')
const yaml = require('yaml')
const chokidar = require('chokidar')
// const depTree = require('deptree')
var plugins = []
/*
@struct plugin = {
name: 'relaxed-example-plugin',
mixin: 'path/to/file' | 'mixin ...',
watch: {
priority: 0, // optional, not implemented
before: [], // optional, not implemented
after: [], // optional, not implemented
extensions: ['.example.txt'],
handler: async function(path, file) {...}
},
htmlFilter: {
priority: 0,
before: [],
after: [],
handler: async function(html) {...}
},
page1stPassFilter: {
priority: 0,
before: [],
after: [],
handler: async function(page) {...}
},
page2ndPassFilter: {
priority: 0,
before: [],
after: [],
handler: async function(page) {...}
}
*/
var pluginSettings = {}
/*
@struct 'plugin-name': settings
*/
function _requirePlugins(list) {
for (var plugin of list) {
// Not sure how I am going to pass settings for plugins
// Need to figure out how to do dependencies with plugin package.json
try {
let alreadyPresent = false
for (var p of plugins) {
if (p.name == plugin.name) {
alreadyPresent = true
break
}
}
if (alreadyPresent) {
continue
}
var loaded = require(`relaxed-${plugin.name}`)
loaded.name = plugin.name
loaded.settings = plugin.settings
if(loaded.public) {
pluginSettings[plugin.name] = loaded.public
}
plugins.push(loaded)
} catch(error) {
console.log(error)
}
}
for (var loadedPlugin of plugins) {
let present = false
for(var item of list) {
if (item.name == loadedPlugin.name) {
present = true
break
}
}
if (present) {
continue
} else {
delete plugins[plugins.indexOf(loadedPlugin)]
}
}
}
function _handleComments(file) {
var variables = function(string) {
var settings = {}
var re = /([\w-_]+)=([\w-_]+)/gm
var match
while(match = re.exec(string)) {
settings[match[1]] = match[2]
}
return settings
}
var data = fs.readFileSync(file)
var re = /\/\/-\suse-plugin:\s([\w-_]+)(.*)/gm
var match
var list = []
while(match = re.exec(data)) {
let obj = {}
if (match[2]) {
obj.settings = variables(match[2])
}
obj.name = match[1]
list.push(obj)
}
return _requirePlugins(list)
}
function _handleConfig(file) {
var config
switch(path.extname(file)) {
case 'yml':
config = yaml.eval(fs.readFileSync(file))
break
case 'yaml':
config = yaml.eval(fs.readFileSync(file))
break
case 'json':
config = require(file)
break
}
if (!config.plugins) {
return false
}
var list = []
for (var plugin in config.plugins) {
list.push(plugin)
}
return _requirePlugins(list)
}
function _activatePlugins() {
for (var plugin of plugins) {
if(plugin.activate) {
plugin.activate(plugin.settings, plugins)
}
}
}
function _loadPlugins(file) {
if (['yml', 'yaml', 'json'].indexOf(path.extname(file)) != -1) {
_handleConfig(file)
} else {
_handleComments(file)
}
_activatePlugins()
}
exports.loadPlugins = function(file, watch=false) {
if (watch) {
chokidar.watch(file, {
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 100
}
})
.on('change', filepath => {
_loadPlugins(filepath)
})
} else {
_loadPlugins(file)
}
}
exports.get = function(type) {
if (type == 'mixins') {
var mixins = ''
for (var plugin of plugins) {
if (plugin[type]) {
for(var mixin of plugin[type]) {
if (!fs.existsSync(mixin)) {
mixins += mixin + '\n'
} else {
mixins += fs.readFileSync(mixin) + '\n'
}
}
}
}
return mixins
} else {
var list = []
for(var plugin of plugins) {
if (plugin[type]) {
list.push(plugin[type])
}
}
return list
}
}