app.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 pkg = require('./package.json');
  14. const storageDir = path.normalize(os.userInfo().homedir + '/' + pkg.name + '/');
  15. const storageDb = 'db.json';
  16. class AppBootHook {
  17. constructor(app) {
  18. this.app = app;
  19. global.OS_PLATFORM = process.platform;
  20. global.IS_WIN = /^win/.test(process.platform);
  21. }
  22. configWillLoad() {
  23. // Ready to call configDidLoad,
  24. // Config, plugin files are referred,
  25. // this is the last chance to modify the config.
  26. }
  27. configDidLoad() {
  28. // Config, plugin files have been loaded.
  29. }
  30. async didLoad() {
  31. // All files have loaded, start plugin here.
  32. }
  33. async willReady() {
  34. // All plugins have started, can do some thing before app ready
  35. }
  36. async didReady() {
  37. // Worker is ready, can do some things
  38. // don't need to block the app boot.
  39. // 数据库
  40. if (!fs.existsSync(storageDir)) {
  41. utils.mkdir(storageDir);
  42. utils.chmodPath(storageDir, '777');
  43. }
  44. const file = storageDir + storageDb;
  45. const adapter = new FileSync(file);
  46. const db = lowdb(adapter);
  47. if (!db.has('default').value()) {
  48. db.set('default', {}).write();
  49. }
  50. }
  51. async serverDidReady() {
  52. // Server is listening.
  53. // const storageFile = './storage';
  54. // utils.chmodPath(storageFile, '777');
  55. }
  56. async beforeClose() {
  57. // Do some thing before app close.
  58. }
  59. }
  60. module.exports = AppBootHook;