.manifest.js 4.2 KB

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