From 6eada33d4767d8e2fb81b981ff09f9e0a315cf59 Mon Sep 17 00:00:00 2001 From: Sumit Kumar Soni Date: Mon, 3 Aug 2026 01:06:46 +0530 Subject: [PATCH] build: make WebUI npm scripts cross-platform --- README.md | 8 +++++-- webui-src/make-src/build.bat | 45 +++++++++++++++++++----------------- webui-src/make-src/build.js | 35 ++++++++++++++++++++++++++++ webui-src/package.json | 6 ++--- webui.pro | 2 +- 5 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 webui-src/make-src/build.js diff --git a/README.md b/README.md index 4508302..f93a7b6 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,9 @@ npm run build npm run lint ``` +These npm commands use `build.bat` on native Windows and `build.sh` on other +platforms. + Commit SCSS changes together with the regenerated `webui-src/styles.css`. `qmake .` packages that committed CSS but does not compile SCSS. Running `make` alone does not rebuild this `TEMPLATE = subdirs` project; rerun @@ -189,8 +192,9 @@ when testing watched changes through RetroShare. Before submitting, run the full build and lint commands, then point RetroShare's **Web interface directory** at `webui/` and manually test the -affected screens. ESLint, generated JavaScript syntax, and shell syntax are -checked by `npm run lint`; no formatting script is defined. +affected screens. ESLint, generated JavaScript syntax, the build dispatcher, +and shell syntax on non-Windows systems are checked by `npm run lint`; no +formatting script is defined. ### References diff --git a/webui-src/make-src/build.bat b/webui-src/make-src/build.bat index b9851fd..c61fe57 100644 --- a/webui-src/make-src/build.bat +++ b/webui-src/make-src/build.bat @@ -5,55 +5,58 @@ setlocal enabledelayedexpansion echo "### Starting WebUI build ###" -set src=%~dp0..\..\webui-src +set "src=%~dp0..\..\webui-src" rem Output destination if "%~1"=="" ( - set publicdest=%~dp0..\..\webui + set "publicdest=%~dp0..\..\webui" ) else ( - set publicdest=%~1\webui + set "publicdest=%~1\webui" ) -if exist "%publicdest%" echo removing existing %publicdest%&&rd %publicdest% /S /Q +if exist "%publicdest%" ( + echo removing existing %publicdest% + rd "%publicdest%" /S /Q +) echo creating %publicdest% -md %publicdest% +md "%publicdest%" rem Make full path -pushd %publicdest% -set publicdest=%cd% +pushd "%publicdest%" +set "publicdest=%cd%" popd echo copying html file -xcopy /s %src%\index.html %publicdest% +copy /Y "%src%\index.html" "%publicdest%\index.html" >nul echo copying css file -xcopy /s %src%\styles.css %publicdest% +copy /Y "%src%\styles.css" "%publicdest%\styles.css" >nul echo building app.js echo - copying template.js ... -copy %src%\make-src\template.js %publicdest%\app.js +copy /Y "%src%\make-src\template.js" "%publicdest%\app.js" >nul -pushd %src%\app +pushd "%src%\app" set "basefolder=%cd%\" set "lastsection=" for /R %%F in (*.js) do call :addfile-js "%basefolder%" "%%F" popd echo copying assets folder -xcopy /s %src%\assets\ %publicdest% +xcopy "%src%\assets\*" "%publicdest%\" /E /I /Y >nul echo "### WebUI build complete ###" goto :EOF :addfile-js -set basefolder=%~1 -set fname=%~2 +set "basefolder=%~1" +set "fname=%~2" -set registername=%~dpn2 -set registername=!registername:%basefolder%=! -set registername=%registername:\=/% +set "registername=%~dpn2" +set "registername=!registername:%basefolder%=!" +set "registername=%registername:\=/%" for /f "tokens=1,2 delims=/" %%A in ("!registername!") do ( if "%%B"=="" ( @@ -64,9 +67,9 @@ for /f "tokens=1,2 delims=/" %%A in ("!registername!") do ( set "lastsection=%%A" ) ) -echo require.register("%registername%", function(exports, require, module) { >> %publicdest%\app.js -type %fname% >> %publicdest%\app.js -echo. >> %publicdest%\app.js -echo }); >> %publicdest%\app.js +echo require.register("%registername%", function(exports, require, module) { >>"%publicdest%\app.js" +type "%fname%" >>"%publicdest%\app.js" +echo. >>"%publicdest%\app.js" +echo }); >>"%publicdest%\app.js" :EOF diff --git a/webui-src/make-src/build.js b/webui-src/make-src/build.js new file mode 100644 index 0000000..bfb7c60 --- /dev/null +++ b/webui-src/make-src/build.js @@ -0,0 +1,35 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); + +const shellScript = path.join(__dirname, 'build.sh'); +const batchScript = path.join(__dirname, 'build.bat'); + +function getRunner(platform, commandShell = 'cmd.exe') { + return platform === 'win32' + ? { command: commandShell, args: ['/d', '/c', batchScript] } + : { command: 'sh', args: [shellScript] }; +} + +function run(runner) { + const result = spawnSync(runner.command, runner.args, { stdio: 'inherit' }); + if (result.error) { + console.error(result.error.message); + return 1; + } + return result.status ?? 1; +} + +if (process.argv[2] === '--check') { + assert.equal(path.basename(getRunner('win32').args[2]), 'build.bat'); + assert.equal(path.basename(getRunner('linux').args[0]), 'build.sh'); + const status = process.platform === 'win32' + ? 0 + : run({ command: 'sh', args: ['-n', shellScript] }); + if (status === 0) console.log('Build dispatcher checks passed.'); + process.exitCode = status; +} else { + process.exitCode = run(getRunner(process.platform, process.env.ComSpec)); +} diff --git a/webui-src/package.json b/webui-src/package.json index f16f87e..90482de 100644 --- a/webui-src/package.json +++ b/webui-src/package.json @@ -3,9 +3,9 @@ "version": "1.0.0", "description": "Retroshare's Web Interface", "scripts": { - "watch": "rm ./styles.css && sass --watch --embed-sources --embed-source-map ./app/scss/main.scss ./styles.css", - "build": "sass --no-source-map --style=compressed ./app/scss/main.scss ./styles.css && sh ./make-src/build.sh", - "lint": "eslint app && node --check ../webui/app.js && sh -n make-src/build.sh && echo 'Lint checks passed.'" + "watch": "sass --watch --embed-sources --embed-source-map ./app/scss/main.scss ./styles.css", + "build": "sass --no-source-map --style=compressed ./app/scss/main.scss ./styles.css && node ./make-src/build.js", + "lint": "eslint app && node --check ../webui/app.js && node ./make-src/build.js --check && echo Lint checks passed." }, "license": "ISC", "engines": { diff --git a/webui.pro b/webui.pro index accf8a0..64e5f79 100644 --- a/webui.pro +++ b/webui.pro @@ -33,7 +33,7 @@ WEBUI_SRC_CSS = $$PWD/webui-src/styles.css WEBUI_SRC_IMAGE = $$PWD/data/retroshare.svg -win32-g++ { +win32 { isEmpty(QMAKE_SH) { # Windows native build WEBUI_SRC_SCRIPT = $$PWD/webui-src/make-src/build.bat