Skip to content

Commit 1b75d08

Browse files
committed
🚧 cli.js: Adds renaming post save functionality
1 parent 3ec0843 commit 1b75d08

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

‎cli.js‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#!/usr/bin/env node
22

3+
// Native
4+
const {promisify} = require('util');
5+
const {basename, extname} = require('path');
6+
const asyncRename = promisify(require('fs').rename);
7+
38
// Packages
49
const meow = require('meow');
510
const chalk = require('chalk');
611
const opn = require('opn');
712
const queryString = require('query-string');
813
const terminalImage = require('terminal-image');
14+
const generate = require('nanoid/generate');
915
const Listr = require('listr');
1016

1117
// Source
@@ -148,7 +154,18 @@ if (!file) {
148154
{
149155
title: 'Fetching beautiful image',
150156
skip: () => open,
151-
task: () => headlessVisit(url, location, settings.type)
157+
task: async ctx => {
158+
const {type} = settings;
159+
const original = basename(file, extname(file));
160+
const downloaded = `${location}/carbon.${type}`;
161+
const saveAs = `${location}/${original}-${generate('123456abcdef', 10)}.${type}`;
162+
163+
// Fetch image and rename it
164+
await headlessVisit(url, location, type);
165+
await asyncRename(downloaded, saveAs);
166+
167+
ctx.savedAs = saveAs;
168+
}
152169
}
153170
]);
154171

@@ -157,9 +174,7 @@ if (!file) {
157174
// and prefer it to async/await in this case… go ahead, JUDGE ME
158175
tasks
159176
.run()
160-
.then(async () => {
161-
const downloadedFile = `${location}/carbon.${settings.type}`;
162-
177+
.then(async ({savedAs}) => {
163178
console.log(`
164179
${chalk.green('Done!')}`
165180
);
@@ -170,14 +185,14 @@ if (!file) {
170185
);
171186
} else {
172187
console.log(`
173-
The file can be found here: ${downloadedFile} 😌`
188+
The file can be found here: ${savedAs} 😌`
174189
);
175190

176191
if (process.env.TERM_PROGRAM.match('iTerm')) {
177192
console.log(`
178193
iTerm2 should display the image below. 😊
179194
180-
${await terminalImage.file(downloadedFile)}`
195+
${await terminalImage.file(savedAs)}`
181196
);
182197
}
183198
}

0 commit comments

Comments
 (0)