You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

132 rivejä
2.7 KiB

  1. const path = require("path");
  2. const fs = require("fs");
  3. const frappe_path = path.resolve(__dirname, "..");
  4. const bench_path = path.resolve(frappe_path, "..", "..");
  5. const sites_path = path.resolve(bench_path, "sites");
  6. const apps_path = path.resolve(bench_path, "apps");
  7. const assets_path = path.resolve(sites_path, "assets");
  8. const app_list = get_apps_list();
  9. const app_paths = app_list.reduce((out, app) => {
  10. out[app] = path.resolve(apps_path, app, app);
  11. return out;
  12. }, {});
  13. const public_paths = app_list.reduce((out, app) => {
  14. out[app] = path.resolve(app_paths[app], "public");
  15. return out;
  16. }, {});
  17. const public_js_paths = app_list.reduce((out, app) => {
  18. out[app] = path.resolve(app_paths[app], "public/js");
  19. return out;
  20. }, {});
  21. const bundle_map = app_list.reduce((out, app) => {
  22. const public_js_path = public_js_paths[app];
  23. if (fs.existsSync(public_js_path)) {
  24. const all_files = fs.readdirSync(public_js_path);
  25. const js_files = all_files.filter(file => file.endsWith(".js"));
  26. for (let js_file of js_files) {
  27. const filename = path.basename(js_file).split(".")[0];
  28. out[path.join(app, "js", filename)] = path.resolve(
  29. public_js_path,
  30. js_file
  31. );
  32. }
  33. }
  34. return out;
  35. }, {});
  36. const get_public_path = app => public_paths[app];
  37. const get_build_json_path = app =>
  38. path.resolve(get_public_path(app), "build.json");
  39. function get_build_json(app) {
  40. try {
  41. return require(get_build_json_path(app));
  42. } catch (e) {
  43. // build.json does not exist
  44. return null;
  45. }
  46. }
  47. function delete_file(path) {
  48. if (fs.existsSync(path)) {
  49. fs.unlinkSync(path);
  50. }
  51. }
  52. function run_serially(tasks) {
  53. let result = Promise.resolve();
  54. tasks.forEach(task => {
  55. if (task) {
  56. result = result.then ? result.then(task) : Promise.resolve();
  57. }
  58. });
  59. return result;
  60. }
  61. const get_app_path = app => app_paths[app];
  62. const get_options_for_scss = () => {
  63. const node_modules_path = path.resolve(
  64. get_app_path("frappe"),
  65. "..",
  66. "node_modules"
  67. );
  68. const app_paths = app_list
  69. .map(get_app_path)
  70. .map(app_path => path.resolve(app_path, ".."));
  71. return {
  72. includePaths: [node_modules_path, ...app_paths]
  73. };
  74. };
  75. function get_apps_list() {
  76. return fs
  77. .readFileSync(path.resolve(sites_path, "apps.txt"), {
  78. encoding: "utf-8"
  79. })
  80. .split("\n")
  81. .filter(Boolean);
  82. }
  83. function get_cli_arg(name) {
  84. let args = process.argv.slice(2);
  85. for (let i in args) {
  86. let arg = args[i];
  87. let value = null;
  88. if (arg == `--${name}`) {
  89. value = true;
  90. }
  91. if (args[i + 1]) {
  92. value = args[i + 1];
  93. }
  94. if (value) {
  95. return value;
  96. }
  97. }
  98. }
  99. module.exports = {
  100. app_list,
  101. bench_path,
  102. assets_path,
  103. sites_path,
  104. apps_path,
  105. bundle_map,
  106. get_public_path,
  107. get_build_json_path,
  108. get_build_json,
  109. get_app_path,
  110. delete_file,
  111. run_serially,
  112. get_options_for_scss,
  113. get_cli_arg
  114. };