From ea1eb83a28baba855ad7a490bd74b133ac3fd919 Mon Sep 17 00:00:00 2001 From: Luc <8822552+luc-github@users.noreply.github.com> Date: Wed, 29 May 2024 15:10:47 +0800 Subject: [PATCH] Add Script to parse all c/cpp/h/ino files and format them using clang-format with Google style Update style to some files with clang-format using Google style Add Script to parse all embedded js/css files and format them using prettier based on .prettierrc config file Update style to embedded js/css files with prettier --- embedded/.prettierrc | 28 + embedded/config/buildassets.js | 165 +- embedded/config/buildheader.js | 89 +- embedded/config/pack_favicon.js | 25 +- embedded/config/server.js | 1233 ++++++------ embedded/config/webpack.dev.js | 96 +- embedded/config/webpack.prod.js | 166 +- embedded/src/index.js | 1494 ++++++++------- embedded/src/menu.js | 21 +- embedded/src/style.css | 408 ++-- embedded/tools/format_sources.py | 63 + esp3d/configuration.h | 28 +- esp3d/esp3d.ino | 16 +- esp3d/src/core/commands/ESP0.cpp | 3 +- esp3d/src/core/commands/ESP170.cpp | 46 +- esp3d/src/core/commands/ESP212.cpp | 8 +- esp3d/src/core/commands/ESP400.cpp | 8 +- esp3d/src/core/commands/ESP420.cpp | 10 +- esp3d/src/core/commands/ESP610.cpp | 5 +- esp3d/src/core/esp3d_benchmark.cpp | 66 +- esp3d/src/core/esp3d_benchmark.h | 7 +- esp3d/src/core/esp3d_client_types.h | 4 +- esp3d/src/core/esp3d_commands.cpp | 6 +- esp3d/src/core/esp3d_hal.cpp | 6 +- esp3d/src/core/esp3d_settings.cpp | 3 +- esp3d/src/core/esp3d_string.h | 2 +- esp3d/src/include/Marlin/cardreader.h | 80 +- esp3d/src/include/esp3d_defines.h | 150 +- esp3d/src/include/esp3d_pins.h | 36 +- esp3d/src/include/esp3d_sanity.h | 18 +- .../authentication/authentication_service.h | 3 +- esp3d/src/modules/boot_delay/boot_delay.cpp | 2 +- esp3d/src/modules/buzzer/buzzer.h | 56 +- esp3d/src/modules/camera/camera.h | 50 +- esp3d/src/modules/devices/devices_services.h | 20 +- .../OLED_I2C_SSD1306_128X64/esp3d_logo.h | 4 +- .../OLED_I2C_SSD1306_128X64/settings.h | 42 +- .../OLED_I2C_SSDSH1106_132X64/esp3d_logo.h | 4 +- .../OLED_I2C_SSDSH1106_132X64/settings.h | 38 +- .../TFT_SPI_ST7789_135X240/esp3d_logo.h | 184 +- .../display/TFT_SPI_ST7789_135X240/settings.h | 44 +- .../TFT_SPI_ST7789_240X240/esp3d_logo.h | 184 +- .../display/TFT_SPI_ST7789_240X240/settings.h | 44 +- esp3d/src/modules/ethernet/ethconfig.h | 63 +- esp3d/src/modules/filesystem/esp_filesystem.h | 1 - .../flash/littlefs_esp8266_filesystem.cpp | 1 - .../modules/filesystem/sd/sd_sdfat2_esp32.cpp | 272 +-- .../filesystem/sd/sd_sdfat2_esp8266.cpp | 276 +-- .../src/modules/filesystem/sd/sdio_esp32.cpp | 5 +- esp3d/src/modules/ftp/ExtStreaming.h | 72 +- esp3d/src/modules/ftp/FtpServer.h | 224 ++- esp3d/src/modules/gcode_host/gcode_host.cpp | 4 +- esp3d/src/modules/http/embedded.h | 1706 ++++++++++------- esp3d/src/modules/http/favicon.h | 56 +- .../modules/http/handlers/handle-SD-files.cpp | 8 +- .../http/handlers/handle-description_xml.cpp | 35 +- esp3d/src/modules/input/input.h | 24 +- .../lua_interpreter/lua_interpreter_service.h | 26 +- esp3d/src/modules/mDNS/mDNS.h | 55 +- esp3d/src/modules/network/netconfig.cpp | 23 +- esp3d/src/modules/network/netservices.cpp | 5 +- esp3d/src/modules/network/netservices.h | 27 +- .../notifications/notifications_service.cpp | 60 +- .../notifications/notifications_service.h | 2 +- esp3d/src/modules/recovery/recovery_service.h | 29 +- esp3d/src/modules/sensor/analogsensor.h | 32 +- esp3d/src/modules/sensor/bmx280.cpp | 86 +- esp3d/src/modules/sensor/bmx280.h | 32 +- esp3d/src/modules/sensor/dht.h | 32 +- esp3d/src/modules/sensor/sensor.h | 109 +- esp3d/src/modules/serial/serial_service.cpp | 6 +- .../modules/serial2socket/serial2socket.cpp | 9 +- .../src/modules/serial2socket/serial2socket.h | 5 +- esp3d/src/modules/time/time_service.cpp | 3 +- esp3d/src/modules/time/time_service.h | 1 - esp3d/src/modules/update/esp_config_file.h | 41 +- esp3d/src/modules/update/update_service.cpp | 3 +- esp3d/src/modules/update/update_service.h | 23 +- .../modules/webdav/handlers/handler_copy.cpp | 8 +- .../modules/webdav/handlers/handler_put.cpp | 16 +- .../modules/websocket/websocket_server.cpp | 4 +- esp3d/src/modules/wifi/wificonfig.cpp | 6 +- tools/format_sources.py | 32 + 83 files changed, 4358 insertions(+), 4029 deletions(-) create mode 100644 embedded/.prettierrc create mode 100644 embedded/tools/format_sources.py create mode 100644 tools/format_sources.py diff --git a/embedded/.prettierrc b/embedded/.prettierrc new file mode 100644 index 00000000..e00e0f86 --- /dev/null +++ b/embedded/.prettierrc @@ -0,0 +1,28 @@ +{ + "printWidth": 80, + "tabWidth": 4, + "useTabs": false, + "semi": true, + "singleQuote": true, + "jsxSingleQuote": false, + "trailingComma": "es5", + "bracketSpacing": true, + "bracketSameLine": false, + "arrowParens": "always", + "requirePragma": false, + "insertPragma": false, + "proseWrap": "preserve", + "overrides": [ + { + "files": "*.js", + "options": { + "parser": "babel" + } + }, + {"files": "*.scss", + "options": { + "parser": "css" + } + } + ] +} diff --git a/embedded/config/buildassets.js b/embedded/config/buildassets.js index ba223a95..0cd58626 100644 --- a/embedded/config/buildassets.js +++ b/embedded/config/buildassets.js @@ -1,95 +1,94 @@ -let path = require("path"); -const fs = require("fs"); -const { createReadStream, createWriteStream } = require("fs"); -const { createGzip } = require("zlib"); -const chalk = require("chalk"); +let path = require('path'); +const fs = require('fs'); +const { createReadStream, createWriteStream } = require('fs'); +const { createGzip } = require('zlib'); +const chalk = require('chalk'); -let distPath = path.normalize(__dirname + "/../dist/"); -let srcPath = path.normalize(__dirname + "/../assets/"); +let distPath = path.normalize(__dirname + '/../dist/'); +let srcPath = path.normalize(__dirname + '/../assets/'); let headerPath = path.normalize( - __dirname + "/../../esp3d/src/modules/http/favicon.h" + __dirname + '/../../esp3d/src/modules/http/favicon.h' ); - - const convertToC = (filepath) => { -console.log(chalk.yellow("Converting bin to text file")); -//Cleaning files -if (fs.existsSync(distPath + "out.tmp")) fs.rmSync(distPath + "out.tmp"); -if (fs.existsSync(distPath + "favicon.h")) fs.rmSync(distPath + "favicon.h"); + console.log(chalk.yellow('Converting bin to text file')); + //Cleaning files + if (fs.existsSync(distPath + 'out.tmp')) fs.rmSync(distPath + 'out.tmp'); + if (fs.existsSync(distPath + 'favicon.h')) + fs.rmSync(distPath + 'favicon.h'); -const data = new Uint8Array( - fs.readFileSync(filepath, { flag: "r" }) -); -console.log("data size is ", data.length); -let out = "#define favicon_size " + data.length + "\n"; -out += "const unsigned char favicon[" + data.length + "] PROGMEM = {\n "; -let nb = 0; -data.forEach((byte, index) => { - out += " 0x" + (byte.toString(16).length == 1 ? "0" : "") + byte.toString(16); - if (index < data.length - 1) out += ","; - if (nb == 15) { - out += "\n "; - nb = 0; - } else { - nb++; - } -}); + const data = new Uint8Array(fs.readFileSync(filepath, { flag: 'r' })); + console.log('data size is ', data.length); + let out = '#define favicon_size ' + data.length + '\n'; + out += 'const unsigned char favicon[' + data.length + '] PROGMEM = {\n '; + let nb = 0; + data.forEach((byte, index) => { + out += + ' 0x' + + (byte.toString(16).length == 1 ? '0' : '') + + byte.toString(16); + if (index < data.length - 1) out += ','; + if (nb == 15) { + out += '\n '; + nb = 0; + } else { + nb++; + } + }); -out += "\n};\n"; -fs.writeFileSync(distPath + "out.tmp", out); + out += '\n};\n'; + fs.writeFileSync(distPath + 'out.tmp', out); -//Check conversion -if (fs.existsSync(distPath + "out.tmp")) { - console.log(chalk.green("[ok]")); -} else { - console.log(chalk.red("[error]Conversion failed")); - return; -} + //Check conversion + if (fs.existsSync(distPath + 'out.tmp')) { + console.log(chalk.green('[ok]')); + } else { + console.log(chalk.red('[error]Conversion failed')); + return; + } -//Format header file -console.log(chalk.yellow("Building header")); -fs.writeFileSync( - distPath + "favicon.h", - fs.readFileSync(srcPath + "header.txt") -); -let bin2cfile = fs.readFileSync(distPath + "out.tmp").toString(); -fs.appendFileSync(distPath + "favicon.h", bin2cfile); -fs.appendFileSync( - distPath + "favicon.h", - fs.readFileSync(srcPath + "footer.txt") -); - -//Check format result -if (fs.existsSync(distPath + "favicon.h")) { - console.log(chalk.green("[ok]")); -} else { - console.log(chalk.red("[error]Conversion failed")); - return; -} - -//Move file to src -console.log(chalk.yellow("Overwriting header in sources")); -fs.writeFileSync(headerPath, fs.readFileSync(distPath + "favicon.h")); -if (fs.existsSync(headerPath)) { - console.log(chalk.green("[ok]")); -} else { - console.log(chalk.red("[error]Overwriting failed")); - return; -} - -} - - -// Create a gzip function for reusable purpose -const compressFile = (filePath, targetPath) => { - const stream = createReadStream(filePath); - stream - .pipe(createGzip(targetPath)) - .pipe(createWriteStream(targetPath)) - .on("finish", () =>{console.log(`Successfully compressed at ${targetPath}`); - convertToC (targetPath)} + //Format header file + console.log(chalk.yellow('Building header')); + fs.writeFileSync( + distPath + 'favicon.h', + fs.readFileSync(srcPath + 'header.txt') ); + let bin2cfile = fs.readFileSync(distPath + 'out.tmp').toString(); + fs.appendFileSync(distPath + 'favicon.h', bin2cfile); + fs.appendFileSync( + distPath + 'favicon.h', + fs.readFileSync(srcPath + 'footer.txt') + ); + + //Check format result + if (fs.existsSync(distPath + 'favicon.h')) { + console.log(chalk.green('[ok]')); + } else { + console.log(chalk.red('[error]Conversion failed')); + return; + } + + //Move file to src + console.log(chalk.yellow('Overwriting header in sources')); + fs.writeFileSync(headerPath, fs.readFileSync(distPath + 'favicon.h')); + if (fs.existsSync(headerPath)) { + console.log(chalk.green('[ok]')); + } else { + console.log(chalk.red('[error]Overwriting failed')); + return; + } }; -compressFile(srcPath + "favicon.ico", distPath + "favicon.ico.gz"); \ No newline at end of file +// Create a gzip function for reusable purpose +const compressFile = (filePath, targetPath) => { + const stream = createReadStream(filePath); + stream + .pipe(createGzip(targetPath)) + .pipe(createWriteStream(targetPath)) + .on('finish', () => { + console.log(`Successfully compressed at ${targetPath}`); + convertToC(targetPath); + }); +}; + +compressFile(srcPath + 'favicon.ico', distPath + 'favicon.ico.gz'); diff --git a/embedded/config/buildheader.js b/embedded/config/buildheader.js index 5ad4f8fd..d52152fb 100644 --- a/embedded/config/buildheader.js +++ b/embedded/config/buildheader.js @@ -1,75 +1,76 @@ -let path = require("path"); -const fs = require("fs"); -const child_process = require("child_process"); -const chalk = require("chalk"); +let path = require('path'); +const fs = require('fs'); +const child_process = require('child_process'); +const chalk = require('chalk'); -let distPath = path.normalize(__dirname + "/../dist/"); -let srcPath = path.normalize(__dirname + "/../src/"); +let distPath = path.normalize(__dirname + '/../dist/'); +let srcPath = path.normalize(__dirname + '/../src/'); let headerPath = path.normalize( - __dirname + "/../../esp3d/src/modules/http/embedded.h" + __dirname + '/../../esp3d/src/modules/http/embedded.h' ); -console.log(chalk.yellow("Converting bin to text file")); +console.log(chalk.yellow('Converting bin to text file')); //Cleaning files -if (fs.existsSync(distPath + "out.tmp")) fs.rmSync(distPath + "out.tmp"); -if (fs.existsSync(distPath + "embedded.h")) fs.rmSync(distPath + "embedded.h"); +if (fs.existsSync(distPath + 'out.tmp')) fs.rmSync(distPath + 'out.tmp'); +if (fs.existsSync(distPath + 'embedded.h')) fs.rmSync(distPath + 'embedded.h'); const data = new Uint8Array( - fs.readFileSync(distPath + "index.html.gz", { flag: "r" }) + fs.readFileSync(distPath + 'index.html.gz', { flag: 'r' }) ); -console.log("data size is ", data.length); -let out = "#define tool_html_gz_size " + data.length + "\n"; -out += "const unsigned char tool_html_gz[" + data.length + "] PROGMEM = {\n "; +console.log('data size is ', data.length); +let out = '#define tool_html_gz_size ' + data.length + '\n'; +out += 'const unsigned char tool_html_gz[' + data.length + '] PROGMEM = {\n '; let nb = 0; data.forEach((byte, index) => { - out += " 0x" + (byte.toString(16).length == 1 ? "0" : "") + byte.toString(16); - if (index < data.length - 1) out += ","; - if (nb == 15) { - out += "\n "; - nb = 0; - } else { - nb++; - } + out += + ' 0x' + (byte.toString(16).length == 1 ? '0' : '') + byte.toString(16); + if (index < data.length - 1) out += ','; + if (nb == 15) { + out += '\n '; + nb = 0; + } else { + nb++; + } }); -out += "\n};\n"; -fs.writeFileSync(distPath + "out.tmp", out); +out += '\n};\n'; +fs.writeFileSync(distPath + 'out.tmp', out); //Check conversion -if (fs.existsSync(distPath + "out.tmp")) { - console.log(chalk.green("[ok]")); +if (fs.existsSync(distPath + 'out.tmp')) { + console.log(chalk.green('[ok]')); } else { - console.log(chalk.red("[error]Conversion failed")); - return; + console.log(chalk.red('[error]Conversion failed')); + return; } //Format header file -console.log(chalk.yellow("Building header")); +console.log(chalk.yellow('Building header')); fs.writeFileSync( - distPath + "embedded.h", - fs.readFileSync(srcPath + "header.txt") + distPath + 'embedded.h', + fs.readFileSync(srcPath + 'header.txt') ); -let bin2cfile = fs.readFileSync(distPath + "out.tmp").toString(); -fs.appendFileSync(distPath + "embedded.h", bin2cfile); +let bin2cfile = fs.readFileSync(distPath + 'out.tmp').toString(); +fs.appendFileSync(distPath + 'embedded.h', bin2cfile); fs.appendFileSync( - distPath + "embedded.h", - fs.readFileSync(srcPath + "footer.txt") + distPath + 'embedded.h', + fs.readFileSync(srcPath + 'footer.txt') ); //Check format result -if (fs.existsSync(distPath + "embedded.h")) { - console.log(chalk.green("[ok]")); +if (fs.existsSync(distPath + 'embedded.h')) { + console.log(chalk.green('[ok]')); } else { - console.log(chalk.red("[error]Conversion failed")); - return; + console.log(chalk.red('[error]Conversion failed')); + return; } //Move file to src -console.log(chalk.yellow("Overwriting header in sources")); -fs.writeFileSync(headerPath, fs.readFileSync(distPath + "embedded.h")); +console.log(chalk.yellow('Overwriting header in sources')); +fs.writeFileSync(headerPath, fs.readFileSync(distPath + 'embedded.h')); if (fs.existsSync(headerPath)) { - console.log(chalk.green("[ok]")); + console.log(chalk.green('[ok]')); } else { - console.log(chalk.red("[error]Overwriting failed")); - return; + console.log(chalk.red('[error]Overwriting failed')); + return; } diff --git a/embedded/config/pack_favicon.js b/embedded/config/pack_favicon.js index ac56755e..eb5879f5 100644 --- a/embedded/config/pack_favicon.js +++ b/embedded/config/pack_favicon.js @@ -1,15 +1,16 @@ -const path = require("path"); -const { createReadStream, createWriteStream } = require("fs"); -const { createGzip } = require("zlib"); -const faviconPath = path.normalize(__dirname + "/../assets/favicon.ico"); - +const path = require('path'); +const { createReadStream, createWriteStream } = require('fs'); +const { createGzip } = require('zlib'); +const faviconPath = path.normalize(__dirname + '/../assets/favicon.ico'); + // Create a gzip function for reusable purpose const compressFile = (filePath) => { - const stream = createReadStream(filePath); - stream - .pipe(createGzip()) - .pipe(createWriteStream(`${filePath}.gz`)) - .on("finish", () =>console.log(`Successfully compressed the file at ${filePath}`) - ); + const stream = createReadStream(filePath); + stream + .pipe(createGzip()) + .pipe(createWriteStream(`${filePath}.gz`)) + .on('finish', () => + console.log(`Successfully compressed the file at ${filePath}`) + ); }; -compressFile(faviconPath); \ No newline at end of file +compressFile(faviconPath); diff --git a/embedded/config/server.js b/embedded/config/server.js index 0aa219b4..d6d3278f 100644 --- a/embedded/config/server.js +++ b/embedded/config/server.js @@ -1,7 +1,7 @@ -const express = require("express"); -const chalk = require("chalk"); -let path = require("path"); -const fs = require("fs"); +const express = require('express'); +const chalk = require('chalk'); +let path = require('path'); +const fs = require('fs'); const port = 8080; /* * Web Server for development @@ -10,666 +10,677 @@ const port = 8080; const wscolor = chalk.cyan; const expresscolor = chalk.green; const commandcolor = chalk.white; -const WebSocket = require("ws"); +const WebSocket = require('ws'); let currentID = 0; const app = express(); -const fileUpload = require("express-fileupload"); -let serverpath = path.normalize(__dirname + "/../server/public/"); -let sdpath = path.normalize(__dirname + "/../server/sd/"); +const fileUpload = require('express-fileupload'); +let serverpath = path.normalize(__dirname + '/../server/public/'); +let sdpath = path.normalize(__dirname + '/../server/sd/'); -let WebSocketServer = require("ws").Server, - wss = new WebSocketServer({ port: 81,handleProtocols:function(protocol) {console.log( "protocol received from client " + protocol ); - return "webui-v3"; - return null;}}); +let WebSocketServer = require('ws').Server, + wss = new WebSocketServer({ + port: 81, + handleProtocols: function (protocol) { + console.log('protocol received from client ' + protocol); + return 'webui-v3'; + return null; + }, + }); app.use(fileUpload({ preserveExtension: true, debug: false })); app.listen(port, () => - console.log(expresscolor(`[express] Listening on port ${port}!`)) + console.log(expresscolor(`[express] Listening on port ${port}!`)) ); //app.use(express.urlencoded({ extended: false })); function SendBinary(text) { - const array = new Uint8Array(text.length); - for (let i = 0; i < array.length; ++i) { - array[i] = text.charCodeAt(i); - } - wss.clients.forEach(function each(client) { - if (client.readyState === WebSocket.OPEN) { - client.send(array); + const array = new Uint8Array(text.length); + for (let i = 0; i < array.length; ++i) { + array[i] = text.charCodeAt(i); } - }); + wss.clients.forEach(function each(client) { + if (client.readyState === WebSocket.OPEN) { + client.send(array); + } + }); } -app.post("/login", function (req, res) { - res.send(""); - return; +app.post('/login', function (req, res) { + res.send(''); + return; }); -app.get("/config", function (req, res) { - res.send( - "chip id: 56398\nCPU Freq: 240 Mhz
" + - "CPU Temp: 58.3 C
" + - "free mem: 212.36 KB
" + - "SDK: v3.2.3-14-gd3e562907
" + - "flash size: 4.00 MB
" + - "size for update: 1.87 MB
" + - "FS type: LittleFS
" + - "FS usage: 104.00 KB/192.00 KB
" + - "baud: 115200
" + - "sleep mode: none
" + - "wifi: ON
" + - "hostname: esp3d
" + - "HTTP port: 80
" + - "Telnet port: 23
" + - "WebDav port: 8383
" + - "sta: ON
" + - "mac: 80:7D:3A:C4:4E:DC
" + - "SSID: WIFI_OFFICE_A2G
" + - "signal: 100 %
" + - "phy mode: 11n
" + - "channel: 11
" + - "ip mode: dhcp
" + - "ip: 192.168.1.61
" + - "gw: 192.168.1.1
" + - "msk: 255.255.255.0
" + - "DNS: 192.168.1.1
" + - "ap: OFF
" + - "mac: 80:7D:3A:C4:4E:DD
" + - "serial: ON
" + - "notification: OFF
" + - "Target Fw: repetier
" + - "FW ver: 3.0.0.a91
" + - "FW arch: ESP32 " - ); - return; -}); - -app.get("/command", function (req, res) { - console.log(commandcolor(`[server]/command params: ${req.query.cmd}`)); - let url = req.query.cmd; - if (url.startsWith("[ESP800]json")) { - res.json( - { - cmd: "800", - status: "ok", - data:{ - FWVersion: "3.0.0.a28", - FWTarget: 40, - SDConnection: "none", - Authentication: "Disabled", - WebCommunication: "Synchronous", - WebSocketIP: "localhost", - WebSocketPort: "81", - Hostname: "esp3d", - WiFiMode: "STA", - WebUpdate: "Enabled", - FlashFileSystem: "LittleFs", - HostPath: "/", - Time: "none", - Cam_ID: "4", - Cam_name: "ESP32 Cam", - } - } +app.get('/config', function (req, res) { + res.send( + 'chip id: 56398\nCPU Freq: 240 Mhz
' + + 'CPU Temp: 58.3 C
' + + 'free mem: 212.36 KB
' + + 'SDK: v3.2.3-14-gd3e562907
' + + 'flash size: 4.00 MB
' + + 'size for update: 1.87 MB
' + + 'FS type: LittleFS
' + + 'FS usage: 104.00 KB/192.00 KB
' + + 'baud: 115200
' + + 'sleep mode: none
' + + 'wifi: ON
' + + 'hostname: esp3d
' + + 'HTTP port: 80
' + + 'Telnet port: 23
' + + 'WebDav port: 8383
' + + 'sta: ON
' + + 'mac: 80:7D:3A:C4:4E:DC
' + + 'SSID: WIFI_OFFICE_A2G
' + + 'signal: 100 %
' + + 'phy mode: 11n
' + + 'channel: 11
' + + 'ip mode: dhcp
' + + 'ip: 192.168.1.61
' + + 'gw: 192.168.1.1
' + + 'msk: 255.255.255.0
' + + 'DNS: 192.168.1.1
' + + 'ap: OFF
' + + 'mac: 80:7D:3A:C4:4E:DD
' + + 'serial: ON
' + + 'notification: OFF
' + + 'Target Fw: repetier
' + + 'FW ver: 3.0.0.a91
' + + 'FW arch: ESP32 ' ); return; - } - if (url.indexOf("ESP111") != -1) { - res.send("192.168.1.111"); - return; - } - if (url.indexOf("ESP420") != -1) { - res.json({ - Status: [ - { id: "chip id", value: "38078" }, - { id: "CPU Freq", value: "240 Mhz" }, - { id: "CPU Temp", value: "50.6 C" }, - { id: "free mem", value: "217.50 KB" }, - { id: "SDK", value: "v3.3.1-61-g367c3c09c" }, - { id: "flash size", value: "4.00 MB" }, - { id: "size for update", value: "1.87 MB" }, - { id: "FS type", value: "SPIFFS" }, - { id: "FS usage", value: "39.95 KB/169.38 KB" }, - { id: "baud", value: "115200" }, - { id: "sleep mode", value: "none" }, - { id: "wifi", value: "ON" }, - { id: "hostname", value: "esp3d" }, - { id: "HTTP port", value: "80" }, - { id: "Telnet port", value: "23" }, - { id: "Ftp ports", value: "21, 20, 55600" }, - { id: "sta", value: "ON" }, - { id: "mac", value: "30:AE:A4:21:BE:94" }, - { id: "SSID", value: "WIFI_OFFICE_B2G" }, - { id: "signal", value: "100 %" }, - { id: "phy mode", value: "11n" }, - { id: "channel", value: "2" }, - { id: "ip mode", value: "dhcp" }, - { id: "ip", value: "192.168.1.43" }, - { id: "gw", value: "192.168.1.1" }, - { id: "msk", value: "255.255.255.0" }, - { id: "DNS", value: "192.168.1.1" }, - { id: "ap", value: "OFF" }, - { id: "mac", value: "30:AE:A4:21:BE:95" }, - { id: "serial", value: "ON" }, - { id: "notification", value: "OFF" }, - { id: "FW ver", value: "3.0.0.a28" }, - { id: "FW arch", value: "ESP32" }, - ], - }); - return; - } +}); - if (url.indexOf("ESP410") != -1) { - res.json({ - AP_LIST: [ - { - SSID: "HP-Setup>71-M277 LaserJet", - SIGNAL: "92", - IS_PROTECTED: "0", - }, - { SSID: "WIFI_OFFICE_B2G", SIGNAL: "88", IS_PROTECTED: "1" }, - { SSID: "NETGEAR70", SIGNAL: "66", IS_PROTECTED: "1" }, - { SSID: "ZenFone6'luc", SIGNAL: "48", IS_PROTECTED: "1" }, - { SSID: "Livebox-EF01", SIGNAL: "20", IS_PROTECTED: "1" }, - { SSID: "orange", SIGNAL: "20", IS_PROTECTED: "0" }, - ], - }); - return; - } +app.get('/command', function (req, res) { + console.log(commandcolor(`[server]/command params: ${req.query.cmd}`)); + let url = req.query.cmd; + if (url.startsWith('[ESP800]json')) { + res.json({ + cmd: '800', + status: 'ok', + data: { + FWVersion: '3.0.0.a28', + FWTarget: 40, + SDConnection: 'none', + Authentication: 'Disabled', + WebCommunication: 'Synchronous', + WebSocketIP: 'localhost', + WebSocketPort: '81', + Hostname: 'esp3d', + WiFiMode: 'STA', + WebUpdate: 'Enabled', + FlashFileSystem: 'LittleFs', + HostPath: '/', + Time: 'none', + Cam_ID: '4', + Cam_name: 'ESP32 Cam', + }, + }); + return; + } + if (url.indexOf('ESP111') != -1) { + res.send('192.168.1.111'); + return; + } + if (url.indexOf('ESP420') != -1) { + res.json({ + Status: [ + { id: 'chip id', value: '38078' }, + { id: 'CPU Freq', value: '240 Mhz' }, + { id: 'CPU Temp', value: '50.6 C' }, + { id: 'free mem', value: '217.50 KB' }, + { id: 'SDK', value: 'v3.3.1-61-g367c3c09c' }, + { id: 'flash size', value: '4.00 MB' }, + { id: 'size for update', value: '1.87 MB' }, + { id: 'FS type', value: 'SPIFFS' }, + { id: 'FS usage', value: '39.95 KB/169.38 KB' }, + { id: 'baud', value: '115200' }, + { id: 'sleep mode', value: 'none' }, + { id: 'wifi', value: 'ON' }, + { id: 'hostname', value: 'esp3d' }, + { id: 'HTTP port', value: '80' }, + { id: 'Telnet port', value: '23' }, + { id: 'Ftp ports', value: '21, 20, 55600' }, + { id: 'sta', value: 'ON' }, + { id: 'mac', value: '30:AE:A4:21:BE:94' }, + { id: 'SSID', value: 'WIFI_OFFICE_B2G' }, + { id: 'signal', value: '100 %' }, + { id: 'phy mode', value: '11n' }, + { id: 'channel', value: '2' }, + { id: 'ip mode', value: 'dhcp' }, + { id: 'ip', value: '192.168.1.43' }, + { id: 'gw', value: '192.168.1.1' }, + { id: 'msk', value: '255.255.255.0' }, + { id: 'DNS', value: '192.168.1.1' }, + { id: 'ap', value: 'OFF' }, + { id: 'mac', value: '30:AE:A4:21:BE:95' }, + { id: 'serial', value: 'ON' }, + { id: 'notification', value: 'OFF' }, + { id: 'FW ver', value: '3.0.0.a28' }, + { id: 'FW arch', value: 'ESP32' }, + ], + }); + return; + } - if (url.indexOf("ESP400") != -1) { - res.json({ - Settings: [ - { - F: "network/network", - P: "130", - T: "S", - V: "esp3d", - H: "hostname", - S: "32", - M: "1", - }, - { - F: "network/network", - P: "0", - T: "B", - V: "1", - H: "radio mode", - O: [{ none: "0" }, { sta: "1" }, { ap: "2" }], - }, - { - F: "network/sta", - P: "1", - T: "S", - V: "WIFI_OFFICE_B2G", - S: "32", - H: "SSID", - M: "1", - }, - { - F: "network/sta", - P: "34", - T: "S", - N: "1", - V: "********", - S: "64", - H: "pwd", - M: "8", - }, - { - F: "network/sta", - P: "99", - T: "B", - V: "1", - H: "ip mode", - O: [{ dhcp: "1" }, { static: "0" }], - }, - { - F: "network/sta", - P: "100", - T: "A", - V: "192.168.0.1", - H: "ip", - }, - { - F: "network/sta", - P: "108", - T: "A", - V: "192.168.0.1", - H: "gw", - }, - { - F: "network/sta", - P: "104", - T: "A", - V: "255.255.255.0", - H: "msk", - }, - { - F: "network/ap", - P: "218", - T: "S", - V: "ESP3D", - S: "32", - H: "SSID", - M: "1", - }, - { - F: "network/ap", - P: "251", - T: "S", - N: "1", - V: "********", - S: "64", - H: "pwd", - M: "8", - }, - { - F: "network/ap", - P: "316", - T: "A", - V: "192.168.0.1", - H: "ip", - }, - { - F: "network/ap", - P: "118", - T: "B", - V: "11", - H: "channel", - O: [ - { 1: "1" }, - { 2: "2" }, - { 3: "3" }, - { 4: "4" }, - { 5: "5" }, - { 6: "6" }, - { 7: "7" }, - { 8: "8" }, - { 9: "9" }, - { 10: "10" }, - { 11: "11" }, - { 12: "12" }, - { 13: "13" }, - { 14: "14" }, - ], - }, - { - F: "service/http", - P: "328", - T: "B", - V: "1", - H: "enable", - O: [{ no: "0" }, { yes: "1" }], - }, - { - F: "service/http", - P: "121", - T: "I", - V: "80", - H: "port", - S: "65001", - M: "1", - }, - { - F: "service/telnetp", - P: "329", - T: "B", - V: "1", - H: "enable", - O: [{ no: "0" }, { yes: "1" }], - }, - { - F: "service/telnetp", - P: "125", - T: "I", - V: "23", - H: "port", - S: "65001", - M: "1", - }, - { - F: "service/ftp", - P: "1208", - T: "B", - V: "1", - H: "enable", - O: [{ no: "0" }, { yes: "1" }], - }, - { - F: "service/ftp", - P: "1196", - T: "I", - V: "21", - H: "control port", - S: "65001", - M: "1", - }, - { - F: "service/ftp", - P: "1200", - T: "I", - V: "20", - H: "active port", - S: "65001", - M: "1", - }, - { - F: "service/ftp", - P: "1204", - T: "I", - V: "55600", - H: "passive port", - S: "65001", - M: "1", - }, - { - F: "service/notification", - P: "1191", - T: "B", - V: "1", - H: "auto notif", - O: [{ no: "0" }, { yes: "1" }], - }, - { - F: "service/notification", - P: "116", - T: "B", - V: "0", - H: "notification", - O: [{ none: "0" }, { pushover: "1" }, { email: "2" }, { line: "3" }], - }, - { - F: "service/notification", - P: "332", - T: "S", - V: "********", - S: "63", - H: "t1", - M: "0", - }, - { - F: "service/notification", - P: "583", - T: "S", - V: "********", - S: "63", - H: "t2", - M: "0", - }, - { - F: "service/notification", - P: "1042", - T: "S", - V: " ", - S: "127", - H: "ts", - M: "0", - }, - { - F: "system/system", - P: "648", - T: "B", - V: "40", - H: "targetfw", - O: [ - { repetier: "50" }, - { marlin: "20" }, - { marlinkimbra: "35" }, - { smoothieware: "40" }, - { grbl: "10" }, - { unknown: "0" }, - ], - }, - { - F: "system/system", - P: "112", - T: "I", - V: "115200", - H: "baud", - O: [ - { 9600: "9600" }, - { 19200: "19200" }, - { 38400: "38400" }, - { 57600: "57600" }, - { 74880: "74880" }, - { 115200: "115200" }, - { 230400: "230400" }, - { 250000: "250000" }, - { 500000: "500000" }, - { 921600: "921600" }, - ], - }, - { - F: "system/system", - P: "320", - T: "I", - V: "10000", - H: "bootdelay", - S: "40000", - M: "0", - }, - { - F: "system/system", - P: "129", - T: "F", - V: "255", - H: "outputmsg", - O: [{ M117: "16" }, { serial: "1" }, { telnet: "2" }], - }, - ], - }); - return; - } - SendBinary("ok\n"); - res.send(""); + if (url.indexOf('ESP410') != -1) { + res.json({ + AP_LIST: [ + { + SSID: 'HP-Setup>71-M277 LaserJet', + SIGNAL: '92', + IS_PROTECTED: '0', + }, + { SSID: 'WIFI_OFFICE_B2G', SIGNAL: '88', IS_PROTECTED: '1' }, + { SSID: 'NETGEAR70', SIGNAL: '66', IS_PROTECTED: '1' }, + { SSID: 'ZenFone6'luc', SIGNAL: '48', IS_PROTECTED: '1' }, + { SSID: 'Livebox-EF01', SIGNAL: '20', IS_PROTECTED: '1' }, + { SSID: 'orange', SIGNAL: '20', IS_PROTECTED: '0' }, + ], + }); + return; + } + + if (url.indexOf('ESP400') != -1) { + res.json({ + Settings: [ + { + F: 'network/network', + P: '130', + T: 'S', + V: 'esp3d', + H: 'hostname', + S: '32', + M: '1', + }, + { + F: 'network/network', + P: '0', + T: 'B', + V: '1', + H: 'radio mode', + O: [{ none: '0' }, { sta: '1' }, { ap: '2' }], + }, + { + F: 'network/sta', + P: '1', + T: 'S', + V: 'WIFI_OFFICE_B2G', + S: '32', + H: 'SSID', + M: '1', + }, + { + F: 'network/sta', + P: '34', + T: 'S', + N: '1', + V: '********', + S: '64', + H: 'pwd', + M: '8', + }, + { + F: 'network/sta', + P: '99', + T: 'B', + V: '1', + H: 'ip mode', + O: [{ dhcp: '1' }, { static: '0' }], + }, + { + F: 'network/sta', + P: '100', + T: 'A', + V: '192.168.0.1', + H: 'ip', + }, + { + F: 'network/sta', + P: '108', + T: 'A', + V: '192.168.0.1', + H: 'gw', + }, + { + F: 'network/sta', + P: '104', + T: 'A', + V: '255.255.255.0', + H: 'msk', + }, + { + F: 'network/ap', + P: '218', + T: 'S', + V: 'ESP3D', + S: '32', + H: 'SSID', + M: '1', + }, + { + F: 'network/ap', + P: '251', + T: 'S', + N: '1', + V: '********', + S: '64', + H: 'pwd', + M: '8', + }, + { + F: 'network/ap', + P: '316', + T: 'A', + V: '192.168.0.1', + H: 'ip', + }, + { + F: 'network/ap', + P: '118', + T: 'B', + V: '11', + H: 'channel', + O: [ + { 1: '1' }, + { 2: '2' }, + { 3: '3' }, + { 4: '4' }, + { 5: '5' }, + { 6: '6' }, + { 7: '7' }, + { 8: '8' }, + { 9: '9' }, + { 10: '10' }, + { 11: '11' }, + { 12: '12' }, + { 13: '13' }, + { 14: '14' }, + ], + }, + { + F: 'service/http', + P: '328', + T: 'B', + V: '1', + H: 'enable', + O: [{ no: '0' }, { yes: '1' }], + }, + { + F: 'service/http', + P: '121', + T: 'I', + V: '80', + H: 'port', + S: '65001', + M: '1', + }, + { + F: 'service/telnetp', + P: '329', + T: 'B', + V: '1', + H: 'enable', + O: [{ no: '0' }, { yes: '1' }], + }, + { + F: 'service/telnetp', + P: '125', + T: 'I', + V: '23', + H: 'port', + S: '65001', + M: '1', + }, + { + F: 'service/ftp', + P: '1208', + T: 'B', + V: '1', + H: 'enable', + O: [{ no: '0' }, { yes: '1' }], + }, + { + F: 'service/ftp', + P: '1196', + T: 'I', + V: '21', + H: 'control port', + S: '65001', + M: '1', + }, + { + F: 'service/ftp', + P: '1200', + T: 'I', + V: '20', + H: 'active port', + S: '65001', + M: '1', + }, + { + F: 'service/ftp', + P: '1204', + T: 'I', + V: '55600', + H: 'passive port', + S: '65001', + M: '1', + }, + { + F: 'service/notification', + P: '1191', + T: 'B', + V: '1', + H: 'auto notif', + O: [{ no: '0' }, { yes: '1' }], + }, + { + F: 'service/notification', + P: '116', + T: 'B', + V: '0', + H: 'notification', + O: [ + { none: '0' }, + { pushover: '1' }, + { email: '2' }, + { line: '3' }, + ], + }, + { + F: 'service/notification', + P: '332', + T: 'S', + V: '********', + S: '63', + H: 't1', + M: '0', + }, + { + F: 'service/notification', + P: '583', + T: 'S', + V: '********', + S: '63', + H: 't2', + M: '0', + }, + { + F: 'service/notification', + P: '1042', + T: 'S', + V: ' ', + S: '127', + H: 'ts', + M: '0', + }, + { + F: 'system/system', + P: '648', + T: 'B', + V: '40', + H: 'targetfw', + O: [ + { repetier: '50' }, + { marlin: '20' }, + { marlinkimbra: '35' }, + { smoothieware: '40' }, + { grbl: '10' }, + { unknown: '0' }, + ], + }, + { + F: 'system/system', + P: '112', + T: 'I', + V: '115200', + H: 'baud', + O: [ + { 9600: '9600' }, + { 19200: '19200' }, + { 38400: '38400' }, + { 57600: '57600' }, + { 74880: '74880' }, + { 115200: '115200' }, + { 230400: '230400' }, + { 250000: '250000' }, + { 500000: '500000' }, + { 921600: '921600' }, + ], + }, + { + F: 'system/system', + P: '320', + T: 'I', + V: '10000', + H: 'bootdelay', + S: '40000', + M: '0', + }, + { + F: 'system/system', + P: '129', + T: 'F', + V: '255', + H: 'outputmsg', + O: [{ M117: '16' }, { serial: '1' }, { telnet: '2' }], + }, + ], + }); + return; + } + SendBinary('ok\n'); + res.send(''); }); function fileSizeString(size) { - let s; - if (size < 1024) return size + " B"; - if (size < 1024 * 1024) return (size / 1024).toFixed(2) + " KB"; - if (size < 1024 * 1024 * 1024) - return (size / (1024 * 1024)).toFixed(2) + " MB"; - if (size < 1024 * 1024 * 1024 * 1024) - return (size / (1024 * 1024 * 1024)).toFixed(2) + " GB"; - return "X B"; + let s; + if (size < 1024) return size + ' B'; + if (size < 1024 * 1024) return (size / 1024).toFixed(2) + ' KB'; + if (size < 1024 * 1024 * 1024) + return (size / (1024 * 1024)).toFixed(2) + ' MB'; + if (size < 1024 * 1024 * 1024 * 1024) + return (size / (1024 * 1024 * 1024)).toFixed(2) + ' GB'; + return 'X B'; } -function filesList(mypath,mainpath) { - let res = '{"files":['; - let nb = 0; - let total = sdpath==mainpath? (4096 * 1024 * 1024):(1.2 * 1024 * 1024); - let totalused = getTotalSize(mainpath); - let currentpath = path.normalize(mainpath + mypath); - console.log("[path]" + currentpath); - fs.readdirSync(currentpath).forEach((fileelement) => { - let fullpath = path.normalize(currentpath + "/" + fileelement); - let fst = fs.statSync(fullpath); - let fsize = -1; +function filesList(mypath, mainpath) { + let res = '{"files":['; + let nb = 0; + let total = sdpath == mainpath ? 4096 * 1024 * 1024 : 1.2 * 1024 * 1024; + let totalused = getTotalSize(mainpath); + let currentpath = path.normalize(mainpath + mypath); + console.log('[path]' + currentpath); + fs.readdirSync(currentpath).forEach((fileelement) => { + let fullpath = path.normalize(currentpath + '/' + fileelement); + let fst = fs.statSync(fullpath); + let fsize = -1; - if (fst.isFile()) { - fsize = fileSizeString(fst.size); - } - if (nb > 0) res += ","; - res += '{"name":"' + fileelement + '","size":"' + fsize + '"}'; - nb++; - }); - res += - '],"path":"' + - mypath + - '","occupation":"' + - ((100 * totalused) / total).toFixed(0) + - '","status":"ok","total":"' + - fileSizeString(total) + - '","used":"' + - fileSizeString(totalused) + - '"}'; - return res; + if (fst.isFile()) { + fsize = fileSizeString(fst.size); + } + if (nb > 0) res += ','; + res += '{"name":"' + fileelement + '","size":"' + fsize + '"}'; + nb++; + }); + res += + '],"path":"' + + mypath + + '","occupation":"' + + ((100 * totalused) / total).toFixed(0) + + '","status":"ok","total":"' + + fileSizeString(total) + + '","used":"' + + fileSizeString(totalused) + + '"}'; + return res; } const getAllFiles = function (dirPath, arrayOfFiles) { - let files = fs.readdirSync(dirPath); + let files = fs.readdirSync(dirPath); - arrayOfFiles = arrayOfFiles || []; + arrayOfFiles = arrayOfFiles || []; - files.forEach(function (file) { - if (fs.statSync(dirPath + "/" + file).isDirectory()) { - arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles); - } else { - arrayOfFiles.push(dirPath + "/" + file); - } - }); + files.forEach(function (file) { + if (fs.statSync(dirPath + '/' + file).isDirectory()) { + arrayOfFiles = getAllFiles(dirPath + '/' + file, arrayOfFiles); + } else { + arrayOfFiles.push(dirPath + '/' + file); + } + }); - return arrayOfFiles; + return arrayOfFiles; }; const getTotalSize = function (directoryPath) { - const arrayOfFiles = getAllFiles(directoryPath); + const arrayOfFiles = getAllFiles(directoryPath); - let totalSize = 0; + let totalSize = 0; - arrayOfFiles.forEach(function (filePath) { - totalSize += fs.statSync(filePath).size; - }); + arrayOfFiles.forEach(function (filePath) { + totalSize += fs.statSync(filePath).size; + }); - return totalSize; + return totalSize; }; function deleteFolderRecursive(path) { - if (fs.existsSync(path) && fs.lstatSync(path).isDirectory()) { - fs.readdirSync(path).forEach(function (file, index) { - let curPath = path + "/" + file; + if (fs.existsSync(path) && fs.lstatSync(path).isDirectory()) { + fs.readdirSync(path).forEach(function (file, index) { + let curPath = path + '/' + file; - if (fs.lstatSync(curPath).isDirectory()) { - // recurse - deleteFolderRecursive(curPath); - } else { - // delete file - fs.unlinkSync(curPath); - } - }); + if (fs.lstatSync(curPath).isDirectory()) { + // recurse + deleteFolderRecursive(curPath); + } else { + // delete file + fs.unlinkSync(curPath); + } + }); - console.log(`[server]Deleting directory "${path}"...`); - if (fs.existsSync(path)) fs.rmdirSync(path); - } else console.log(`[server]No directory "${path}"...`); + console.log(`[server]Deleting directory "${path}"...`); + if (fs.existsSync(path)) fs.rmdirSync(path); + } else console.log(`[server]No directory "${path}"...`); } -app.all("/updatefw", function (req, res) { - res.send("ok"); +app.all('/updatefw', function (req, res) { + res.send('ok'); }); -app.all("/sdfiles", function (req, res) { - let mypath = req.query.path; - let url = req.originalUrl; - let filepath = path.normalize(sdpath + mypath + "/" + req.query.filename); - if (url.indexOf("action=deletedir") != -1) { - console.log("[server]delete directory " + filepath); - deleteFolderRecursive(filepath); - fs.readdirSync(mypath); - } else if (url.indexOf("action=delete") != -1) { - fs.unlinkSync(filepath); - console.log("[server]delete file " + filepath); - } - if (url.indexOf("action=createdir") != -1) { - fs.mkdirSync(filepath); - console.log("[server]new directory " + filepath); - } - if (typeof mypath == "undefined") { - if (typeof req.body.path == "undefined") { - console.log("[server]path is not defined"); - mypath = "/"; +app.all('/sdfiles', function (req, res) { + let mypath = req.query.path; + let url = req.originalUrl; + let filepath = path.normalize(sdpath + mypath + '/' + req.query.filename); + if (url.indexOf('action=deletedir') != -1) { + console.log('[server]delete directory ' + filepath); + deleteFolderRecursive(filepath); + fs.readdirSync(mypath); + } else if (url.indexOf('action=delete') != -1) { + fs.unlinkSync(filepath); + console.log('[server]delete file ' + filepath); + } + if (url.indexOf('action=createdir') != -1) { + fs.mkdirSync(filepath); + console.log('[server]new directory ' + filepath); + } + if (typeof mypath == 'undefined') { + if (typeof req.body.path == 'undefined') { + console.log('[server]path is not defined'); + mypath = '/'; + } else { + mypath = (req.body.path == '/' ? '' : req.body.path) + '/'; + } + } + console.log('[server]path is ' + mypath); + if (!req.files || Object.keys(req.files).length === 0) { + return res.send(filesList(mypath, sdpath)); + } + let myFile = req.files.myfiles; + if (typeof myFile.length == 'undefined') { + let fullpath = path.normalize(sdpath + mypath + myFile.name); + console.log('[server]one file:' + fullpath); + myFile.mv(fullpath, function (err) { + if (err) return res.status(500).send(err); + res.send(filesList(mypath, sdpath)); + }); + return; } else { - mypath = (req.body.path == "/" ? "" : req.body.path) + "/"; + console.log(myFile.length + ' files'); + for (let i = 0; i < myFile.length; i++) { + let fullpath = path.normalize(sdpath + mypath + myFile[i].name); + console.log(fullpath); + myFile[i].mv(fullpath).then(() => { + if (i == myFile.length - 1) res.send(filesList(mypath, sdpath)); + }); + } } - } - console.log("[server]path is " + mypath); - if (!req.files || Object.keys(req.files).length === 0) { - return res.send(filesList(mypath,sdpath)); - } - let myFile = req.files.myfiles; - if (typeof myFile.length == "undefined") { - let fullpath = path.normalize(sdpath + mypath + myFile.name); - console.log("[server]one file:" + fullpath); - myFile.mv(fullpath, function (err) { - if (err) return res.status(500).send(err); - res.send(filesList(mypath,sdpath)); - }); - return; - } else { - console.log(myFile.length + " files"); - for (let i = 0; i < myFile.length; i++) { - let fullpath = path.normalize(sdpath + mypath + myFile[i].name); - console.log(fullpath); - myFile[i].mv(fullpath).then(() => { - if (i == myFile.length - 1) res.send(filesList(mypath,sdpath)); - }); - } - } }); -app.all("/files", function (req, res) { - let mypath = req.query.path; - let url = req.originalUrl; - let filepath = path.normalize(serverpath + mypath + "/" + req.query.filename); - if (url.indexOf("action=deletedir") != -1) { - console.log("[server]delete directory " + filepath); - deleteFolderRecursive(filepath); - fs.readdirSync(mypath); - } else if (url.indexOf("action=delete") != -1) { - fs.unlinkSync(filepath); - console.log("[server]delete file " + filepath); - } - if (url.indexOf("action=createdir") != -1) { - fs.mkdirSync(filepath); - console.log("[server]new directory " + filepath); - } - if (typeof mypath == "undefined") { - if (typeof req.body.path == "undefined") { - console.log("[server]path is not defined"); - mypath = "/"; +app.all('/files', function (req, res) { + let mypath = req.query.path; + let url = req.originalUrl; + let filepath = path.normalize( + serverpath + mypath + '/' + req.query.filename + ); + if (url.indexOf('action=deletedir') != -1) { + console.log('[server]delete directory ' + filepath); + deleteFolderRecursive(filepath); + fs.readdirSync(mypath); + } else if (url.indexOf('action=delete') != -1) { + fs.unlinkSync(filepath); + console.log('[server]delete file ' + filepath); + } + if (url.indexOf('action=createdir') != -1) { + fs.mkdirSync(filepath); + console.log('[server]new directory ' + filepath); + } + if (typeof mypath == 'undefined') { + if (typeof req.body.path == 'undefined') { + console.log('[server]path is not defined'); + mypath = '/'; + } else { + mypath = (req.body.path == '/' ? '' : req.body.path) + '/'; + } + } + console.log('[server]path is ' + mypath); + if (!req.files || Object.keys(req.files).length === 0) { + return res.send(filesList(mypath, serverpath)); + } + let myFile = req.files.myfiles; + if (typeof myFile.length == 'undefined') { + let fullpath = path.normalize(serverpath + mypath + myFile.name); + console.log('[server]one file:' + fullpath); + myFile.mv(fullpath, function (err) { + if (err) return res.status(500).send(err); + res.send(filesList(mypath, serverpath)); + }); + return; } else { - mypath = (req.body.path == "/" ? "" : req.body.path) + "/"; + console.log(myFile.length + ' files'); + for (let i = 0; i < myFile.length; i++) { + let fullpath = path.normalize(serverpath + mypath + myFile[i].name); + console.log(fullpath); + myFile[i].mv(fullpath).then(() => { + if (i == myFile.length - 1) + res.send(filesList(mypath, serverpath)); + }); + } } - } - console.log("[server]path is " + mypath); - if (!req.files || Object.keys(req.files).length === 0) { - return res.send(filesList(mypath,serverpath)); - } - let myFile = req.files.myfiles; - if (typeof myFile.length == "undefined") { - let fullpath = path.normalize(serverpath + mypath + myFile.name); - console.log("[server]one file:" + fullpath); - myFile.mv(fullpath, function (err) { - if (err) return res.status(500).send(err); - res.send(filesList(mypath, serverpath)); - }); - return; - } else { - console.log(myFile.length + " files"); - for (let i = 0; i < myFile.length; i++) { - let fullpath = path.normalize(serverpath + mypath + myFile[i].name); - console.log(fullpath); - myFile[i].mv(fullpath).then(() => { - if (i == myFile.length - 1) res.send(filesList(mypath, serverpath)); - }); - } - } }); -wss.on("connection", (socket, request) => { - console.log(wscolor("[ws] New connection")); - console.log(wscolor(`[ws] currentID:${currentID}`)); - socket.send(`currentID:${currentID}`); - wss.clients.forEach(function each(client) { - if (client.readyState === WebSocket.OPEN) { - client.send(`activeID:${currentID}`); - } - }); - currentID++; - socket.on("message", (message) => { - console.log(wscolor("[ws] received: %s", message)); - }); +wss.on('connection', (socket, request) => { + console.log(wscolor('[ws] New connection')); + console.log(wscolor(`[ws] currentID:${currentID}`)); + socket.send(`currentID:${currentID}`); + wss.clients.forEach(function each(client) { + if (client.readyState === WebSocket.OPEN) { + client.send(`activeID:${currentID}`); + } + }); + currentID++; + socket.on('message', (message) => { + console.log(wscolor('[ws] received: %s', message)); + }); }); -wss.on("error", (error) => { - console.log(wscolor("[ws] Error: %s", error)); +wss.on('error', (error) => { + console.log(wscolor('[ws] Error: %s', error)); }); diff --git a/embedded/config/webpack.dev.js b/embedded/config/webpack.dev.js index 47b56242..ad53f803 100644 --- a/embedded/config/webpack.dev.js +++ b/embedded/config/webpack.dev.js @@ -1,53 +1,53 @@ -const path = require("path"); -const HtmlWebpackPlugin = require("html-webpack-plugin"); +const path = require('path'); +const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { - mode: "development", // this will trigger some webpack default stuffs for dev - entry: path.resolve(__dirname, "../src/index.js"), // if not set, default path to './src/index.js'. Accepts an object with multiple key-value pairs, with key as your custom bundle filename(substituting the [name]), and value as the corresponding file path - output: { - filename: "[name].bundle.js", // [name] will take whatever the input filename is. defaults to 'main' if only a single entry value - path: path.resolve(__dirname, "../dist"), // the folder containing you final dist/build files. Default to './dist' - }, - devServer: { - historyApiFallback: true, // to make our SPA works after a full reload, so that it serves 'index.html' when 404 response - open: true, - static: { - directory: path.resolve(__dirname, "./dist"), + mode: 'development', // this will trigger some webpack default stuffs for dev + entry: path.resolve(__dirname, '../src/index.js'), // if not set, default path to './src/index.js'. Accepts an object with multiple key-value pairs, with key as your custom bundle filename(substituting the [name]), and value as the corresponding file path + output: { + filename: '[name].bundle.js', // [name] will take whatever the input filename is. defaults to 'main' if only a single entry value + path: path.resolve(__dirname, '../dist'), // the folder containing you final dist/build files. Default to './dist' }, - port: 8088, - proxy: { - context: () => true, - target: "http://localhost:8080", - }, - }, - stats: "minimal", // default behaviour spit out way too much info. adjust to your need. - devtool: "source-map", // a sourcemap type. map to original source with line number - plugins: [ - new HtmlWebpackPlugin({ - template: path.join(__dirname, "../src/index.html"), - inlineSource: ".(js|css)$", - inject: true, - }), - ], // automatically creates a 'index.html' for us with our ,