File size: 5,694 Bytes
a2b2aac |
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 |
// inspired from https://github.com/Nurutomo/mahbod/blob/main/src/util/PluginManager.ts
import fs, { existsSync, watch } from 'fs'
import { join, resolve } from 'path'
import * as os from 'os'
import syntaxerror from 'syntax-error'
import importFile from './import.js'
import Helper from './helper.js'
const __dirname = Helper.__dirname(import.meta)
const rootDirectory = Helper.__dirname(join(__dirname, '../'))
const pluginFolder = Helper.__dirname(join(__dirname, '../plugins'))
const pluginFilter = filename => /\.(mc)?js$/.test(filename)
let watcher = {},
plugins = {},
pluginFolders = []
/**
* load files from plugin folder as plugins
* @param {string} pluginFolder
* @param {(filename: string) => boolean} pluginFilter
* @param {{
* logger: import('./connection.js').Socket['logger'];
* recursiveRead: boolean;
* }} opts if `'recursiveRead'` is true, it will load folder (call `loadPluginsFiles` function) inside pluginFolder not just load the files
*/
async function loadPluginFiles(
pluginFolder = pluginFolder,
pluginFilter = pluginFilter,
opts = { recursiveRead: false }) {
const folder = resolve(pluginFolder)
if (folder in watcher) return
pluginFolders.push(folder)
const paths = await fs.promises.readdir(pluginFolder)
await Promise.all(paths.map(async path => {
const resolved = join(folder, path)
// trim file:// prefix because lstat will throw error
const dirname = Helper.__filename(resolved, true)
const formatedFilename = formatFilename(resolved)
try {
const stats = await fs.promises.lstat(dirname)
// if folder
if (!stats.isFile()) {
// and if `recursiveRead` is true
if (opts.recursiveRead) await loadPluginFiles(dirname, pluginFilter, opts)
// return because import only can load file
return
}
// if windows it will have file:// prefix because if not it will throw error
const filename = Helper.__filename(resolved)
const isValidFile = pluginFilter(filename)
if (!isValidFile) return
const module = await importFile(filename)
if (module) plugins[formatedFilename] = module
} catch (e) {
opts.logger?.error(e, `error while requiring ${formatedFilename}`)
delete plugins[formatedFilename]
}
}))
const watching = watch(folder, reload.bind(null, {
logger: opts.logger,
pluginFolder,
pluginFilter
}))
watching.on('close', () => deletePluginFolder(folder, true))
watcher[folder] = watching
return plugins = sortedPlugins(plugins)
}
/**
* It will delete and doesn't watch the folder
* @param {string} folder ;
* @param {boolean?} isAlreadyClosed
*/
function deletePluginFolder(folder, isAlreadyClosed = false) {
const resolved = resolve(folder)
if (!(resolved in watcher)) return
if (!isAlreadyClosed) watcher[resolved].close()
delete watcher[resolved]
pluginFolders.splice(pluginFolders.indexOf(resolved), 1)
}
/**
* reload file to load latest changes
* @param {{
* logger?: import('./connection.js').Socket['logger'];
* pluginFolder?: string;
* pluginFilter?: (filename: string) => boolean;
* }} opts
* @param {*} _ev
* @param {*} filename
* @returns
*/
async function reload({
logger,
pluginFolder = pluginFolder,
pluginFilter = pluginFilter
}, _ev, filename) {
if (pluginFilter(filename)) {
// trim file:// prefix because lstat will throw exception
const file = Helper.__filename(join(pluginFolder, filename), true)
const formatedFilename = formatFilename(file)
if (formatedFilename in plugins) {
if (existsSync(file)) logger?.info(`updated plugin - '${formatedFilename}'`)
else {
logger?.warn(`deleted plugin - '${formatedFilename}'`)
return delete plugins[formatedFilename]
}
} else logger?.info(`new plugin - '${formatedFilename}'`)
const src = await fs.promises.readFile(file)
// check syntax error
let err = syntaxerror(src, filename, {
sourceType: 'module',
allowAwaitOutsideFunction: true
})
if (err) logger?.error(err, `syntax error while loading '${formatedFilename}'`)
else try {
const module = await importFile(file)
if (module) plugins[formatedFilename] = module
} catch (e) {
logger?.error(e, `error require plugin '${formatedFilename}'`)
delete plugins[formatedFilename]
} finally {
plugins = sortedPlugins(plugins)
}
}
}
/**
* `'/home/games-wabot/plugins/games/tebakgambar.js'` formated to `'plugins/games/tebakgambar.js'`
* @param {string} filename
* @returns {string}
*/
function formatFilename(filename) {
let dir = join(rootDirectory, './')
// fix invalid regular expresion when run in windows
if (os.platform() === 'win32') dir = dir.replace(/\\/g, '\\\\')
// '^' mean only replace if starts with
const regex = new RegExp(`^${dir}`)
const formated = filename.replace(regex, '')
return formated
}
/**
* Sorted plugins by of their key
* @param {{
* [k: string]: any;
* }} plugins
* @returns {{
* [k: string]: any;
* }}
*/
function sortedPlugins(plugins) {
return Object.fromEntries(Object.entries(plugins).sort(([a], [b]) => a.localeCompare(b)))
}
export {
pluginFolder,
pluginFilter,
plugins,
watcher,
pluginFolders,
loadPluginFiles,
deletePluginFolder,
reload
}
|