This commit is contained in:
Jack Zhuang 2019-03-04 17:41:57 +08:00
parent 1dff9d5b41
commit 793f00acc4
7 changed files with 320 additions and 0 deletions

61
.gitignore vendored Normal file
View File

@ -0,0 +1,61 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next

177
app/index.js Normal file
View File

@ -0,0 +1,177 @@
const _ = require("underscore");
const fs = require("fs");
const path = require("path");
const yaml = require('js-yaml');
const loadToDB = require('../lib/load_to_db');
const appFileName = 'app.yml';
const objectFolderName = 'objects';
const triggerFolderName = 'triggers';
const actionFolderName = 'actions';
const reportFolderName = 'reports';
const loadJSONFile = (filePath)=>{
return JSON.parse(fs.readFileSync(filePath, 'utf8').normalize('NFC'));
}
const loadYmlFile = (filePath)=>{
return yaml.safeLoad(fs.readFileSync(filePath, 'utf8'));
}
const loadJSFile = (filePath)=>{
return require(filePath);
}
const loadFile = (filePath)=>{
try {
let extname = path.extname(filePath);
if(extname.toLocaleLowerCase() == '.json')
return loadJSONFile(filePath);
else if(extname.toLocaleLowerCase() == '.yml')
return loadYmlFile(filePath);
} catch (error) {
console.error('loadFile error', filePath, error);
return {}
}
}
const loadApp = (appFilePath)=>{
let app = loadFile(appFilePath);
if(!_.isEmpty(app)){
Creator.Apps[ app._id || app.name] = app;
}
}
const loadObjectInCreator = (object)=>{
if(!_.isEmpty(object)){
Creator.Objects[object.name] = object;
Creator.fiberLoadObjects(object);
}
}
const loadObject = (objectPath)=>{
let obj =loadFile(objectPath);
loadObjectInCreator(obj)
}
//TODO afterUndeletebeforeSubmit
const triggerMapping = {
beforeInsert: {
on: 'server',
when: 'before.insert'
},
beforeUpdate: {
on: 'server',
when: 'before.update'
},
beforeDelete: {
on: 'server',
when: 'before.remove'
},
afterInsert: {
on: 'server',
when: 'after.insert'
},
afterUpdate: {
on: 'server',
when: 'after.update'
},
afterDelete: {
on: 'server',
when: 'after.remove'
}
}
const loadTrigger = (triggerPath)=>{
let trigger = loadJSFile(triggerPath);
let object_name = trigger.object_name
if(!object_name){
console.error(`load trigger errorMissing attribute 'object_name' /r ${triggerPath}`)
return
}
let object = Creator.Objects[object_name]
if(!object){
console.error(`load trigger errorInvalid 'object_name' /r ${triggerPath}`)
return
}
if(!object.triggers){
object.triggers = {}
}
_.each(trigger, (attr, key)=>{
let tm = triggerMapping[key]
if(!_.isEmpty(tm)){
let tkey = `_${key}`.toLocaleUpperCase();
object.triggers[tkey] = _.extend({}, tm, {
todo: attr
})
}
})
loadObjectInCreator(object);
}
const loadReport = (reportPath)=>{
let report = loadFile(reportPath);
Creator.Reports[report._id] = report;
loadToDB.loadReports(report);
}
exports.load = function(srcDirectory) {
if(fs.existsSync(srcDirectory) && fs.statSync(srcDirectory).isDirectory()){
console.log('Loading app ... ', srcDirectory);
fs.readdir(srcDirectory, (err, appFiles)=>{
//读取 app
const appFilePath = path.join(srcDirectory, appFileName);
if(fs.existsSync(appFilePath)){
loadApp(appFilePath);
// fs.watchFile(appFilePath, (curr, prev) => {
// loadApp(appFilePath);
// });
}
//读取 object
const objectFolderPath = path.join(srcDirectory, objectFolderName);
if(fs.existsSync(objectFolderPath)){
fs.readdir(objectFolderPath, (err, objectFiles)=>{
_.each(objectFiles, (aof)=>{
const objectPath = path.join(objectFolderPath, aof);
loadObject(objectPath);
// fs.watchFile(objectPath, (curr, prev) => {
// loadObject(objectPath);
// });
})
})
}
//读取 triggers
const triggerFolderPath = path.join(srcDirectory, triggerFolderName);
if (fs.existsSync(triggerFolderPath)) {
fs.readdir(triggerFolderPath, (err, triggerFiles)=>{
_.each(triggerFiles, (tf)=>{
const triggerPath = path.join(triggerFolderPath, tf);
loadTrigger(triggerPath);
})
})
}
//读取 reports
const reportFolderPath = path.join(srcDirectory, reportFolderName);
if (fs.existsSync(reportFolderPath)) {
fs.readdir(reportFolderPath, (err, reportFiles)=>{
_.each(reportFiles, (rf)=>{
const reportPath = path.join(reportFolderPath, rf);
loadReport(reportPath);
})
})
}
})
//监控apps文件夹变化
// fs.watch(srcDirectory, (eventType, filename)=>{
// console.warn('文件修改未生效,请重启服务', eventType, filename);
// })
}
}

38
app/load_to_db.js Normal file
View File

@ -0,0 +1,38 @@
// fix: Meteor code must always run within a Fiber.
var Fiber = require("fibers");
exports.loadObject = function (obj) {
var collection = Creator.getCollection('objects');
Fiber(function () {
collection.upsert({
name: obj.name
}, {
$set: obj
});
}).run();
}
exports.loadTrigger = function (trigger) {
var collection = Creator.getCollection('objects');
Fiber(function () {
collection.upsert({
name: trigger.name
}, {
$set: trigger
});
}).run();
}
exports.loadReports = function (report) {
var collection = Creator.getCollection('reports');
if (!report.space) {
report.space = 'global';
}
Fiber(function () {
collection.upsert({
_id: report._id
}, {
$set: report
});
}).run();
}

15
index.js Normal file
View File

@ -0,0 +1,15 @@
// const path = require("path")
// const projectDir = path.resolve(__dirname, '../../../')
// const {steedos} = require(`${projectDir}/package.json`);
// const steedos_plugins = steedos.plugins
// // const projectNodeModules = path.join(projectDir, 'node_modules');
// //先加载plugin, 再加载本地app
// _.each(steedos_plugins, (plugin)=>{
// // require(path.join(projectNodeModules,plugin));
// require(require.resolve(plugin))
// })
exports.load = require("./app").load;

BIN
package-lock.json generated Normal file

Binary file not shown.

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "@steedos/core",
"version": "1.1.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@steedos/meteor-bundle-runner": "^1.1.4",
"fibers": "^3.1.1",
"js-yaml": "3.12.1",
"underscore": "1.5.2"
},
"publishConfig": {
"access": "public"
}
}

9
server.js Normal file
View File

@ -0,0 +1,9 @@
var server = require('@steedos/meteor-bundle-runner');
server.Fiber(function () {
server.Profile.run("Server startup", function () {
server.loadServerBundles();
server.callStartupHooks();
server.runMain();
});
}).run();