11#!/usr/bin/env node
22"use strict" ;
3+ const { parse, resolve } = require ( "path" ) ;
34
45const sharp = require ( "sharp" ) ;
56const execa = require ( "execa" ) ;
67const gifsicle = require ( "gifsicle" ) ;
78const updateNotifier = require ( "update-notifier" ) ;
89const meow = require ( "meow" ) ;
10+ const got = require ( "got" ) ;
11+ const cp = require ( "cp-file" ) ;
12+
13+ const { cloneGistPath, addAll, commitAll, push } = require ( "./git-util" ) ;
914
1015const CONTAINER_WIDTH = 727 ;
1116const CUT_WIDTH = 325 ;
@@ -26,7 +31,12 @@ const cli = meow(
2631 $ crop-github-images unicorn.png
2732` ,
2833 {
29- string : [ "_" ]
34+ flags : {
35+ "github-token" : {
36+ type : "string" ,
37+ alias : "t"
38+ }
39+ }
3040 }
3141) ;
3242
@@ -54,14 +64,15 @@ const getXY = index => {
5464
5565const cropImage = async path => {
5666 try {
57- const ext = path . split ( "." ) . reverse ( ) [ 0 ] ;
67+ const { ext } = parse ( path ) ;
5868 const image = sharp ( path ) ;
5969 const { width, height } = await image . metadata ( ) ;
6070 const resizeOpts =
6171 width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
6272 ? { width : CONTAINER_WIDTH , height : MINIMUM_HEIGHT , fit : "outside" }
6373 : { width : CONTAINER_WIDTH } ;
6474 const resized = image . clone ( ) . resize ( resizeOpts ) ;
75+ const files = [ ] ;
6576 for ( let i = 0 ; i < 6 ; i ++ ) {
6677 const filename = `${ i } .${ ext } ` ;
6778 const { x, y } = getXY ( i ) ;
@@ -70,37 +81,88 @@ const cropImage = async path => {
7081 . extract ( { left : x , top : y , width : CUT_WIDTH , height : CUT_HEIGHT } )
7182 . toFile ( filename ) ;
7283 console . log ( `Successfully cropped ${ filename } .` ) ;
84+ files . push ( filename ) ;
7385 }
86+ return files ;
7487 } catch ( e ) {
7588 console . error ( e ) ;
7689 }
7790} ;
7891
7992const cropGif = async path => {
80- const { width, height } = await sharp ( path ) . metadata ( ) ;
81- const resizeOpts =
82- width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
83- ? [ "--resize" , "_x513" ]
84- : [ "--resize" , "727x_" ] ;
85- const resized = "resized.gif" ;
86- await execa ( gifsicle , [ ...resizeOpts , "-o" , resized , path ] ) ;
87- for ( let i = 0 ; i < 6 ; i ++ ) {
88- const filename = `${ i } .gif` ;
89- const { x, y } = getXY ( i ) ;
90- await execa ( gifsicle , [
91- "--crop" ,
92- `${ x } ,${ y } +${ CUT_WIDTH } x${ CUT_HEIGHT } ` ,
93- "-o" ,
94- filename ,
95- resized
96- ] ) ;
97- console . log ( `Successfully cropped ${ filename } .` ) ;
93+ try {
94+ const { width, height } = await sharp ( path ) . metadata ( ) ;
95+ const resizeOpts =
96+ width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
97+ ? [ "--resize" , "_x513" ]
98+ : [ "--resize" , "727x_" ] ;
99+ const resized = "resized.gif" ;
100+ await execa ( gifsicle , [ ...resizeOpts , "-o" , resized , path ] ) ;
101+ const files = [ ] ;
102+ for ( let i = 0 ; i < 6 ; i ++ ) {
103+ const filename = `${ i } .gif` ;
104+ const { x, y } = getXY ( i ) ;
105+ await execa ( gifsicle , [
106+ "--crop" ,
107+ `${ x } ,${ y } +${ CUT_WIDTH } x${ CUT_HEIGHT } ` ,
108+ "-o" ,
109+ filename ,
110+ resized
111+ ] ) ;
112+ console . log ( `Successfully cropped ${ filename } .` ) ;
113+ files . push ( filename ) ;
114+ }
115+ return files ;
116+ } catch ( e ) {
117+ console . error ( e ) ;
118+ }
119+ } ;
120+
121+ const crop = async ( path , githubToken ) => {
122+ let files = [ ] ;
123+ const isGif = path . endsWith ( ".gif" ) ;
124+ if ( isGif ) {
125+ files = await cropGif ( path ) ;
126+ } else {
127+ files = await cropImage ( path ) ;
128+ }
129+ if ( githubToken ) {
130+ try {
131+ for ( const file of files ) {
132+ const files = {
133+ [ file ] : {
134+ content : "Hello"
135+ }
136+ } ;
137+ const body = JSON . stringify ( {
138+ description : `Gist for ${ file } ` ,
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 data = JSON . parse ( res ) ;
150+ console . log ( `Gist for ${ file } has been created in ${ data . html_url } ` ) ;
151+ await cloneGistPath ( data . id ) ;
152+ await cp ( file , `${ data . id } /${ file } ` ) ;
153+ const gitPath = resolve ( `./${ data . id } /.git` ) ;
154+ await addAll ( gitPath ) ;
155+ await commitAll ( gitPath ) ;
156+ await push ( gitPath ) ;
157+ console . log ( `${ isGif ? "GIF" : "Image" } has been added to the gist` ) ;
158+ }
159+ } catch ( e ) {
160+ console . error ( e ) ;
161+ }
98162 }
99163} ;
100164
101165const path = cli . input [ 0 ] ;
102- if ( path . endsWith ( ".gif" ) ) {
103- cropGif ( path ) ;
104- } else {
105- cropImage ( path ) ;
106- }
166+ const { githubToken } = cli . flags ;
167+
168+ crop ( path , githubToken ) ;
0 commit comments