From 1332d77a80525e10001e4a87a48a5fdea19dac73 Mon Sep 17 00:00:00 2001 From: Jannis Baum Date: Fri, 19 Jul 2024 12:53:05 +0200 Subject: [PATCH 1/5] feat(#111): print output of vivify-server --- src/app.ts | 17 ++++++++++------- viv | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/app.ts b/src/app.ts index 8424e96e..dc8c9279 100644 --- a/src/app.ts +++ b/src/app.ts @@ -30,7 +30,7 @@ export const { clientsAt, messageClientsAt } = setupSockets( () => { if (config.timeout > 0) shutdownTimer = setInterval(() => { - console.log(`No clients for ${config.timeout}ms, shutting down.`); + // timeout when no clients are connected process.exit(0); }, config.timeout); }, @@ -40,7 +40,7 @@ export const { clientsAt, messageClientsAt } = setupSockets( ); const address = `http://localhost:${config.port}`; -const openArgs = async () => { +const handleArgs = async () => { await Promise.all( process.argv.slice(2).map(async (path) => { if (path.startsWith('-')) return; @@ -53,16 +53,19 @@ const openArgs = async () => { await open(url); }), ); + if (process.env['NODE_ENV'] !== 'development') { + // - viv executable waits for this string and then stops printing + // vivify-server's output and terminates + // - the string itself is not shown to the user + console.log('STARTUP COMPLETE'); + } }; get(`${address}/health`, async () => { // server is already running - await openArgs(); + await handleArgs(); process.exit(0); }).on('error', () => { // server is not running so we start it - server.listen(config.port, async () => { - console.log(`App is listening on port ${config.port}!`); - openArgs(); - }); + server.listen(config.port, handleArgs); }); diff --git a/viv b/viv index 64e0d195..30f6514c 100755 --- a/viv +++ b/viv @@ -10,4 +10,19 @@ if [ "$#" -lt 1 -o "$1" = "-h" -o "$1" = "--help" ]; then exit 1 fi -nohup vivify-server $@ 1>/dev/null 2>/dev/null & +output=`mktemp` +cleanup() { + rm -f "$output" +} +trap cleanup EXIT + +nohup vivify-server $@ > "$output" 2> /dev/null & + +# print stdout of vivify-server until STARTUP COMPLETE is found +tail -f "$output" | while read line; do + if echo "$line" | grep -q "STARTUP COMPLETE"; then + pkill -P $$ tail + break + fi + echo "$line" +done From 4e89db7c09c3261d1be35073de62731861d7ddf9 Mon Sep 17 00:00:00 2001 From: Jannis Baum Date: Fri, 19 Jul 2024 13:10:08 +0200 Subject: [PATCH 2/5] feat(#111): inject version number from built-time env --- webpack.config.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/webpack.config.js b/webpack.config.js index 443a1697..a76de4c8 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,9 +1,15 @@ import path from 'path'; import { fileURLToPath } from 'url'; +import webpack from 'webpack'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const buildDir = path.resolve(__dirname, 'build'); +if (!('VIV_VERSION' in process.env)) { + throw new Error('VIV_VERSION environment variable has to be set to build'); +} +const version = process.env.VIV_VERSION; + export default { mode: 'production', entry: { @@ -19,4 +25,9 @@ export default { bufferutil: 'esm bufferutil', }, target: 'node', + plugins: [ + new webpack.DefinePlugin({ + 'process.env.VERSION': JSON.stringify(version), + }), + ], }; From 8ef57c4400ec871d3b5ffdc19d505ca48fbb1d0f Mon Sep 17 00:00:00 2001 From: Jannis Baum Date: Fri, 19 Jul 2024 13:26:28 +0200 Subject: [PATCH 3/5] feat(#111): parse args & print version --- src/app.ts | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/app.ts b/src/app.ts index dc8c9279..4376f3f4 100644 --- a/src/app.ts +++ b/src/app.ts @@ -41,23 +41,39 @@ export const { clientsAt, messageClientsAt } = setupSockets( const address = `http://localhost:${config.port}`; const handleArgs = async () => { - await Promise.all( - process.argv.slice(2).map(async (path) => { - if (path.startsWith('-')) return; - if (!existsSync(path)) { - console.log(`File not found: ${path}`); - return; + try { + const args = process.argv.slice(2); + const options = args.filter((arg) => arg.startsWith('-')); + for (const option of options) { + switch (option) { + case '-v': + case '--version': + console.log(`vivify-server ${process.env.VERSION ?? 'dev'}`); + break; + default: + console.log(`unknown option "${option}"`); } - const target = preferredPath(presolve(path)); - const url = `${address}${pathToURL(target)}`; - await open(url); - }), - ); - if (process.env['NODE_ENV'] !== 'development') { - // - viv executable waits for this string and then stops printing - // vivify-server's output and terminates - // - the string itself is not shown to the user - console.log('STARTUP COMPLETE'); + } + + const paths = args.filter((arg) => !arg.startsWith('-')); + await Promise.all( + paths.map(async (path) => { + if (!existsSync(path)) { + console.log(`File not found: ${path}`); + return; + } + const target = preferredPath(presolve(path)); + const url = `${address}${pathToURL(target)}`; + await open(url); + }), + ); + } finally { + if (process.env['NODE_ENV'] !== 'development') { + // - viv executable waits for this string and then stops printing + // vivify-server's output and terminates + // - the string itself is not shown to the user + console.log('STARTUP COMPLETE'); + } } }; From 1fd31f3a80f0ee9bfba5364ec24ad8316ecb5313 Mon Sep 17 00:00:00 2001 From: Jannis Baum Date: Fri, 19 Jul 2024 13:29:04 +0200 Subject: [PATCH 4/5] ci(#111): inject version number --- .github/workflows/ci.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b55a26c5..77f033c5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -36,7 +36,7 @@ jobs: - name: build run: | yarn - make linux + VIV_VERSION="${{ github.ref_name }}" make linux - name: upload artifact uses: actions/upload-artifact@v4 with: @@ -53,7 +53,7 @@ jobs: - name: build run: | yarn - make macos + VIV_VERSION="${{ github.ref_name }}" make macos - name: upload artifact uses: actions/upload-artifact@v4 with: From 05d074cb2c8e4d0ae670145d6cc821259bd9aa36 Mon Sep 17 00:00:00 2001 From: Jannis Baum Date: Fri, 19 Jul 2024 13:40:53 +0200 Subject: [PATCH 5/5] feat(#111): version numbers for local builds --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index bb7e22ee..8049edee 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,8 @@ EXE_PATH_MACOS=$(BUILD_DIR_MACOS)/$(EXE_NAME) SERVER_PATH_LINUX=$(BUILD_DIR_LINUX)/$(SERVER_NAME) EXE_PATH_LINUX=$(BUILD_DIR_LINUX)/$(EXE_NAME) +VIV_VERSION ?= $(shell git describe --tags --always --dirty) + .PHONY: instruct-build instruct-build: @ echo 'Please run `make macos` or `make linux` to build the project' @@ -32,7 +34,7 @@ $(DIST_PATH): $(shell find src -type f) tsconfig.json package.json yarn.lock touch $(DIST_PATH) $(BUNDLE_PATH): webpack.config.js $(STATIC_PATH) $(DIST_PATH) - node_modules/.bin/webpack + VIV_VERSION=$(VIV_VERSION) node_modules/.bin/webpack touch $(BUNDLE_PATH) # ------------------------------------------------------------------------------