2022-01-20 20:48:08 +08:00
|
|
|
/*---------------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const cp = require('child_process');
|
|
|
|
const path = require('path');
|
|
|
|
const os = require('os');
|
|
|
|
const opn = require('opn');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const minimist = require('minimist');
|
|
|
|
|
|
|
|
const args = minimist(process.argv.slice(2), {
|
|
|
|
boolean: [
|
|
|
|
'help',
|
2022-01-21 00:26:16 +08:00
|
|
|
'launch'
|
2022-01-20 20:48:08 +08:00
|
|
|
],
|
|
|
|
string: [
|
|
|
|
'host',
|
|
|
|
'port',
|
|
|
|
'driver',
|
2022-01-21 19:11:47 +08:00
|
|
|
'connection-token',
|
|
|
|
'server-data-dir'
|
2022-01-20 20:48:08 +08:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (args.help) {
|
|
|
|
console.log(
|
|
|
|
'./scripts/code-server.sh|bat [options]\n' +
|
|
|
|
' --launch Opens a browser'
|
|
|
|
);
|
2022-01-21 22:15:12 +08:00
|
|
|
// more help options will be printed by startServer
|
2022-01-20 20:48:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const serverArgs = process.argv.slice(2).filter(v => v !== '--launch');
|
|
|
|
|
|
|
|
const HOST = args['host'] ?? 'localhost';
|
|
|
|
const PORT = args['port'] ?? '9888';
|
|
|
|
const TOKEN = args['connection-token'] ?? String(crypto.randomInt(0xffffffff));
|
|
|
|
|
2022-01-22 00:37:36 +08:00
|
|
|
if (args['connection-token'] === undefined && args['connection-token-file'] === undefined && !args['without-connection-token']) {
|
2022-01-20 20:48:08 +08:00
|
|
|
serverArgs.push('--connection-token', TOKEN);
|
|
|
|
}
|
|
|
|
if (args['host'] === undefined) {
|
|
|
|
serverArgs.push('--host', HOST);
|
|
|
|
}
|
|
|
|
if (args['port'] === undefined) {
|
|
|
|
serverArgs.push('--port', PORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
const env = { ...process.env };
|
|
|
|
|
2022-01-21 21:33:34 +08:00
|
|
|
const entryPoint = path.join(__dirname, '..', 'out', 'server-main.js');
|
2022-01-20 20:48:08 +08:00
|
|
|
startServer();
|
|
|
|
|
|
|
|
function startServer() {
|
|
|
|
console.log(`Starting server: ${entryPoint} ${serverArgs.join(' ')}`);
|
2022-01-28 03:22:34 +08:00
|
|
|
const proc = cp.spawn(process.execPath, [entryPoint, ...serverArgs], { env, stdio: 'inherit' });
|
2022-01-20 20:48:08 +08:00
|
|
|
|
2022-01-21 19:11:47 +08:00
|
|
|
proc.on('exit', (code) => process.exit(code));
|
2022-01-20 20:48:08 +08:00
|
|
|
|
|
|
|
process.on('exit', () => proc.kill());
|
|
|
|
process.on('SIGINT', () => {
|
|
|
|
proc.kill();
|
|
|
|
process.exit(128 + 2); // https://nodejs.org/docs/v14.16.0/api/process.html#process_signal_events
|
|
|
|
});
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
proc.kill();
|
|
|
|
process.exit(128 + 15); // https://nodejs.org/docs/v14.16.0/api/process.html#process_signal_events
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args['launch']) {
|
|
|
|
opn(`http://${HOST}:${PORT}/?tkn=${TOKEN}`);
|
|
|
|
}
|