1- import { Octokit } from ' @octokit/core'
2- import { greenBright , redBright , yellowBright } from ' chalk' ;
3- import { readdirSync , readFileSync , statSync } from 'fs' ;
4- import inquirer from ' inquirer' ;
5- import { join } from ' path' ;
6- import { cwd } from ' process' ;
7- import { clearPreviousLine } from ' ../utils' ;
1+ import { Octokit } from " @octokit/core" ;
2+ import { greenBright , redBright , yellowBright } from " chalk" ;
3+ import { readdirSync , readFileSync , statSync } from "fs" ;
4+ import inquirer from " inquirer" ;
5+ import { join } from " path" ;
6+ import { cwd } from " process" ;
7+ import { clearPreviousLine } from " ../utils" ;
88
99export class CreateGist {
10- private client :Octokit
10+ private client : Octokit ;
1111
12- /**
13- * @constructor
14- * @param {Octokit } client The client object
15- */
16- constructor ( client :Octokit ) {
17- this . client = client ;
18- this . collectFiles ( )
19- }
12+ /**
13+ * @constructor
14+ * @param {Octokit } client The client object
15+ */
16+ constructor ( client : Octokit ) {
17+ this . client = client ;
18+ this . collectFiles ( ) ;
19+ }
2020
21- private collectFiles ( ) :void {
22- const directoryContent :Array < string > = readdirSync ( cwd ( ) ) . filter ( ( filename :string ) :boolean => {
23- return statSync ( join ( cwd ( ) , filename ) ) . isFile ( )
24- } )
25- for ( let index = 0 ; index < directoryContent . length ; index ++ ) {
26- const currentFilename :string = directoryContent [ index ]
27- console . log ( `${ yellowBright ( ( index + 1 ) + ":" ) } ${ greenBright ( currentFilename ) } ` )
28- }
29- inquirer . prompt ( {
30- type : "input" ,
31- message : "Enter the file indexes(seperated by comma)" ,
32- name : "inputFiles"
33- } ) . then ( ( response :{ inputFiles :any } ) :void => {
34- const filenames :Array < string > = response . inputFiles . split ( "," ) . map ( ( currentFile :string ) :string => {
35- return currentFile . trim ( )
36- } )
37- const selectedFiles :Array < string > = filenames . map ( ( fileIndex :string ) :string | undefined => {
38- const fileIndexNumber :number = parseInt ( fileIndex )
39- return directoryContent [ fileIndexNumber - 1 ]
40- } ) . filter ( ( value :string | undefined ) :boolean => {
41- return value != undefined
42- } )
43- let fileContent :any = { }
44- for ( let selectedFileIndex = 0 ; selectedFileIndex < selectedFiles . length ; selectedFileIndex ++ ) {
45- const currentSelectedFile = join ( cwd ( ) , selectedFiles [ selectedFileIndex ] )
46- fileContent [ selectedFiles [ selectedFileIndex ] ] = { content :readFileSync ( currentSelectedFile ) . toString ( ) }
47- }
48- console . log ( yellowBright ( "Publishing the gist..." ) )
49- this . client . request ( "POST /gists" , { files : fileContent } ) . then ( ( response :any ) :void => {
50- clearPreviousLine ( )
51- console . log ( greenBright ( "Published" ) )
52- } ) . catch ( ( error ) :void => {
53- console . log ( redBright ( error ) )
54- } )
55- } )
21+ private collectFiles ( ) : void {
22+ const directoryContent : Array < string > = readdirSync ( cwd ( ) ) . filter (
23+ ( filename : string ) : boolean => {
24+ return statSync ( join ( cwd ( ) , filename ) ) . isFile ( ) ;
25+ }
26+ ) ;
27+ for ( let index = 0 ; index < directoryContent . length ; index ++ ) {
28+ const currentFilename : string = directoryContent [ index ] ;
29+ console . log (
30+ `${ yellowBright ( index + 1 + ":" ) } ${ greenBright ( currentFilename ) } `
31+ ) ;
5632 }
57- }
33+ inquirer
34+ . prompt ( {
35+ type : "input" ,
36+ message : "Enter the file indexes(seperated by comma)" ,
37+ name : "inputFiles" ,
38+ } )
39+ . then ( ( response : { inputFiles : any } ) : void => {
40+ const filenames : Array < string > = response . inputFiles
41+ . split ( "," )
42+ . map ( ( currentFile : string ) : string => {
43+ return currentFile . trim ( ) ;
44+ } ) ;
45+ const selectedFiles : Array < string > = filenames
46+ . map ( ( fileIndex : string ) : string | undefined => {
47+ const fileIndexNumber : number = parseInt ( fileIndex ) ;
48+ return directoryContent [ fileIndexNumber - 1 ] ;
49+ } )
50+ . filter ( ( value : string | undefined ) : boolean => {
51+ return value != undefined ;
52+ } ) ;
53+ let fileContent : any = { } ;
54+ for (
55+ let selectedFileIndex = 0 ;
56+ selectedFileIndex < selectedFiles . length ;
57+ selectedFileIndex ++
58+ ) {
59+ const currentSelectedFile = join (
60+ cwd ( ) ,
61+ selectedFiles [ selectedFileIndex ]
62+ ) ;
63+ fileContent [ selectedFiles [ selectedFileIndex ] ] = {
64+ content : readFileSync ( currentSelectedFile ) . toString ( ) ,
65+ } ;
66+ }
67+ console . log ( yellowBright ( "Publishing the gist..." ) ) ;
68+ this . client
69+ . request ( "POST /gists" , { files : fileContent } )
70+ . then ( ( response : any ) : void => {
71+ clearPreviousLine ( ) ;
72+ console . log ( greenBright ( "Published" ) ) ;
73+ } )
74+ . catch ( ( error ) : void => {
75+ console . log ( redBright ( error ) ) ;
76+ } ) ;
77+ } ) ;
78+ }
79+ }
0 commit comments