-
Notifications
You must be signed in to change notification settings - Fork 2
/
hyper-pokemon-cli.js
106 lines (96 loc) · 2.67 KB
/
hyper-pokemon-cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const fs = require("fs");
const inquirer = require("inquirer");
const chalk = require("chalk");
const homeDir = require("home-dir");
const randomItem = require("random-item");
const yaml = require("js-yaml");
const log = console.log;
const hyperConfig = require(homeDir("/.hyper.js"));
const pokemonDir = homeDir(
"/.hyper_plugins/node_modules/hyper-pokemon/"
);
if (!hyperConfig.plugins.includes("hyper-pokemon")) {
log(
chalk.red(
"You doesn't seem to have 'hyper-pokemon' installed in your system"
)
);
process.exit(1);
}
const listOfQuestions = availablePokemon => {
return [
{
type: "list",
name: "pokemon",
message: "Choose which pokemon theme",
choices: availablePokemon
},
{
type: "list",
name: "color",
message: "Choose which color scheme",
choices: ["light", "dark"]
},
{
type: "input",
name: "unibody",
message:
"unibody color? (header color is the same as the theme background) Y/n"
}
];
};
const hyperPokemon = (inputs, flags) => {
if (flags.random) {
const currentPokemon = hyperConfig.config.pokemon;
getAvailablePokemon()
.then(availablePokemon => {
const index = availablePokemon.indexOf(currentPokemon);
if (index !== -1) {
availablePokemon.splice(index, 1);
}
setPokemonToBe(randomItem(availablePokemon));
});
} else {
askForPokemon();
}
};
function setPokemonToBe(pokemon, color, unibody) {
hyperConfig.config.pokemon = pokemon;
hyperConfig.config.color = color ? color : 'dark';
hyperConfig.config.unibody = unibody ? !(unibody === "n") : false;
const json = JSON.stringify(hyperConfig, null, " ").replace(
/\"([^(\")"]+)\":/g,
"$1:"
);
const fileContent = `module.exports = ${json};`;
fs.writeFile(homeDir("/.hyper.js"), fileContent, "utf8", err => {
if (err) throw err;
log(
`${chalk.green(
"Success!"
)} Your hyper-pokemon theme was changed to ${chalk.bold(
color ? color : 'dark' + " " + pokemon
)}
Reload your terminal to see the changes!`
);
});
}
function askForPokemon() {
getAvailablePokemon()
.then(availablePokemon => {
return inquirer.prompt(listOfQuestions(availablePokemon));
})
.then(answers => {
const { pokemon, color, unibody } = answers;
setPokemonToBe(pokemon, color, unibody);
});
}
const getAvailablePokemon = () => {
return new Promise((resolve, reject) => {
fs.readFile(pokemonDir + "/pokemon.yml", "utf-8", (err, data) => {
if (err) reject(err);
else resolve(Object.keys(yaml.safeLoad(data).pokemon));
});
});
};
module.exports = hyperPokemon;