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.
 
 
 
 
 
 

131 satır
2.8 KiB

  1. const path = require("path");
  2. const fs = require("fs");
  3. const chalk = require("chalk");
  4. const frappe_path = path.resolve(__dirname, "..");
  5. const bench_path = path.resolve(frappe_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(
  30. public_js_path,
  31. js_file
  32. );
  33. }
  34. }
  35. return out;
  36. }, {});
  37. const get_public_path = app => public_paths[app];
  38. const get_build_json_path = app =>
  39. path.resolve(get_public_path(app), "build.json");
  40. function get_build_json(app) {
  41. try {
  42. return require(get_build_json_path(app));
  43. } catch (e) {
  44. // build.json does not exist
  45. return null;
  46. }
  47. }
  48. function delete_file(path) {
  49. if (fs.existsSync(path)) {
  50. fs.unlinkSync(path);
  51. }
  52. }
  53. function run_serially(tasks) {
  54. let result = Promise.resolve();
  55. tasks.forEach(task => {
  56. if (task) {
  57. result = result.then ? result.then(task) : Promise.resolve();
  58. }
  59. });
  60. return result;
  61. }
  62. const get_app_path = app => app_paths[app];
  63. function get_apps_list() {
  64. return fs
  65. .readFileSync(path.resolve(sites_path, "apps.txt"), {
  66. encoding: "utf-8"
  67. })
  68. .split("\n")
  69. .filter(Boolean);
  70. }
  71. function get_cli_arg(name) {
  72. let args = process.argv.slice(2);
  73. let arg = `--${name}`;
  74. let index = args.indexOf(arg);
  75. let value = null;
  76. if (index != -1) {
  77. value = true;
  78. }
  79. if (value && args[index + 1]) {
  80. value = args[index + 1];
  81. }
  82. return value;
  83. }
  84. function log_error(message, badge = "ERROR") {
  85. badge = chalk.white.bgRed(` ${badge} `);
  86. console.error(`${badge} ${message}`);
  87. }
  88. function log_warn(message, badge = "WARN") {
  89. badge = chalk.black.bgYellowBright(` ${badge} `);
  90. console.warn(`${badge} ${message}`);
  91. }
  92. function log(...args) {
  93. console.log(...args);
  94. }
  95. module.exports = {
  96. app_list,
  97. bench_path,
  98. assets_path,
  99. sites_path,
  100. apps_path,
  101. bundle_map,
  102. get_public_path,
  103. get_build_json_path,
  104. get_build_json,
  105. get_app_path,
  106. delete_file,
  107. run_serially,
  108. get_cli_arg,
  109. log,
  110. log_warn,
  111. log_error
  112. };