@@ -11,6 +11,7 @@ const updateNotifier = require("update-notifier");
1111const meow = require ( "meow" ) ;
1212const got = require ( "got" ) ;
1313const cp = require ( "cp-file" ) ;
14+ const trash = require ( "trash" ) ;
1415
1516const { cloneGistPath, commitAll, push } = require ( "./git-util" ) ;
1617
@@ -66,7 +67,7 @@ const getXY = index => {
6667
6768const cropImage = async path => {
6869 try {
69- const { ext } = parse ( path ) ;
70+ const { ext, name } = parse ( path ) ;
7071 const image = sharp ( path ) ;
7172 const { width, height } = await image . metadata ( ) ;
7273 const resizeOpts =
@@ -76,7 +77,7 @@ const cropImage = async path => {
7677 const resized = image . clone ( ) . resize ( resizeOpts ) ;
7778 const files = [ ] ;
7879 for ( let i = 0 ; i < 6 ; i ++ ) {
79- const filename = `${ i } . ${ ext } ` ;
80+ const filename = `${ name } . ${ i } ${ ext } ` ;
8081 const { x, y } = getXY ( i ) ;
8182 await resized
8283 . clone ( )
@@ -93,6 +94,7 @@ const cropImage = async path => {
9394
9495const cropGif = async path => {
9596 try {
97+ const { name } = parse ( path ) ;
9698 const { width, height } = await sharp ( path ) . metadata ( ) ;
9799 const resizeOpts =
98100 width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
@@ -102,7 +104,7 @@ const cropGif = async path => {
102104 await execa ( gifsicle , [ ...resizeOpts , "-o" , resized , path ] ) ;
103105 const files = [ ] ;
104106 for ( let i = 0 ; i < 6 ; i ++ ) {
105- const filename = `${ i } .gif` ;
107+ const filename = `${ name } . ${ i } .gif` ;
106108 const { x, y } = getXY ( i ) ;
107109 await execa ( gifsicle , [
108110 "--crop" ,
@@ -114,59 +116,80 @@ const cropGif = async path => {
114116 console . log ( `Successfully cropped ${ filename } .` ) ;
115117 files . push ( filename ) ;
116118 }
119+ await trash ( resized ) ;
117120 return files ;
118121 } catch ( e ) {
119122 console . error ( e ) ;
120123 }
121124} ;
122125
126+ const isGif = path => path . endsWith ( ".gif" ) ;
127+
128+ const createGists = async ( files , githubToken ) => {
129+ const hashes = [ ] ;
130+ try {
131+ for ( const file of files ) {
132+ const files = {
133+ [ file ] : {
134+ content : `Placeholder for ${ file } .`
135+ }
136+ } ;
137+ const body = JSON . stringify ( {
138+ description : `Gist for ${ file } . Generated by \`crop-github-images-cli\`.` ,
139+ public : true ,
140+ files
141+ } ) ;
142+ const { body : res } = await got . post ( "gists" , {
143+ baseUrl : "https://api.github.com" ,
144+ headers : {
145+ Authorization : `token ${ githubToken } `
146+ } ,
147+ body
148+ } ) ;
149+ const {
150+ html_url,
151+ id : hash ,
152+ owner : { login }
153+ } = JSON . parse ( res ) ;
154+ console . log ( `Prepared the gist for ${ file } in ${ html_url } ` ) ;
155+ await cloneGistPath ( hash ) ;
156+ await cp ( file , `${ hash } /${ file } ` ) ;
157+ const gitPath = resolve ( `./${ hash } /.git` ) ;
158+ await commitAll ( gitPath ) ;
159+ fs . writeFileSync (
160+ `${ hash } /.netrc` ,
161+ `machine github.com\nlogin ${ login } \npassword ${ githubToken } `
162+ ) ;
163+ await push ( gitPath ) ;
164+ hashes . push ( hash ) ;
165+ console . log (
166+ `${ isGif ( path ) ? "GIF" : "Image" } ${ file } has been added to the gist`
167+ ) ;
168+ }
169+ await trash ( hashes ) ;
170+ } catch ( e ) {
171+ console . error ( e ) ;
172+ if ( hashes . length ) {
173+ await trash ( hashes ) ;
174+ }
175+ }
176+ } ;
177+
123178const crop = async ( path , githubToken ) => {
124179 let files = [ ] ;
125- const isGif = path . endsWith ( ".gif" ) ;
126- if ( isGif ) {
127- files = await cropGif ( path ) ;
128- } else {
129- files = await cropImage ( path ) ;
130- }
131- if ( githubToken ) {
132- try {
133- for ( const file of files ) {
134- const files = {
135- [ file ] : {
136- content : "Hello"
137- }
138- } ;
139- const body = JSON . stringify ( {
140- description : `Gist for ${ file } ` ,
141- public : true ,
142- files
143- } ) ;
144- const { body : res } = await got . post ( "gists" , {
145- baseUrl : "https://api.github.com" ,
146- headers : {
147- Authorization : `token ${ githubToken } `
148- } ,
149- body
150- } ) ;
151- const data = JSON . parse ( res ) ;
152- console . log ( `Prepared the gist for ${ file } in ${ data . html_url } ` ) ;
153- await cloneGistPath ( data . id ) ;
154- await cp ( file , `${ data . id } /${ file } ` ) ;
155- const gitPath = resolve ( `./${ data . id } /.git` ) ;
156- await commitAll ( gitPath ) ;
157- fs . writeFileSync (
158- `${ data . id } /.netrc` ,
159- `machine github.com\nlogin ${
160- data . owner . login
161- } \npassword ${ githubToken } `
162- ) ;
163- await push ( gitPath ) ;
164- console . log (
165- `${ isGif ? "GIF" : "Image" } ${ file } has been added to the gist`
166- ) ;
167- }
168- } catch ( e ) {
169- console . error ( e ) ;
180+ try {
181+ if ( isGif ( path ) ) {
182+ files = await cropGif ( path ) ;
183+ } else {
184+ files = await cropImage ( path ) ;
185+ }
186+ if ( githubToken ) {
187+ createGists ( files , githubToken ) ;
188+ }
189+ } catch ( e ) {
190+ console . error ( e ) ;
191+ if ( files . length ) {
192+ await trash ( files ) ;
170193 }
171194 }
172195} ;
0 commit comments