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/build/buildModule.js

76 lines
2.4 KiB
JavaScript

/* eslint-disable import/no-extraneous-dependencies */
const fs = require('fs');
const webpack = require('webpack');
const yaml = require('js-yaml');
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
const root = require('./root');
const template = require('./template');
const exist = (name) => {
try {
fs.statSync(root(name));
} catch (e) {
return false;
}
return true;
};
const build = async (type) => {
const modules = fs.readdirSync(root('module'));
const config = {
mode: type,
entry: {},
output: {
filename: 'module/[name].js',
path: root('.build'),
},
target: 'node',
module: {},
plugins: [
new webpack.ProgressPlugin(),
new FriendlyErrorsPlugin(),
],
};
for (const i of modules) {
if (!i.startsWith('.')) {
if (exist(`module/${i}/model.js`)) {
config.entry[`${i}/model`] = root(`module/${i}/model.js`);
}
if (exist(`module/${i}/handler.js`)) {
config.entry[`${i}/handler`] = root(`module/${i}/handler.js`);
}
}
}
const compiler = webpack(config);
await new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
console.error(err.stack || err);
if (err.details) console.error(err.details);
reject();
}
if (stats.hasErrors()) process.exitCode = 1;
resolve();
});
});
for (const i of modules) {
if (!i.startsWith('.')) {
if (exist(`module/${i}/locale`)) {
const locales = fs.readdirSync(root(`module/${i}/locale`));
const lang = {};
for (const j of locales) {
const content = fs.readFileSync(root(`module/${i}/locale/${j}`)).toString();
lang[i.split('.')[0]] = yaml.safeLoad(content);
}
const file = root(`.build/module/${i}/locale.json`);
fs.writeFileSync(file, JSON.stringify(lang));
}
if (exist(`module/${i}/template`)) {
const file = root(`.build/module/${i}/template.yaml`);
fs.writeFileSync(file, yaml.safeDump(template(`module/${i}/template`)));
}
}
}
};
module.exports = build;