Skip to content

Commit 3f11644

Browse files
committed
✨ Update _fetchRemoteEmojis and _createCache methods
Add feedback when using the cache method (success and error messages), prevent creating gitmojis.json cache file when there's no internet connection, to avoid empty and invalid json creation. #1 Signed-off-by: Carlos Cuesta <carloscuesta@me.com>
1 parent 00e68e7 commit 3f11644

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/gitmoji.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ class GitmojiCli {
6767
break;
6868

6969
default:
70-
console.error(chalk.red(
71-
`ERROR: unexpected mode [${mode}]`
72-
));
70+
console.error(chalk.red(`ERROR: unexpected mode [${mode}]`));
7371
}
7472
});
7573
})
@@ -195,17 +193,24 @@ class GitmojiCli {
195193

196194
_createCache(cachePath, emojis) {
197195
const cacheDir = path.dirname(cachePath);
198-
if (!pathExists.sync(cacheDir)) {
199-
fs.mkdirSync(cacheDir);
196+
197+
if (emojis !== undefined) {
198+
if (!pathExists.sync(cacheDir)) {
199+
fs.mkdirSync(cacheDir);
200+
}
201+
fs.writeFileSync(cachePath, JSON.stringify(emojis));
200202
}
201-
fs.writeFileSync(cachePath, JSON.stringify(emojis));
202203
}
203204

204205
_fetchRemoteEmojis() {
205206
return this._gitmojiApiClient.request({
206207
method: 'GET',
207208
url: '/src/data/gitmojis.json'
208-
}).then(res => res.data.gitmojis);
209+
}).then(res => {
210+
console.log(`${chalk.yellow('Gitmojis')} updated succesfully!`);
211+
return res.data.gitmojis;
212+
})
213+
.catch(err => console.error(chalk.red(`ERROR: Network connection not found - ${err.code}`)));
209214
}
210215

211216
_fetchCachedEmojis(cachePath) {

0 commit comments

Comments
 (0)