You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/lib/loader.js

50 lines
1.1 KiB
JavaScript

const fs = require('fs');
const path = require('path');
let installed;
function root(name) {
return path.resolve(process.cwd(), name);
}
function exist(name) {
try {
fs.statSync(root(name));
} catch (e) {
return false;
}
return true;
}
5 years ago
const superRequire = (name) => {
let m;
try {
m = require(root(name)); // eslint-disable-line import/no-dynamic-require
} catch (e) {
m = __non_webpack_require__(root(name)); // eslint-disable-line no-undef
}
return m;
};
async function prepare() {
installed = fs.readdirSync(root('.build/module'));
}
async function handler() {
for (const i of installed) {
if (exist(`.build/module/${i}/handler.js`)) {
superRequire(`.build/module/${i}/handler.js`);
5 years ago
console.log(`Handler init: ${i}`);
}
}
}
async function model() {
for (const i of installed) {
if (exist(`.build/module/${i}/model.js`)) {
superRequire(`.build/module/${i}/model.js`);
5 years ago
console.log(`Model init: ${i}`);
}
}
}
global.Hydro['lib.loader'] = module.exports = { prepare, handler, model };