server.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. let app = require('express')();
  2. let port = process.env.port || process.env.PORT || 8291;
  3. const { exec } = require("child_process");
  4. const fs = require('fs')
  5. app.listen(port, () => {
  6. console.log('server app listen http://localhost:' + port)
  7. })
  8. function require2(url) {
  9. try {
  10. return require(url)
  11. } catch (e) {
  12. }
  13. }
  14. // setInterval(() => {
  15. // start();
  16. // }, 2000)
  17. require2('./l5_TASK_RUN/start.sh')
  18. require2('./l4-LOCAL_VS_CODE/start.sh')
  19. require2('./l4-LOCAL_VS_CODE/logs.sh')
  20. require2('./41start.sh')
  21. console.log('xxx')
  22. start();
  23. function ep(command) {
  24. return new Promise((resolve, reject) => {
  25. exec(command, (error, stdout, stderr) => {
  26. if (error) {
  27. reject(`Error: ${error.message}`);
  28. return;
  29. }
  30. if (stderr) {
  31. reject(`Stderr: ${stderr}`);
  32. return;
  33. }
  34. resolve(stdout);
  35. }, e => {
  36. console.log('aaa')
  37. })
  38. });
  39. }
  40. async function gzipFolder(folderPath, outputFilePath) {
  41. try {
  42. // Create a .tar.gz archive of the folder
  43. await ep(`tar -czf ${outputFilePath} -C ${folderPath} .`);
  44. // Get the size of the gzipped file
  45. const stats = fs.statSync(outputFilePath);
  46. const fileSizeInBytes = stats.size;
  47. const fileSizeInKB = fileSizeInBytes / 1024;
  48. const fileSizeInMB = fileSizeInKB / 1024;
  49. console.log(`Gzipped file created: ${outputFilePath}`);
  50. console.log(`Size: ${fileSizeInBytes} bytes (${fileSizeInKB.toFixed(2)} KB / ${fileSizeInMB.toFixed(2)} MB)`);
  51. return (fileSizeInBytes / 1000).toFixed(1);
  52. } catch (error) {
  53. console.error("Error:", error);
  54. }
  55. }
  56. function getRandomSymbol() {
  57. const characters = "abcdefghijklmnopqrstuvwxyz0123456789"; // a-z0-9
  58. const randomIndex = Math.floor(Math.random() * characters.length);
  59. return characters[randomIndex];
  60. }
  61. let execPromise = ep;
  62. async function start(){
  63. let url = '/rdata/runs/custom/'
  64. let url2 = '/rdata/runs/custom2/'
  65. let url3 = '/rdata/runs/custom3/'
  66. let files = ['server.js', 'main.js']
  67. let filePath = url + files[0];
  68. await ep(`rm -rf ${url}/.git`)
  69. await ep(`rm -rf ${url2}/.git`)
  70. await ep(`rm -rf ${url3}/.git`)
  71. await ep(`cd ${url} && git config --global init.defaultBranch main`)
  72. await ep(`cd ${url} && git init . && git add . && git commit -a -m"init"`)
  73. await ep(`cd ${url2} && rm -rf * && touch test.md && git init . && git add . && git commit -a -m"init"`)
  74. await ep(`cd ${url3} && rm -rf * && touch test.md && git init . && git add . && git commit -a -m"init"`)
  75. let commitId1 = await ep(`cd ${url} && git rev-parse HEAD`)
  76. fs.writeFileSync(filePath, '')
  77. let v = await gzipFolder(url, '/rdata/tmp/rzip.gzip')
  78. let lineLength = 7
  79. for (let i = 1; i < 300; i++) {
  80. await fs.appendFileSync(filePath, getRandomSymbol() + getRandomSymbol() + getRandomSymbol() + (i%lineLength == 0 ? '\n': ''));
  81. await ep(`cd ${url} && git diff --unified=0 > ${url3}safe.txt`)
  82. let v1 = await ep(`cd ${url} && git diff --unified=0 ${commitId1} > ${url2}safe.txt`)
  83. fs.writeFileSync(url2 + `safe.txt`, v1)
  84. let v = await ep(`cd ${url} && git commit -a -m'${i}'`)
  85. await ep(`cd ${url2} && git add . && git commit -a -m'.'`)
  86. await ep(`cd ${url3} && git add . && git commit -a -m'.'`)
  87. console.log("i", i)
  88. }
  89. // let hist = await ep(`cd ${url} && git log -n 10`)
  90. let v1 = await gzipFolder(url, '/rdata/tmp/rzip.gzip')
  91. let v2 = await gzipFolder(url2, '/rdata/tmp/rzip2.gzip')
  92. let v3 = await gzipFolder(url3, '/rdata/tmp/rzip3.gzip')
  93. console.log("v",{v1, v2, v3})
  94. // exec('sh l4-LOCAL_VS_CODE/START_INSIDE_DOCKER.sh', (e, out, err ) => {
  95. // console.log('vvvv', out)
  96. // })
  97. }