25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

2 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const path = require("path");
  2. const fs = require("fs");
  3. const chalk = require("chalk");
  4. const xhiveframework_path = path.resolve(__dirname, "..");
  5. const bench_path = path.resolve(xhiveframework_path, "..", "..");
  6. const sites_path = path.resolve(bench_path, "sites");
  7. const apps_path = path.resolve(bench_path, "apps");
  8. const assets_path = path.resolve(sites_path, "assets");
  9. const app_list = get_apps_list();
  10. const app_paths = app_list.reduce((out, app) => {
  11. out[app] = path.resolve(apps_path, app, app);
  12. return out;
  13. }, {});
  14. const public_paths = app_list.reduce((out, app) => {
  15. out[app] = path.resolve(app_paths[app], "public");
  16. return out;
  17. }, {});
  18. const public_js_paths = app_list.reduce((out, app) => {
  19. out[app] = path.resolve(app_paths[app], "public/js");
  20. return out;
  21. }, {});
  22. const bundle_map = app_list.reduce((out, app) => {
  23. const public_js_path = public_js_paths[app];
  24. if (fs.existsSync(public_js_path)) {
  25. const all_files = fs.readdirSync(public_js_path);
  26. const js_files = all_files.filter((file) => file.endsWith(".js"));
  27. for (let js_file of js_files) {
  28. const filename = path.basename(js_file).split(".")[0];
  29. out[path.join(app, "js", filename)] = path.resolve(public_js_path, js_file);
  30. }
  31. }
  32. return out;
  33. }, {});
  34. const get_public_path = (app) => public_paths[app];
  35. const get_build_json_path = (app) => path.resolve(get_public_path(app), "build.json");
  36. function get_build_json(app) {
  37. try {
  38. return require(get_build_json_path(app));
  39. } catch (e) {
  40. // build.json does not exist
  41. return null;
  42. }
  43. }
  44. function delete_file(path) {
  45. if (fs.existsSync(path)) {
  46. fs.unlinkSync(path);
  47. }
  48. }
  49. function run_serially(tasks) {
  50. let result = Promise.resolve();
  51. tasks.forEach((task) => {
  52. if (task) {
  53. result = result.then ? result.then(task) : Promise.resolve();
  54. }
  55. });
  56. return result;
  57. }
  58. const get_app_path = (app) => app_paths[app];
  59. function get_apps_list() {
  60. return fs
  61. .readFileSync(path.resolve(sites_path, "apps.txt"), {
  62. encoding: "utf-8",
  63. })
  64. .split("\n")
  65. .filter(Boolean);
  66. }
  67. function get_cli_arg(name) {
  68. let args = process.argv.slice(2);
  69. let arg = `--${name}`;
  70. let index = args.indexOf(arg);
  71. let value = null;
  72. if (index != -1) {
  73. value = true;
  74. }
  75. if (value && args[index + 1]) {
  76. value = args[index + 1];
  77. }
  78. return value;
  79. }
  80. function log_error(message, badge = "ERROR") {
  81. badge = chalk.white.bgRed(` ${badge} `);
  82. console.error(`${badge} ${message}`); // eslint-disable-line no-console
  83. }
  84. function log_warn(message, badge = "WARN") {
  85. badge = chalk.black.bgYellowBright(` ${badge} `);
  86. console.warn(`${badge} ${message}`); // eslint-disable-line no-console
  87. }
  88. function log(...args) {
  89. console.log(...args); // eslint-disable-line no-console
  90. }
  91. function get_redis_subscriber(kind) {
  92. // get redis subscriber that aborts after 10 connection attempts
  93. let retry_strategy;
  94. let { get_redis_subscriber: get_redis, get_conf } = require("../node_utils");
  95. if (process.env.CI == 1 || get_conf().developer_mode == 0) {
  96. retry_strategy = () => {};
  97. } else {
  98. retry_strategy = function (options) {
  99. // abort after 5 x 3 connection attempts ~= 3 seconds
  100. if (options.attempt > 4) {
  101. return undefined;
  102. }
  103. return options.attempt * 100;
  104. };
  105. }
  106. return get_redis(kind, { retry_strategy });
  107. }
  108. module.exports = {
  109. app_list,
  110. bench_path,
  111. assets_path,
  112. sites_path,
  113. apps_path,
  114. bundle_map,
  115. get_public_path,
  116. get_build_json_path,
  117. get_build_json,
  118. get_app_path,
  119. delete_file,
  120. run_serially,
  121. get_cli_arg,
  122. log,
  123. log_warn,
  124. log_error,
  125. get_redis_subscriber,
  126. };