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: 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) # ------------------------------------------------------------------------------ diff --git a/src/app.ts b/src/app.ts index 8424e96e..4376f3f4 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,29 +40,48 @@ export const { clientsAt, messageClientsAt } = setupSockets( ); const address = `http://localhost:${config.port}`; -const openArgs = 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; +const handleArgs = async () => { + 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); - }), - ); + } + + 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'); + } + } }; 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 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), + }), + ], };