app.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * 全局定义
  3. * @param app
  4. */
  5. 'use strict';
  6. global.CODE = require('./app/const/statusCode');
  7. const fs = require('fs');
  8. const path = require('path');
  9. const lowdb = require('lowdb');
  10. const FileSync = require('lowdb/adapters/FileSync');
  11. const utils = require('./app/utils/utils');
  12. const os = require('os');
  13. const storageDir = path.normalize(os.homedir() + '/electron-egg-storage/');
  14. class AppBootHook {
  15. constructor(app) {
  16. this.app = app;
  17. global.OS_PLATFORM = process.platform;
  18. global.IS_WIN = /^win/.test(process.platform);
  19. }
  20. configWillLoad() {
  21. // Ready to call configDidLoad,
  22. // Config, plugin files are referred,
  23. // this is the last chance to modify the config.
  24. }
  25. configDidLoad() {
  26. // Config, plugin files have been loaded.
  27. }
  28. async didLoad() {
  29. // All files have loaded, start plugin here.
  30. }
  31. async willReady() {
  32. // All plugins have started, can do some thing before app ready
  33. }
  34. async didReady() {
  35. // Worker is ready, can do some things
  36. // don't need to block the app boot.
  37. // 数据库
  38. if (!fs.existsSync(storageDir)) {
  39. utils.mkdir(storageDir);
  40. utils.chmodPath(storageDir, '777');
  41. }
  42. const file = storageDir + 'db.json';
  43. const adapter = new FileSync(file);
  44. const db = lowdb(adapter);
  45. if (!db.has('default').value()) {
  46. db.set('default', {}).write();
  47. }
  48. }
  49. async serverDidReady() {
  50. // Server is listening.
  51. // const storageFile = './storage';
  52. // utils.chmodPath(storageFile, '777');
  53. }
  54. async beforeClose() {
  55. // Do some thing before app close.
  56. }
  57. }
  58. module.exports = AppBootHook;