.manifest.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* eslint-disable no-param-reassign */
  2. /* eslint-disable global-require */
  3. /* eslint-disable no-magic-numbers */
  4. "use strict";
  5. // ignore these files for making manifest
  6. const IGNORED_FILES = [
  7. "package-lock.json",
  8. "module.json",
  9. "manifest.json",
  10. "manifest-generator.js",
  11. "manifest-generator.bat",
  12. "manifest-generator.exe",
  13. "node.exe"
  14. ];
  15. // ignore files/folders which start with these characters: i.e. ".git" or "_old"
  16. const IGNORED_CHARACTERS = [
  17. ".",
  18. "_"
  19. ];
  20. ////////////////////
  21. // CODE //
  22. ////////////////////
  23. const crypto = require("crypto"),
  24. fs = require("fs"),
  25. path = require("path");
  26. // set directory to launch argument or local directory
  27. let directory = __dirname;
  28. if (process.argv[2]) {
  29. directory = process.argv[2];
  30. // check if valid directory
  31. try {
  32. fs.readdirSync(directory, "utf8");
  33. }
  34. catch (err) {
  35. return;
  36. }
  37. }
  38. // read existing module.json
  39. let modulejson = null;
  40. try {
  41. modulejson = require(path.join(directory, "module.json"));
  42. }
  43. catch (error) {
  44. modulejson = {};
  45. }
  46. modulejson.disableAutoUpdate = false;
  47. modulejson.name = "fps-manager";
  48. modulejson.options = {
  49. niceName: "FPM",
  50. guiName: "FPS Manager",
  51. };
  52. modulejson.author = "SaltyMonkey";
  53. modulejson.description = "FPS Manager is modern and extremely customizable module for Tera Toolbox which can help to solve annoynce by some ingame messages and fps drops.";
  54. modulejson.servers = ["https://raw.githubusercontent.com/SaltyMonkey/fps-manager/master/"];
  55. modulejson.supportUrl = ["https://github.com/SaltyMonkey/fps-manager/issues"];
  56. fs.writeFileSync(path.join(directory, "module.json"), jsonify(modulejson), "utf8");
  57. // read existing manifest.json
  58. let manifest = null;
  59. try {
  60. // sanitize input
  61. manifest = require(path.join(directory, "manifest.json"));
  62. if (manifest && typeof manifest === "object") {
  63. if (!manifest.files) manifest.files = {};
  64. }
  65. else {
  66. manifest = {
  67. "files": {}
  68. };
  69. }
  70. }
  71. catch (error) {
  72. // make new manifest
  73. manifest = {
  74. "files": {}
  75. };
  76. }
  77. // delete removed file entries
  78. let checking = 0;
  79. for (let entry of Object.keys(manifest.files)) {
  80. // check if file exists
  81. checking += 1;
  82. fs.access(path.join(directory, entry), fs.constants.F_OK, (err) => {
  83. checking -= 1;
  84. if (err) delete manifest.files[entry];
  85. checkProg();
  86. return;
  87. });
  88. }
  89. let reading = 0;
  90. getFiles();
  91. // get all files in folder and subfolder
  92. function getFiles(relativePath = "", files) {
  93. let dir = path.join(directory, relativePath);
  94. if (!files) files = fs.readdirSync(dir, "utf8");
  95. for (let file of files) {
  96. // if not ignored file or begins with ignored character
  97. if (!IGNORED_FILES.includes(file) && !IGNORED_CHARACTERS.includes(file[0])) {
  98. reading += 1;
  99. fs.readdir(path.join(dir, file), "utf8", (err, moreFiles) => {
  100. if (moreFiles) {
  101. getFiles(path.join(relativePath, file), moreFiles);
  102. }
  103. else {
  104. getHash(path.join(relativePath, file));
  105. }
  106. reading -= 1;
  107. checkProg();
  108. });
  109. }
  110. }
  111. }
  112. // get sha256 hash
  113. function getHash(file, type = "sha256") {
  114. file = file.replace(/\\/g, "/");
  115. if (manifest.files[file] && typeof manifest.files[file] === "object") {
  116. manifest.files[file].hash = crypto.createHash(type).update(fs.readFileSync(path.join(directory, file))).digest("hex");
  117. }
  118. else {
  119. manifest.files[file] = crypto.createHash(type).update(fs.readFileSync(path.join(directory, file))).digest("hex");
  120. }
  121. }
  122. // JSON.stringify but make lists single line
  123. function jsonify(obj) {
  124. obj = JSON.stringify(obj, null, " ");
  125. let lists = obj.match(/\[[^]+?\].*/igm);
  126. if (lists) for (let list of lists) {
  127. obj = obj.substring(0,obj.indexOf(list)) + list.replace(/[ \n\t]*/igm, "") + obj.substring(obj.indexOf(list) + list.length);
  128. }
  129. return obj;
  130. }
  131. // alphabetize object keys
  132. function alphabetizeObject(obj) {
  133. let keys = Object.keys(obj);
  134. keys.sort();
  135. let newObj = {};
  136. for (let key of keys) {
  137. newObj[key] = obj[key];
  138. }
  139. return newObj;
  140. }
  141. // check if process completed
  142. function checkProg() {
  143. if (reading === 0 && checking === 0) {
  144. manifest.files = alphabetizeObject(manifest.files);
  145. fs.writeFileSync(path.join(directory, "manifest.json"), jsonify(manifest), "utf8");
  146. }
  147. }