From 8ee6fdfe76534c802fc4f32ebf221b9d3e2981bf Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 12 Sep 2021 14:02:46 +0530 Subject: [PATCH] fmt --- package.json | 2 +- src/gists/create.ts | 126 ++++++++++++++++++++++++++------------------ src/gists/gists.ts | 6 +-- 3 files changed, 78 insertions(+), 56 deletions(-) diff --git a/package.json b/package.json index b58aafa..d742bbd 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start": "ts-node index.ts . --ignore", "lint": "npx prettier . -w", "build": "tsc index.ts --esModuleInterop true --allowJs true --outDir ./dist", - "build-exe" : "pkg ./dist/index.js" + "build-exe": "pkg ./dist/index.js" }, "repository": { "type": "git", diff --git a/src/gists/create.ts b/src/gists/create.ts index ddd9bcc..fce9086 100644 --- a/src/gists/create.ts +++ b/src/gists/create.ts @@ -1,57 +1,79 @@ -import { Octokit } from '@octokit/core' -import { greenBright, redBright, yellowBright } from 'chalk'; -import { readdirSync, readFileSync, statSync } from 'fs'; -import inquirer from 'inquirer'; -import { join } from 'path'; -import { cwd } from 'process'; -import { clearPreviousLine } from '../utils'; +import { Octokit } from "@octokit/core"; +import { greenBright, redBright, yellowBright } from "chalk"; +import { readdirSync, readFileSync, statSync } from "fs"; +import inquirer from "inquirer"; +import { join } from "path"; +import { cwd } from "process"; +import { clearPreviousLine } from "../utils"; export class CreateGist { - private client:Octokit + private client: Octokit; - /** - * @constructor - * @param {Octokit} client The client object - */ - constructor(client:Octokit) { - this.client = client; - this.collectFiles() - } + /** + * @constructor + * @param {Octokit} client The client object + */ + constructor(client: Octokit) { + this.client = client; + this.collectFiles(); + } - private collectFiles():void { - const directoryContent:Array = readdirSync(cwd()).filter((filename:string):boolean => { - return statSync(join(cwd(), filename)).isFile() - }) - for(let index=0; index { - const filenames:Array = response.inputFiles.split(",").map((currentFile:string):string => { - return currentFile.trim() - }) - const selectedFiles:Array = filenames.map((fileIndex:string):string | undefined => { - const fileIndexNumber:number = parseInt(fileIndex) - return directoryContent[fileIndexNumber-1] - }).filter((value:string | undefined):boolean => { - return value != undefined - }) - let fileContent:any = {} - for(let selectedFileIndex=0; selectedFileIndex { - clearPreviousLine() - console.log(greenBright("Published")) - }).catch((error):void => { - console.log(redBright(error)) - }) - }) + private collectFiles(): void { + const directoryContent: Array = readdirSync(cwd()).filter( + (filename: string): boolean => { + return statSync(join(cwd(), filename)).isFile(); + } + ); + for (let index = 0; index < directoryContent.length; index++) { + const currentFilename: string = directoryContent[index]; + console.log( + `${yellowBright(index + 1 + ":")} ${greenBright(currentFilename)}` + ); } -} \ No newline at end of file + inquirer + .prompt({ + type: "input", + message: "Enter the file indexes(seperated by comma)", + name: "inputFiles", + }) + .then((response: { inputFiles: any }): void => { + const filenames: Array = response.inputFiles + .split(",") + .map((currentFile: string): string => { + return currentFile.trim(); + }); + const selectedFiles: Array = filenames + .map((fileIndex: string): string | undefined => { + const fileIndexNumber: number = parseInt(fileIndex); + return directoryContent[fileIndexNumber - 1]; + }) + .filter((value: string | undefined): boolean => { + return value != undefined; + }); + let fileContent: any = {}; + for ( + let selectedFileIndex = 0; + selectedFileIndex < selectedFiles.length; + selectedFileIndex++ + ) { + const currentSelectedFile = join( + cwd(), + selectedFiles[selectedFileIndex] + ); + fileContent[selectedFiles[selectedFileIndex]] = { + content: readFileSync(currentSelectedFile).toString(), + }; + } + console.log(yellowBright("Publishing the gist...")); + this.client + .request("POST /gists", { files: fileContent }) + .then((response: any): void => { + clearPreviousLine(); + console.log(greenBright("Published")); + }) + .catch((error): void => { + console.log(redBright(error)); + }); + }); + } +} diff --git a/src/gists/gists.ts b/src/gists/gists.ts index b8258ce..3d48296 100644 --- a/src/gists/gists.ts +++ b/src/gists/gists.ts @@ -24,9 +24,9 @@ export class GithubGist { if (command == "reset") { this.github.reset(); process.exit(); - } else if(command == "new"){ - new CreateGist(this.client) - }else { + } else if (command == "new") { + new CreateGist(this.client); + } else { this.github .authenticate(this.client) .then((value: any): void => {