Skip to content

Commit bc0574a

Browse files
Migrate to typescript
1 parent 1481daf commit bc0574a

File tree

14 files changed

+528
-3002
lines changed

14 files changed

+528
-3002
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/node_modules/
2-
dist
2+
bin

.npmignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
src
22
config
33
.gitignore
4-
package.json
5-
package-lock.json
4+
package-lock.json

package-lock.json

Lines changed: 343 additions & 2946 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "jscritty",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "CLI that allows you to change your Alacritty config with one command without editting the config file.",
5-
"main": "src/index.js",
6-
"bin": "bin/outside",
5+
"main": "bin/index.js",
6+
"bin": "bin/index.js",
77
"scripts": {
8-
"build": "babel src --out-dir dist --presets=@babel/preset-env",
8+
"build": "tsc",
99
"prepare": "npm run build"
1010
},
1111
"author": "@CodingLeonardo <Leonardo Rivero>",
@@ -19,8 +19,9 @@
1919
"yaml": "^1.10.0"
2020
},
2121
"devDependencies": {
22-
"@babel/cli": "^7.12.10",
23-
"@babel/core": "^7.12.10",
24-
"@babel/preset-env": "^7.12.11"
22+
"@types/chalk": "^2.2.0",
23+
"@types/node": "^16.9.6",
24+
"ts-node": "^10.2.1",
25+
"typescript": "^4.4.3"
2526
}
2627
}

src/alacritty.js renamed to src/alacritty.ts

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
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"
59
const log = console.log;
610
const error = chalk.bold.red;
711
const warning = chalk.keyword("orange");
812

913
class 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

src/cli.js renamed to src/cli.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
const { Command } = require("commander");
1+
import { Command } from "commander"
2+
import { getVersion } from "./getVersion"
23
const program = new Command();
4+
const version = getVersion()
35

4-
program.version("1.0.0");
6+
program.version(version);
57
program.name("jscritty");
68
program.description(
79
"CLI that allows you to change your Alacritty config with one command without editting the config file."
@@ -78,6 +80,6 @@ if (flags.print === undefined) {
7880
delete flags.print;
7981
}
8082

81-
module.exports = {
82-
args: flags,
83-
};
83+
export const args = {
84+
...flags
85+
}

src/getVersion.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as fs from "fs"
2+
import * as path from "path"
3+
4+
export const getVersion = () => {
5+
const packageJSONPath = path.resolve(__dirname, "../package.json")
6+
const content = fs.readFileSync(packageJSONPath, { encoding: "utf8" })
7+
const config = JSON.parse(content)
8+
return config.version
9+
}
10+

src/index.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env node
2+
3+
import Alacritty from "./alacritty";
4+
import { args } from "./cli";
5+
6+
const alacritty = new Alacritty();
7+
alacritty.apply(args);
8+
alacritty.save();
9+

src/types/config.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
type Config = {
2+
colors?: {
3+
primary?: {
4+
background: string,
5+
foreground: string
6+
},
7+
normal?: {
8+
black: string,
9+
red: string,
10+
green: string,
11+
yellow: string,
12+
blue: string,
13+
magenta: string,
14+
cyan: string,
15+
white: string
16+
},
17+
bright?: {
18+
black: string,
19+
red: string,
20+
green: string,
21+
yellow: string,
22+
blue: string,
23+
magenta: string,
24+
cyan: string,
25+
white: string,
26+
},
27+
},
28+
background_opacity?: number
29+
font?: {
30+
normal?: {
31+
family: string,
32+
style: string
33+
},
34+
bold?: {
35+
family: string,
36+
style: string
37+
},
38+
italic?: {
39+
family: string,
40+
style: string
41+
},
42+
size?: number,
43+
offset?: {
44+
x?: number,
45+
y?: number
46+
}
47+
}
48+
window?: {
49+
padding?: {
50+
x?: number,
51+
y?: number
52+
}
53+
}
54+
};
55+
56+
export default Config;

0 commit comments

Comments
 (0)