Skip to content

Commit

Permalink
feat(#159): always output version & complete startup
Browse files Browse the repository at this point in the history
  • Loading branch information
jannis-baum committed Aug 6, 2024
1 parent 20156a2 commit e27529f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
27 changes: 18 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { router as viewerRouter } from './routes/viewer.js';
import { router as openRouter } from './routes/_open.js';
import { setupSockets } from './sockets.js';
import { urlToPath } from './utils/path.js';
import { handleArgs } from './cli.js';
import { completeStartup, handleArgs } from './cli.js';

const app = express();
app.use(express.json());
Expand Down Expand Up @@ -39,11 +39,20 @@ export const { clientsAt, messageClients, openAndMessage } = setupSockets(
},
);

get(`${address}/health`, async () => {
// server is already running
await handleArgs();
process.exit(0);
}).on('error', () => {
// server is not running so we start it
server.listen(config.port, handleArgs);
});
const openTargets = handleArgs();
if (openTargets) {
try {
get(`${address}/health`, async () => {
// server is already running
await openTargets();
}).on('error', () => {
// server is not running so we start it
server.listen(config.port, openTargets);
});
} catch (error) {
console.log(error);
completeStartup();
}
} else {
completeStartup();
}
61 changes: 32 additions & 29 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,40 @@ const openTarget = async (target: string) => {
}
};

export const handleArgs = async () => {
try {
const args = process.argv.slice(2);
const positionals: string[] = [];
let parseOptions = true;
export const completeStartup = () => {
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');
}
};

for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (!(arg.startsWith('-') && parseOptions)) {
positionals.push(arg);
continue;
}
switch (arg) {
case '-v':
case '--version':
console.log(`vivify-server ${process.env.VERSION ?? 'dev'}`);
break;
case '--':
parseOptions = false;
break;
default:
console.log(`Unknown option "${arg}"`);
}
export const handleArgs = (): (() => Promise<void>) | undefined => {
const args = process.argv.slice(2);
const positionals: string[] = [];
let parseOptions = true;

for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (!(arg.startsWith('-') && parseOptions)) {
positionals.push(arg);
continue;
}
await Promise.all(positionals.map((target) => openTarget(target)));
} 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');
switch (arg) {
case '-v':
case '--version':
console.log(`vivify-server ${process.env.VERSION ?? 'dev'}`);
return;
case '--':
parseOptions = false;
break;
default:
console.log(`Unknown option "${arg}"`);
}
}
return async () => {
await Promise.all(positionals.map((target) => openTarget(target)));
completeStartup();
};
};
2 changes: 1 addition & 1 deletion viv
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ arguments:
source file
options:
--help show this help message and exit
--version show version information
--version show version information and exit
EOF
}

Expand Down

0 comments on commit e27529f

Please sign in to comment.