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
49const meow = require ( 'meow' ) ;
510const chalk = require ( 'chalk' ) ;
611const opn = require ( 'opn' ) ;
712const queryString = require ( 'query-string' ) ;
813const terminalImage = require ( 'terminal-image' ) ;
14+ const generate = require ( 'nanoid/generate' ) ;
915const 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