1- const path = require ( "path" ) ;
2- const fs = require ( "fs" ) ;
3- const yaml = require ( "yaml" ) ;
4- const chalk = require ( "chalk" ) ;
1+ import * as path from "path"
2+ import * as fs from "fs"
3+ import * as yaml from "yaml"
4+ import * as chalk from "chalk"
5+ import Config from "./types/config"
6+ import Resources from "./types/resources"
7+ import Flags from "./types/flags"
8+ import Cords from "./types/cords"
59const log = console . log ;
610const error = chalk . bold . red ;
711const warning = chalk . keyword ( "orange" ) ;
812
913class Alacritty {
14+ public basePath : string ;
15+ public configFile : string ;
16+ public config : Config ;
17+ public resources : Resources ;
1018 constructor ( ) {
11- this . basePath = "" ;
12- this . configFile = "" ;
13- this . config = { } ;
14- this . resources = "" ;
1519 this . resourcePath = this . resourcePath . bind ( this ) ;
1620 this . load = this . load . bind ( this ) ;
1721 this . changeTheme = this . changeTheme . bind ( this ) ;
@@ -34,7 +38,7 @@ class Alacritty {
3438 init ( ) {
3539 this . basePath = path . resolve ( process . env . HOME , ".config/alacritty" ) ;
3640 if ( ! fs . existsSync ( this . basePath ) ) {
37- log ( error ( `Config directory not found: ${ this . base_path } ` ) ) ;
41+ log ( error ( `Config directory not found: ${ this . basePath } ` ) ) ;
3842 process . exit ( ) ;
3943 }
4044 this . configFile = `${ this . basePath } /alacritty.yml` ;
@@ -60,12 +64,12 @@ class Alacritty {
6064 type : "Fonts file" ,
6165 path : `${ this . basePath } /fonts.yaml` ,
6266 exists : ( ) => fs . existsSync ( this . resources [ "fonts" ] [ "path" ] ) ,
63- create : ( ) => fs . writeFileSync ( this . resources [ "fonts" ] [ "path" ] ) ,
67+ create : ( ) => fs . writeFileSync ( this . resources [ "fonts" ] [ "path" ] , "" )
6468 } ,
6569 } ;
6670 }
6771
68- load ( yamlFile ) {
72+ load ( yamlFile : string ) {
6973 try {
7074 return yaml . parse (
7175 fs . readFileSync ( yamlFile , {
@@ -78,7 +82,7 @@ class Alacritty {
7882 }
7983 }
8084
81- resourcePath ( resource ) {
85+ resourcePath ( resource : string ) {
8286 if ( ! ( resource in this . resources ) ) {
8387 log ( error ( `Path for resource "${ resource } " not set` ) ) ;
8488 process . exit ( ) ;
@@ -100,8 +104,8 @@ class Alacritty {
100104 } ) ;
101105 }
102106
103- apply ( config ) {
104- if ( config === null || config . length < 1 ) {
107+ apply ( flags : Flags ) {
108+ if ( flags === null || Object . keys ( flags ) . length < 1 ) {
105109 log ( error ( "No options provided" ) ) ;
106110 process . exit ( ) ;
107111 }
@@ -119,9 +123,9 @@ class Alacritty {
119123
120124 let errorsFound = 0 ;
121125 for ( const param in actions ) {
122- if ( param in config ) {
126+ if ( param in flags ) {
123127 try {
124- actions [ param ] ( config [ param ] ) ;
128+ actions [ param ] ( flags [ param ] ) ;
125129 } catch ( err ) {
126130 log ( error ( err ) ) ;
127131 errorsFound += 1 ;
@@ -136,7 +140,7 @@ class Alacritty {
136140 }
137141 }
138142
139- changeTheme ( theme ) {
143+ changeTheme ( theme : string ) {
140144 const themesDirectory = this . resourcePath ( "themes" ) ;
141145 const themeFile = `${ themesDirectory } /${ theme } .yaml` ;
142146 if ( ! fs . existsSync ( themeFile ) ) {
@@ -145,7 +149,7 @@ class Alacritty {
145149 }
146150 const themeYaml = this . load ( themeFile ) ;
147151 if ( themeYaml === null ) {
148- log ( error ( `File ${ themeFile . name } is empty` ) ) ;
152+ log ( error ( `File ${ theme } is empty` ) ) ;
149153 process . exit ( ) ;
150154 }
151155 if ( ! themeYaml [ "colors" ] ) {
@@ -184,7 +188,7 @@ class Alacritty {
184188 log ( chalk . blue ( `Theme ${ theme } applied` ) ) ;
185189 }
186190
187- changeFontSize ( size ) {
191+ changeFontSize ( size : number ) {
188192 size = Number ( size ) ;
189193 if ( size <= 0 ) {
190194 log ( error ( "Font size cannot be negative or zero" ) ) ;
@@ -201,7 +205,7 @@ class Alacritty {
201205 log ( chalk . blue ( `Font size set to ${ size } ` ) ) ;
202206 }
203207
204- changeOpacity ( opacity ) {
208+ changeOpacity ( opacity : number ) {
205209 opacity = Number ( opacity ) ;
206210 if ( opacity < 0.0 || opacity > 1.0 ) {
207211 log ( error ( "Opacity should be between 0.0 and 1.0" ) ) ;
@@ -212,7 +216,7 @@ class Alacritty {
212216 log ( chalk . blue ( `Opacity set to ${ opacity } ` ) ) ;
213217 }
214218
215- changeFont ( font ) {
219+ changeFont ( font : string ) {
216220 if ( ! ( "font" in this . config ) ) {
217221 this . config [ "font" ] = { } ;
218222 log ( warning ( '"font" prop was not present in alacritty.yml' ) ) ;
@@ -263,15 +267,15 @@ class Alacritty {
263267 this . config [ "font" ] [ fontTypes [ t ] ] [ "family" ] = fonts [ font ] [ fontTypes [ t ] ]
264268 ? fonts [ font ] [ fontTypes [ t ] ]
265269 : "tmp" ;
266- const capitalize = ( [ firstLetter , ... restOfWord ] ) =>
267- firstLetter . toUpperCase ( ) + restOfWord . join ( "" ) ;
268- const fontType = fontTypes [ t ] === "normal" ? "regular" : fontTypes [ t ] ;
270+ const capitalize = ( words : string ) => words . charAt ( 0 ) . toUpperCase ( ) + words . slice ( 1 ) ;
271+ // firstLetter.toUpperCase() + restOfWord.join("");
272+ const fontType : string = fontTypes [ t ] === "normal" ? "regular" : fontTypes [ t ] ;
269273 this . config [ "font" ] [ fontTypes [ t ] ] [ "style" ] = capitalize ( fontType ) ;
270274 }
271275 log ( chalk . blue ( `Font ${ font } applied` ) ) ;
272276 }
273277
274- changePadding ( padding ) {
278+ changePadding ( padding : Cords ) {
275279 if ( Object . keys ( padding ) . length !== 2 ) {
276280 log ( error ( "Padding should only have an x and y value" ) ) ;
277281 }
@@ -301,7 +305,7 @@ class Alacritty {
301305 log ( chalk . blue ( `Padding set to x: ${ x } , y: ${ y } ` ) ) ;
302306 }
303307
304- changeFontOffset ( offset ) {
308+ changeFontOffset ( offset : Cords ) {
305309 if ( Object . keys ( offset ) . length != 2 ) {
306310 log ( error ( "Offset should only have an x and y value" ) ) ;
307311 }
@@ -329,7 +333,7 @@ class Alacritty {
329333 log ( chalk . blue ( `Offset set to x: ${ x } , y: ${ y } ` ) ) ;
330334 }
331335
332- list ( toBeListed ) {
336+ list ( toBeListed : string ) {
333337 const options = {
334338 themes : this . listThemes ,
335339 fonts : this . listFonts ,
@@ -396,7 +400,7 @@ class Alacritty {
396400 log ( chalk . green ( yaml . stringify ( this . load ( fontsFile ) ) ) ) ;
397401 }
398402
399- printTheme ( theme ) {
403+ printTheme ( theme : string ) {
400404 const themesDir = this . resourcePath ( "themes" ) ;
401405 const themeFile = `${ themesDir } /${ theme } .yaml` ;
402406
@@ -408,14 +412,14 @@ class Alacritty {
408412 log ( yaml . stringify ( this . load ( themeFile ) ) ) ;
409413 }
410414
411- print ( toBePrinted ) {
415+ print ( toBePrinted : string ) {
412416 const options = {
413417 fonts : this . printFonts ,
414418 config : this . printConfig ,
415419 } ;
416420
417421 if ( toBePrinted . length == 0 ) {
418- toBePrinted . append ( "config" ) ;
422+ toBePrinted = "config" ;
419423 }
420424
421425 if ( toBePrinted in options ) {
@@ -425,4 +429,5 @@ class Alacritty {
425429 }
426430 }
427431}
428- module . exports = Alacritty ;
432+
433+ export default Alacritty
0 commit comments