Skip to content
This repository was archived by the owner on Nov 21, 2025. It is now read-only.

Commit 550493b

Browse files
Keen Yee Liauayazhafiz
authored andcommitted
refactor: move command line utils to standalone module (#445)
This commit moves the command line parsing methods to a standalone file so that they can be shared with the loader (banner) later on. This is in preparation for removing banner.js and rewrite it in TypeScript instead to remove code duplication.
1 parent d290312 commit 550493b

File tree

3 files changed

+110
-45
lines changed

3 files changed

+110
-45
lines changed

server/src/cmdline_utils.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
function findArgument(argv: string[], argName: string): string|undefined {
10+
const index = argv.indexOf(argName);
11+
if (index < 0 || index === argv.length - 1) {
12+
return;
13+
}
14+
return argv[index + 1];
15+
}
16+
17+
function parseStringArray(argv: string[], argName: string): string[] {
18+
const arg = findArgument(argv, argName);
19+
if (!arg) {
20+
return [];
21+
}
22+
return arg.split(',');
23+
}
24+
25+
function hasArgument(argv: string[], argName: string): boolean {
26+
return argv.includes(argName);
27+
}
28+
29+
interface CommandLineOptions {
30+
help: boolean;
31+
logFile?: string;
32+
logVerbosity?: string;
33+
ngProbeLocations: string[];
34+
tsProbeLocations: string[];
35+
}
36+
37+
export function parseCommandLine(argv: string[]): CommandLineOptions {
38+
return {
39+
help: hasArgument(argv, '--help'),
40+
logFile: findArgument(argv, '--logFile'),
41+
logVerbosity: findArgument(argv, '--logVerbosity'),
42+
ngProbeLocations: parseStringArray(argv, '--ngProbeLocations'),
43+
tsProbeLocations: parseStringArray(argv, '--tsProbeLocations'),
44+
};
45+
}
46+
47+
export function generateHelpMessage(argv: string[]) {
48+
return `Angular Language Service that implements the Language Server Protocol (LSP).
49+
50+
Usage: ${argv[0]} ${argv[1]} [options]
51+
52+
Options:
53+
--help: Prints help message.
54+
--logFile: Location to log messages. Logging is disabled if not provided.
55+
--logVerbosity: terse|normal|verbose|requestTime. See ts.server.LogLevel.
56+
--ngProbeLocations: Path of @angular/language-service. Required.
57+
--tsProbeLocations: Path of typescript. Required.
58+
59+
Additional options supported by vscode-languageserver:
60+
--clientProcessId=<number>: Automatically kills the server if the client process dies.
61+
--node-ipc: Communicate using Node's IPC. This is the default.
62+
--stdio: Communicate over stdin/stdout.
63+
--socket=<number>: Communicate using Unix socket.
64+
`;
65+
}

server/src/server.ts

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,27 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {generateHelpMessage, parseCommandLine} from './cmdline_utils';
910
import {createLogger} from './logger';
1011
import {ServerHost} from './server_host';
1112
import {Session} from './session';
1213
import {resolveWithMinMajor} from './version_provider';
1314

1415
// Parse command line arguments
15-
const help = hasArgument('--help');
16-
const logFile = findArgument('--logFile');
17-
const logVerbosity = findArgument('--logVerbosity');
18-
const ngProbeLocations = parseStringArray('--ngProbeLocations');
19-
const tsProbeLocations = parseStringArray('--tsProbeLocations');
16+
const options = parseCommandLine(process.argv);
2017

21-
if (help) {
22-
const {argv} = process;
23-
console.error(`Angular Language Service that implements the Language Server Protocol (LSP).
24-
25-
Usage: ${argv[0]} ${argv[1]} [options]
26-
27-
Options:
28-
--help: Prints help message.
29-
--logFile: Location to log messages. Logging is disabled if not provided.
30-
--logVerbosity: terse|normal|verbose|requestTime. See ts.server.LogLevel.
31-
--ngProbeLocations: Path of @angular/language-service. Required.
32-
--tsProbeLocations: Path of typescript. Required.
33-
34-
Additional options supported by vscode-languageserver:
35-
--clientProcessId=<number>: Automatically kills the server if the client process dies.
36-
--node-ipc: Communicate using Node's IPC. This is the default.
37-
--stdio: Communicate over stdin/stdout.
38-
--socket=<number>: Communicate using Unix socket.
39-
`);
18+
if (options.help) {
19+
console.error(generateHelpMessage(process.argv));
4020
process.exit(0);
4121
}
4222

4323
// Create a logger that logs to file. OK to emit verbose entries.
44-
const logger = createLogger({logFile, logVerbosity});
24+
const logger = createLogger({
25+
logFile: options.logFile,
26+
logVerbosity: options.logVerbosity,
27+
});
4528

29+
const {tsProbeLocations, ngProbeLocations} = options;
4630
const ts = resolveWithMinMajor('typescript', 3, tsProbeLocations);
4731
const ng = resolveWithMinMajor('@angular/language-service', 9, ngProbeLocations);
4832

@@ -69,23 +53,3 @@ if (process.env.TSC_NONPOLLING_WATCHER !== 'true') {
6953
}
7054

7155
session.listen();
72-
73-
function hasArgument(argName: string): boolean {
74-
return process.argv.includes(argName);
75-
}
76-
77-
function findArgument(argName: string): string|undefined {
78-
const index = process.argv.indexOf(argName);
79-
if (index < 0 || index === process.argv.length - 1) {
80-
return;
81-
}
82-
return process.argv[index + 1];
83-
}
84-
85-
function parseStringArray(argName: string): string[] {
86-
const arg = findArgument(argName);
87-
if (!arg) {
88-
return [];
89-
}
90-
return arg.split(',');
91-
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {parseCommandLine} from '../cmdline_utils';
10+
11+
describe('parseCommandLine', () => {
12+
it('should parse "help"', () => {
13+
const options = parseCommandLine(['--help']);
14+
expect(options.help).toBe(true);
15+
});
16+
17+
it('should parse "logFile"', () => {
18+
const options = parseCommandLine(['--logFile', 'foo.log']);
19+
expect(options.logFile).toBe('foo.log');
20+
});
21+
22+
it('should parse "logVerbosity"', () => {
23+
const options = parseCommandLine(['--logVerbosity', 'normal']);
24+
expect(options.logVerbosity).toBe('normal');
25+
});
26+
27+
it('should parse "ngProbeLocations"', () => {
28+
const options = parseCommandLine(['--ngProbeLocations', '/foo,/bar']);
29+
expect(options.ngProbeLocations).toEqual(['/foo', '/bar']);
30+
});
31+
32+
it('should parse "tsProbeLocations"', () => {
33+
const options = parseCommandLine(['--tsProbeLocations', '/baz,/qux']);
34+
expect(options.tsProbeLocations).toEqual(['/baz', '/qux']);
35+
});
36+
});

0 commit comments

Comments
 (0)