1+ /// <reference path="../../compiler/core.ts" />
12/// <reference path="../../services/jsTyping.ts"/>
23/// <reference path="../types.d.ts"/>
34
45namespace ts . server . typingsInstaller {
5- const DefaultTsdSettings = JSON . stringify ( {
6- version : "v4" ,
7- repo : "DefinitelyTyped/DefinitelyTyped" ,
8- ref : "master" ,
9- path : "typings"
10- } , /*replacer*/ undefined , /*space*/ 4 ) ;
11-
12- interface TsdConfig {
13- installed : MapLike < any > ;
6+ interface NpmConfig {
7+ devDependencies : MapLike < any > ;
148 }
159
1610 export interface Log {
@@ -23,18 +17,17 @@ namespace ts.server.typingsInstaller {
2317 writeLine : ( ) => { }
2418 } ;
2519
26- function tsdTypingToFileName ( cachePath : string , tsdTypingFile : string ) {
27- return combinePaths ( cachePath , `typings/${ tsdTypingFile } ` ) ;
20+ function typingToFileName ( cachePath : string , packageName : string , installTypingHost : InstallTypingHost ) : string {
21+ const result = resolveModuleName ( packageName , combinePaths ( cachePath , "index.d.ts" ) , { moduleResolution : ModuleResolutionKind . NodeJs } , installTypingHost ) ;
22+ return result . resolvedModule ? result . resolvedModule . resolvedFileName : null ;
2823 }
2924
30- function getPackageName ( tsdTypingFile : string ) {
31- const idx = tsdTypingFile . indexOf ( "/" ) ;
32- return idx > 0 ? tsdTypingFile . substr ( 0 , idx ) : undefined ;
25+ function getPackageName ( typingFile : string ) {
26+ const idx = typingFile . lastIndexOf ( "/" ) ;
27+ return idx > 0 ? typingFile . substr ( idx + 1 ) : undefined ;
3328 }
3429
3530 export abstract class TypingsInstaller {
36- private isTsdInstalled : boolean ;
37-
3831 private packageNameToTypingLocation : Map < string > = createMap < string > ( ) ;
3932 private missingTypingsSet : Map < true > = createMap < true > ( ) ;
4033 private knownCachesSet : Map < true > = createMap < true > ( ) ;
@@ -50,20 +43,6 @@ namespace ts.server.typingsInstaller {
5043 }
5144
5245 init ( ) {
53- this . isTsdInstalled = this . isPackageInstalled ( "tsd" ) ;
54- if ( this . log . isEnabled ( ) ) {
55- this . log . writeLine ( `isTsdInstalled: ${ this . isTsdInstalled } ` ) ;
56- }
57-
58- if ( ! this . isTsdInstalled ) {
59- if ( this . log . isEnabled ( ) ) {
60- this . log . writeLine ( `tsd is not installed, installing tsd...` ) ;
61- }
62- this . isTsdInstalled = this . installPackage ( "tsd" ) ;
63- if ( this . log . isEnabled ( ) ) {
64- this . log . writeLine ( `isTsdInstalled: ${ this . isTsdInstalled } ` ) ;
65- }
66- }
6746 this . processCacheLocation ( this . globalCachePath ) ;
6847 }
6948
@@ -94,13 +73,6 @@ namespace ts.server.typingsInstaller {
9473 }
9574
9675 install ( req : DiscoverTypings ) {
97- if ( ! this . isTsdInstalled ) {
98- if ( this . log . isEnabled ( ) ) {
99- this . log . writeLine ( `tsd is not installed, ignoring request...` ) ;
100- }
101- return ;
102- }
103-
10476 if ( this . log . isEnabled ( ) ) {
10577 this . log . writeLine ( `Got install request ${ JSON . stringify ( req ) } ` ) ;
10678 }
@@ -153,23 +125,26 @@ namespace ts.server.typingsInstaller {
153125 }
154126 return ;
155127 }
156- const tsdJson = combinePaths ( cacheLocation , "tsd .json" ) ;
128+ const packageJson = combinePaths ( cacheLocation , "package .json" ) ;
157129 if ( this . log . isEnabled ( ) ) {
158- this . log . writeLine ( `Trying to find '${ tsdJson } '...` ) ;
130+ this . log . writeLine ( `Trying to find '${ packageJson } '...` ) ;
159131 }
160- if ( this . installTypingHost . fileExists ( tsdJson ) ) {
161- const tsdConfig = < TsdConfig > JSON . parse ( this . installTypingHost . readFile ( tsdJson ) ) ;
132+ if ( this . installTypingHost . fileExists ( packageJson ) ) {
133+ const npmConfig = < NpmConfig > JSON . parse ( this . installTypingHost . readFile ( packageJson ) ) ;
162134 if ( this . log . isEnabled ( ) ) {
163- this . log . writeLine ( `Loaded content of '${ tsdJson } ': ${ JSON . stringify ( tsdConfig ) } ` ) ;
135+ this . log . writeLine ( `Loaded content of '${ npmConfig } ': ${ JSON . stringify ( npmConfig ) } ` ) ;
164136 }
165- if ( tsdConfig . installed ) {
166- for ( const key in tsdConfig . installed ) {
167- // key is <package name>/<typing file >
137+ if ( npmConfig . devDependencies ) {
138+ for ( const key in npmConfig . devDependencies ) {
139+ // key is @types / <package name>
168140 const packageName = getPackageName ( key ) ;
169141 if ( ! packageName ) {
170142 continue ;
171143 }
172- const typingFile = tsdTypingToFileName ( cacheLocation , key ) ;
144+ var typingFile = typingToFileName ( cacheLocation , packageName , this . installTypingHost ) ;
145+ if ( ! typingFile ) {
146+ continue ;
147+ }
173148 const existingTypingFile = this . packageNameToTypingLocation [ packageName ] ;
174149 if ( existingTypingFile === typingFile ) {
175150 continue ;
@@ -204,20 +179,19 @@ namespace ts.server.typingsInstaller {
204179 return ;
205180 }
206181
207- // TODO: install typings and send response when they are ready
208- const tsdPath = combinePaths ( cachePath , "tsd.json" ) ;
182+ const npmConfigPath = combinePaths ( cachePath , "package.json" ) ;
209183 if ( this . log . isEnabled ( ) ) {
210- this . log . writeLine ( `Tsd config file: ${ tsdPath } ` ) ;
184+ this . log . writeLine ( `Npm config file: ${ npmConfigPath } ` ) ;
211185 }
212- if ( ! this . installTypingHost . fileExists ( tsdPath ) ) {
186+ if ( ! this . installTypingHost . fileExists ( npmConfigPath ) ) {
213187 if ( this . log . isEnabled ( ) ) {
214- this . log . writeLine ( `Tsd config file '${ tsdPath } ' is missing, creating new one...` ) ;
188+ this . log . writeLine ( `Npm config file: '${ npmConfigPath } ' is missing, creating new one...` ) ;
215189 }
216190 this . ensureDirectoryExists ( cachePath , this . installTypingHost ) ;
217- this . installTypingHost . writeFile ( tsdPath , DefaultTsdSettings ) ;
191+ this . installTypingHost . writeFile ( npmConfigPath , "{}" ) ;
218192 }
219193
220- this . runTsd ( cachePath , typingsToInstall , installedTypings => {
194+ this . runInstall ( cachePath , typingsToInstall , installedTypings => {
221195 // TODO: watch project directory
222196 if ( this . log . isEnabled ( ) ) {
223197 this . log . writeLine ( `Requested to install typings ${ JSON . stringify ( typingsToInstall ) } , installed typings ${ JSON . stringify ( installedTypings ) } ` ) ;
@@ -230,7 +204,11 @@ namespace ts.server.typingsInstaller {
230204 continue ;
231205 }
232206 installedPackages [ packageName ] = true ;
233- installedTypingFiles . push ( tsdTypingToFileName ( cachePath , t ) ) ;
207+ var typingFile = typingToFileName ( cachePath , packageName , this . installTypingHost ) ;
208+ if ( ! typingFile ) {
209+ continue ;
210+ }
211+ installedTypingFiles . push ( typingFile ) ;
234212 }
235213 if ( this . log . isEnabled ( ) ) {
236214 this . log . writeLine ( `Installed typing files ${ JSON . stringify ( installedTypingFiles ) } ` ) ;
@@ -287,14 +265,12 @@ namespace ts.server.typingsInstaller {
287265 typingOptions : request . typingOptions ,
288266 compilerOptions : request . compilerOptions ,
289267 typings,
290- files : request . fileNames ,
291268 kind : "set"
292269 } ;
293270 }
294271
295272 protected abstract isPackageInstalled ( packageName : string ) : boolean ;
296- protected abstract installPackage ( packageName : string ) : boolean ;
297273 protected abstract sendResponse ( response : SetTypings | InvalidateCachedTypings ) : void ;
298- protected abstract runTsd ( cachePath : string , typingsToInstall : string [ ] , postInstallAction : ( installedTypings : string [ ] ) => void ) : void ;
274+ protected abstract runInstall ( cachePath : string , typingsToInstall : string [ ] , postInstallAction : ( installedTypings : string [ ] ) => void ) : void ;
299275 }
300276}
0 commit comments