Skip to content

Commit

Permalink
Merge pull request #112 from jannis-baum/issue/111-version-command
Browse files Browse the repository at this point in the history
  • Loading branch information
jannis-baum authored Jul 19, 2024
2 parents 9dc79ac + 05d074c commit 88fa0d5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)

# ------------------------------------------------------------------------------
Expand Down
55 changes: 37 additions & 18 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand All @@ -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);
});
17 changes: 16 additions & 1 deletion viv
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -19,4 +25,9 @@ export default {
bufferutil: 'esm bufferutil',
},
target: 'node',
plugins: [
new webpack.DefinePlugin({
'process.env.VERSION': JSON.stringify(version),
}),
],
};

0 comments on commit 88fa0d5

Please sign in to comment.