Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
lainq committed Sep 12, 2021
1 parent dd64333 commit 8ee6fdf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
126 changes: 74 additions & 52 deletions src/gists/create.ts
Original file line number Diff line number Diff line change
@@ -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<string> = 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)}`)
}
inquirer.prompt({
type: "input",
message: "Enter the file indexes(seperated by comma)",
name: "inputFiles"
}).then((response:{inputFiles:any}):void => {
const filenames:Array<string> = response.inputFiles.split(",").map((currentFile:string):string => {
return currentFile.trim()
})
const selectedFiles:Array<string> = 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))
})
})
private collectFiles(): void {
const directoryContent: Array<string> = 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)}`
);
}
}
inquirer
.prompt({
type: "input",
message: "Enter the file indexes(seperated by comma)",
name: "inputFiles",
})
.then((response: { inputFiles: any }): void => {
const filenames: Array<string> = response.inputFiles
.split(",")
.map((currentFile: string): string => {
return currentFile.trim();
});
const selectedFiles: Array<string> = 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));
});
});
}
}
6 changes: 3 additions & 3 deletions src/gists/gists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 8ee6fdf

Please sign in to comment.