1- 'use strict' ;
2-
3- import process from 'process' ;
4- import fs from 'fs' ;
5- import path from 'path' ;
1+ import * as process from 'process' ;
2+ import * as fs from 'fs' ;
3+ import * as path from 'path' ;
64
75import isThere from 'is-there' ;
8- import mkdirp from 'mkdirp' ;
6+ import * as mkdirp from 'mkdirp' ;
97import camelcase from "camelcase"
108
119import FileSystemLoader from './fileSystemLoader' ;
12- import os from 'os' ;
10+ import * as os from 'os' ;
1311
14- function removeExtension ( filePath ) {
12+ function removeExtension ( filePath : string ) : string {
1513 const ext = path . extname ( filePath ) ;
1614 return filePath . replace ( new RegExp ( ext + '$' ) , '' ) ;
1715}
1816
17+ interface DtsContentOptions {
18+ dropExtension : boolean ;
19+ rootDir : string ;
20+ searchDir : string ;
21+ outDir : string ;
22+ rInputPath : string ;
23+ rawTokenList : string [ ] ;
24+ resultList : string [ ] ;
25+ EOL : string ;
26+ }
27+
1928class DtsContent {
20- constructor ( {
21- dropExtension ,
22- rootDir ,
23- searchDir ,
24- outDir ,
25- rInputPath ,
26- rawTokenList ,
27- resultList ,
28- EOL
29- } ) {
30- this . dropExtension = dropExtension ;
31- this . rootDir = rootDir ;
32- this . searchDir = searchDir ;
33- this . outDir = outDir ;
34- this . rInputPath = rInputPath ;
35- this . rawTokenList = rawTokenList ;
36- this . resultList = resultList ;
37- this . EOL = EOL ;
29+ private dropExtension : boolean ;
30+ private rootDir : string ;
31+ private searchDir : string ;
32+ private outDir : string ;
33+ private rInputPath : string ;
34+ private rawTokenList : string [ ] ;
35+ private resultList : string [ ] ;
36+ private EOL : string ;
37+
38+ constructor ( options : DtsContentOptions ) {
39+ this . dropExtension = options . dropExtension ;
40+ this . rootDir = options . rootDir ;
41+ this . searchDir = options . searchDir ;
42+ this . outDir = options . outDir ;
43+ this . rInputPath = options . rInputPath ;
44+ this . rawTokenList = options . rawTokenList ;
45+ this . resultList = options . resultList ;
46+ this . EOL = options . EOL ;
3847 }
3948
40- get contents ( ) {
49+ public get contents ( ) : string [ ] {
4150 return this . resultList ;
4251 }
4352
44- get formatted ( ) {
53+ public get formatted ( ) : string {
4554 if ( ! this . resultList || ! this . resultList . length ) return '' ;
4655 return [
4756 'declare const styles: {' ,
@@ -52,20 +61,20 @@ class DtsContent {
5261 ] . join ( os . EOL ) + this . EOL ;
5362 }
5463
55- get tokens ( ) {
64+ public get tokens ( ) : string [ ] {
5665 return this . rawTokenList ;
5766 }
5867
59- get outputFilePath ( ) {
68+ public get outputFilePath ( ) : string {
6069 const outputFileName = this . dropExtension ? removeExtension ( this . rInputPath ) : this . rInputPath ;
6170 return path . join ( this . rootDir , this . outDir , outputFileName + '.d.ts' ) ;
6271 }
6372
64- get inputFilePath ( ) {
73+ public get inputFilePath ( ) : string {
6574 return path . join ( this . rootDir , this . searchDir , this . rInputPath ) ;
6675 }
6776
68- writeFile ( ) {
77+ public writeFile ( ) : Promise < DtsContent > {
6978 var outPathDir = path . dirname ( this . outputFilePath ) ;
7079 if ( ! isThere ( outPathDir ) ) {
7180 mkdirp . sync ( outPathDir ) ;
@@ -82,8 +91,29 @@ class DtsContent {
8291 }
8392}
8493
94+ type CamelCaseOption = boolean | 'dashes' | undefined ;
95+
96+ interface DtsCreatorOptions {
97+ rootDir ?: string ;
98+ searchDir ?: string ;
99+ outDir ?: string ;
100+ camelCase ?: CamelCaseOption ;
101+ dropExtension ?: boolean ;
102+ EOL ?: string ;
103+ }
104+
85105export class DtsCreator {
86- constructor ( options ) {
106+ private rootDir : string ;
107+ private searchDir : string ;
108+ private outDir : string ;
109+ private loader : FileSystemLoader ;
110+ private inputDirectory : string ;
111+ private outputDirectory : string ;
112+ private camelCase : boolean | 'dashes' | undefined ;
113+ private dropExtension : boolean ;
114+ private EOL : string ;
115+
116+ constructor ( options ?: DtsCreatorOptions ) {
87117 if ( ! options ) options = { } ;
88118 this . rootDir = options . rootDir || process . cwd ( ) ;
89119 this . searchDir = options . searchDir || '' ;
@@ -96,9 +126,9 @@ export class DtsCreator {
96126 this . EOL = options . EOL || os . EOL ;
97127 }
98128
99- create ( filePath , initialContents , clearCache = false ) {
129+ create ( filePath : string , initialContents ?: string , clearCache : boolean = false ) : Promise < DtsContent > {
100130 return new Promise ( ( resolve , reject ) => {
101- var rInputPath ;
131+ let rInputPath : string ;
102132 if ( path . isAbsolute ( filePath ) ) {
103133 rInputPath = path . relative ( this . inputDirectory , filePath ) ;
104134 } else {
@@ -107,7 +137,7 @@ export class DtsCreator {
107137 if ( clearCache ) {
108138 this . loader . tokensByFile = { } ;
109139 }
110- this . loader . fetch ( filePath , "/" , undefined , initialContents ) . then ( res => {
140+ this . loader . fetch ( filePath , "/" , undefined , initialContents ) . then ( ( res ) => {
111141 if ( res ) {
112142 var tokens = res ;
113143 var keys = Object . keys ( tokens ) ;
@@ -137,7 +167,7 @@ export class DtsCreator {
137167 } ) ;
138168 }
139169
140- getConvertKeyMethod ( camelCaseOption ) {
170+ private getConvertKeyMethod ( camelCaseOption : CamelCaseOption ) : ( str : string ) => string {
141171 switch ( camelCaseOption ) {
142172 case true :
143173 return camelcase ;
@@ -154,7 +184,7 @@ export class DtsCreator {
154184 * Mirrors the behaviour of the css-loader:
155185 * https://github.com/webpack-contrib/css-loader/blob/1fee60147b9dba9480c9385e0f4e581928ab9af9/lib/compile-exports.js#L3-L7
156186 */
157- dashesCamelCase ( str ) {
187+ private dashesCamelCase ( str : string ) : string {
158188 return str . replace ( / - + ( \w ) / g, function ( match , firstLetter ) {
159189 return firstLetter . toUpperCase ( ) ;
160190 } ) ;
0 commit comments