|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | import * as fs from 'fs';
|
| 10 | +import * as path from 'path'; |
10 | 11 |
|
11 | 12 | const MIN_TS_VERSION = '4.1';
|
12 | 13 | const MIN_NG_VERSION = '11.2';
|
13 | 14 | export const NGLANGSVC = '@angular/language-service';
|
| 15 | +const TSSERVERLIB = 'typescript/lib/tsserverlibrary'; |
14 | 16 |
|
15 | 17 | /**
|
16 | 18 | * Represents a valid node module that has been successfully resolved.
|
@@ -75,8 +77,41 @@ function resolveWithMinVersion(
|
75 | 77 | * @param probeLocations
|
76 | 78 | */
|
77 | 79 | export function resolveTsServer(probeLocations: string[]): NodeModule {
|
78 |
| - const tsserver = 'typescript/lib/tsserverlibrary'; |
79 |
| - return resolveWithMinVersion(tsserver, MIN_TS_VERSION, probeLocations, 'typescript'); |
| 80 | + if (probeLocations.length > 0) { |
| 81 | + // The first probe location is `typescript.tsdk` if it is specified. |
| 82 | + const resolvedFromTsdk = resolveTsServerFromTsdk(probeLocations[0]); |
| 83 | + if (resolvedFromTsdk !== undefined) { |
| 84 | + return resolvedFromTsdk; |
| 85 | + } |
| 86 | + } |
| 87 | + return resolveWithMinVersion(TSSERVERLIB, MIN_TS_VERSION, probeLocations, 'typescript'); |
| 88 | +} |
| 89 | + |
| 90 | +function resolveTsServerFromTsdk(tsdk: string): NodeModule|undefined { |
| 91 | + // `tsdk` is the folder path to the tsserver and lib*.d.ts files under a |
| 92 | + // TypeScript install, for example |
| 93 | + // - /google/src/head/depot/google3/third_party/javascript/node_modules/typescript/stable/lib |
| 94 | + if (!path.isAbsolute(tsdk)) { |
| 95 | + return undefined; |
| 96 | + } |
| 97 | + const tsserverlib = path.join(tsdk, 'tsserverlibrary.js'); |
| 98 | + if (!fs.existsSync(tsserverlib)) { |
| 99 | + return undefined; |
| 100 | + } |
| 101 | + const packageJson = path.resolve(tsserverlib, '../../package.json'); |
| 102 | + if (!fs.existsSync(packageJson)) { |
| 103 | + return undefined; |
| 104 | + } |
| 105 | + try { |
| 106 | + const json = JSON.parse(fs.readFileSync(packageJson, 'utf8')); |
| 107 | + return { |
| 108 | + name: TSSERVERLIB, |
| 109 | + resolvedPath: tsserverlib, |
| 110 | + version: new Version(json.version), |
| 111 | + }; |
| 112 | + } catch { |
| 113 | + return undefined; |
| 114 | + } |
80 | 115 | }
|
81 | 116 |
|
82 | 117 | /**
|
|
0 commit comments