.manifest.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "manifest.json",
  9. "settings.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. "settingsVersion": 1,
  52. "settingsFile": "./settings.json",
  53. "settingsMigrator": "./data/migrationData.js"
  54. };
  55. modulejson.author = "SaltyMonkey";
  56. modulejson.description = "FPS Manager is modern and extremely customizable module for Tera Toolbox which can help to solve annoyance by some ingame messages and fps drops.";
  57. modulejson.servers = ["https://raw.githubusercontent.com/SaltyMonkey/fps-manager/master/"];
  58. modulejson.supportUrl = ["https://github.com/SaltyMonkey/fps-manager/issues"];
  59. fs.writeFileSync(path.join(directory, "module.json"), jsonify(modulejson), "utf8");
  60. // read existing manifest.json
  61. let manifest = null;
  62. try {
  63. // sanitize input
  64. manifest = require(path.join(directory, "manifest.json"));
  65. if (manifest && typeof manifest === "object") {
  66. if (!manifest.files) manifest.files = {};
  67. }
  68. else {
  69. manifest = {
  70. "files": {}
  71. };
  72. }
  73. }
  74. catch (error) {
  75. // make new manifest
  76. manifest = {
  77. "files": {}
  78. };
  79. }
  80. // delete removed file entries
  81. let checking = 0;
  82. for (let entry of Object.keys(manifest.files)) {
  83. // check if file exists
  84. checking += 1;
  85. fs.access(path.join(directory, entry), fs.constants.F_OK, (err) => {
  86. checking -= 1;
  87. if (err) delete manifest.files[entry];
  88. checkProg();
  89. return;
  90. });
  91. }
  92. let reading = 0;
  93. getFiles();
  94. // get all files in folder and subfolder
  95. function getFiles(relativePath = "", files) {
  96. let dir = path.join(directory, relativePath);
  97. if (!files) files = fs.readdirSync(dir, "utf8");
  98. for (let file of files) {
  99. // if not ignored file or begins with ignored character
  100. if (!IGNORED_FILES.includes(file) && !IGNORED_CHARACTERS.includes(file[0])) {
  101. reading += 1;
  102. fs.readdir(path.join(dir, file), "utf8", (err, moreFiles) => {
  103. if (moreFiles) {
  104. getFiles(path.join(relativePath, file), moreFiles);
  105. }
  106. else {
  107. getHash(path.join(relativePath, file));
  108. }
  109. reading -= 1;
  110. checkProg();
  111. });
  112. }
  113. }
  114. }
  115. // get sha256 hash
  116. function getHash(file, type = "sha256") {
  117. file = file.replace(/\\/g, "/");
  118. if (manifest.files[file] && typeof manifest.files[file] === "object") {
  119. manifest.files[file].hash = crypto.createHash(type).update(fs.readFileSync(path.join(directory, file))).digest("hex");
  120. }
  121. else {
  122. manifest.files[file] = crypto.createHash(type).update(fs.readFileSync(path.join(directory, file))).digest("hex");
  123. }
  124. }
  125. // JSON.stringify but make lists single line
  126. function jsonify(obj) {
  127. obj = JSON.stringify(obj, null, " ");
  128. let lists = obj.match(/\[[^]+?\].*/igm);
  129. if (lists) for (let list of lists) {
  130. obj = obj.substring(0, obj.indexOf(list)) + list.replace(/[ \n\t]*/igm, "") + obj.substring(obj.indexOf(list) + list.length);
  131. }
  132. return obj;
  133. }
  134. // alphabetize object keys
  135. function alphabetizeObject(obj) {
  136. let keys = Object.keys(obj);
  137. keys.sort();
  138. let newObj = {};
  139. for (let key of keys) {
  140. newObj[key] = obj[key];
  141. }
  142. return newObj;
  143. }
  144. // check if process completed
  145. function checkProg() {
  146. if (reading === 0 && checking === 0) {
  147. manifest.files = alphabetizeObject(manifest.files);
  148. fs.writeFileSync(path.join(directory, "manifest.json"), jsonify(manifest), "utf8");
  149. }
  150. }