generate_webpack.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const { exec } = require("child_process");
  2. const fs = require("fs");
  3. const path = require("path");
  4. const webpack = require("webpack");
  5. const webpackConfig = require("./webpack.config");
  6. webpackConfig.entry = path.resolve(__dirname, "browser/index.js");
  7. /**
  8. * Generates a webpack build and put it in browser folder
  9. */
  10. function addBuffer(dir) {
  11. fs.readdirSync(dir).forEach((file) => {
  12. let fullPath = path.join(dir, file);
  13. if (fs.lstatSync(fullPath).isDirectory()) {
  14. addBuffer(fullPath);
  15. } else {
  16. if (
  17. (fullPath.endsWith(".ts") || fullPath.endsWith(".js")) &&
  18. (!fullPath.endsWith(".d.ts") ||
  19. fullPath.endsWith("api.d.ts") ||
  20. fullPath.endsWith("define.d.ts"))
  21. ) {
  22. const tsFile = fs.readFileSync(fullPath, "utf8");
  23. if (tsFile.includes("Buffer")) {
  24. const newTsFile = 'import { Buffer } from "buffer/";\n' + tsFile;
  25. fs.writeFileSync(fullPath, newTsFile, "utf8");
  26. }
  27. }
  28. }
  29. });
  30. }
  31. function renameFiles(dir, action) {
  32. fs.readdirSync(dir).forEach((file) => {
  33. let fullPath = path.join(dir, file);
  34. if (fs.lstatSync(fullPath).isDirectory()) {
  35. renameFiles(fullPath, action);
  36. } else {
  37. if (fullPath.includes("example")) {
  38. fs.unlinkSync(fullPath);
  39. }
  40. if (fullPath.includes("-BROWSER")) {
  41. console.log(action, fullPath);
  42. if (action === "rename") {
  43. fs.renameSync(fullPath, fullPath.replace("-BROWSER", ""));
  44. } else if (action === "delete") {
  45. fs.unlinkSync(fullPath);
  46. }
  47. }
  48. }
  49. });
  50. }
  51. function copyFolderSync(from, to) {
  52. fs.mkdirSync(to);
  53. fs.readdirSync(from).forEach((element) => {
  54. if (fs.lstatSync(path.join(from, element)).isFile()) {
  55. fs.copyFileSync(path.join(from, element), path.join(to, element));
  56. } else {
  57. copyFolderSync(path.join(from, element), path.join(to, element));
  58. }
  59. });
  60. }
  61. fs.rmSync("browser", { recursive: true, force: true });
  62. fs.rmSync("tempBrowser", { recursive: true, force: true });
  63. copyFolderSync("gramjs", "tempBrowser");
  64. addBuffer("tempBrowser");
  65. renameFiles("tempBrowser", "rename");
  66. const tsconfig = fs.readFileSync("tsconfig.json", "utf8");
  67. let newTsconfig = tsconfig.replace(/\.\/dist/g, "./browser");
  68. newTsconfig = newTsconfig.replace(/gramjs/g, "tempBrowser");
  69. fs.writeFileSync("tsconfig.json", newTsconfig, "utf8");
  70. const packageJSON = JSON.parse(fs.readFileSync("package.json", "utf8"));
  71. const oldValueStorage = packageJSON.dependencies["node-localstorage"];
  72. const oldValueSocks = packageJSON.dependencies["socks"];
  73. delete packageJSON.dependencies["node-localstorage"];
  74. delete packageJSON.dependencies["socks"];
  75. fs.writeFileSync(
  76. "package.json",
  77. JSON.stringify(packageJSON, null, " "),
  78. "utf8"
  79. );
  80. const npmi = exec("npm i");
  81. npmi.on("close", (code) => {
  82. if (code !== 0) {
  83. throw new Error("Error happened " + code);
  84. }
  85. const tsc = exec("npx tsc");
  86. tsc.stdout.on("data", function (data) {
  87. console.log("stdout: " + data.toString());
  88. });
  89. tsc.stderr.on("data", function (data) {
  90. console.error("stderr: " + data.toString());
  91. });
  92. tsc.on("close", (code) => {
  93. if (code !== 0) {
  94. throw new Error("Error happened " + code);
  95. }
  96. fs.copyFileSync("package.json", "browser/package.json");
  97. fs.copyFileSync("README.md", "browser/README.md");
  98. fs.copyFileSync("LICENSE", "browser/LICENSE");
  99. fs.copyFileSync("gramjs/tl/api.d.ts", "browser/tl/api.d.ts");
  100. fs.copyFileSync("gramjs/define.d.ts", "browser/define.d.ts");
  101. fs.rmSync("tempBrowser", { recursive: true, force: true });
  102. const tsconfig = fs.readFileSync("tsconfig.json", "utf8");
  103. let newTsconfig = tsconfig.replace(/\.\/browser/g, "./dist");
  104. newTsconfig = newTsconfig.replace(/tempBrowser/g, "gramjs");
  105. fs.writeFileSync("tsconfig.json", newTsconfig, "utf8");
  106. const packageJSON = JSON.parse(fs.readFileSync("package.json", "utf8"));
  107. packageJSON.dependencies["node-localstorage"] = oldValueStorage;
  108. packageJSON.dependencies["socks"] = oldValueSocks;
  109. fs.writeFileSync(
  110. "package.json",
  111. JSON.stringify(packageJSON, null, " "),
  112. "utf8"
  113. );
  114. webpack(webpackConfig, (err, stats) => {
  115. if (err || stats.hasErrors()) {
  116. console.log("SOME ERROR HAPPENED");
  117. process.exit(0);
  118. }
  119. if (process.env.CI) {
  120. exec("npm ci");
  121. } else {
  122. exec("npm i");
  123. }
  124. console.log(
  125. "DONE!. File created at ",
  126. path.resolve(__dirname, "browser/telegram.js")
  127. );
  128. });
  129. });
  130. });