1
- import inquirer from 'inquirer' ;
2
- import fetch from 'node-fetch' ;
3
- import { API_URLS , FORMAT_CHOICES } from './constant.js' ;
4
- import { readFileSync , rmSync , writeFileSync } from 'fs' ;
5
- import { homedir } from 'os' ;
6
- import { table } from 'table' ;
7
- import { getListTable , mapToVisiblityCode , tokenGuard } from './util.js' ;
8
-
9
- export async function loginUser ( argv , apiToken ) {
1
+ const inquirer = require ( 'inquirer' ) ;
2
+ const fetch = require ( 'node-fetch' ) ;
3
+ const { API_URLS , FORMAT_CHOICES } = require ( './constant.js' ) ;
4
+ const { readFileSync, rmSync, writeFileSync, existsSync } = require ( 'fs' ) ;
5
+ const { homedir } = require ( 'os' ) ;
6
+ const { table } = require ( 'table' ) ;
7
+ const { getListTable, mapToVisiblityCode, tokenGuard } = require ( './util.js' ) ;
8
+
9
+ module . exports = {
10
+ loginUser,
11
+ listPastes,
12
+ logout,
13
+ deletePaste,
14
+ createPaste,
15
+ } ;
16
+
17
+ async function loginUser ( argv , apiToken ) {
10
18
if ( tokenGuard ( apiToken ) ) {
11
19
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.' ;
12
20
}
@@ -42,7 +50,7 @@ export async function loginUser(argv, apiToken) {
42
50
}
43
51
}
44
52
45
- export async function listPastes ( amount , apiToken , userToken ) {
53
+ async function listPastes ( amount , apiToken , userToken ) {
46
54
if ( tokenGuard ( apiToken ) ) {
47
55
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.' ;
48
56
}
@@ -69,16 +77,23 @@ export async function listPastes(amount, apiToken, userToken) {
69
77
}
70
78
}
71
79
72
- export function logout ( ) {
73
- try {
80
+ function logout ( ) {
81
+ if ( existsSync ( ` ${ homedir } /.pasty.user` ) ) {
74
82
rmSync ( `${ homedir ( ) } /.pasty.user` ) ;
75
83
return 'Successfully logged you out.' ;
76
- } catch ( e ) {
77
- return "You're currently not logged in" ;
78
84
}
85
+ return "You're currently not logged in (could not find ~/.pasty.user)" ;
79
86
}
80
87
81
- export async function deletePaste ( pasteId , apiToken , userToken ) {
88
+ async function deletePaste ( pasteId , apiToken , userToken ) {
89
+ if ( tokenGuard ( apiToken ) ) {
90
+ return 'Please provide your pastebin.com API token in the ~/.pasty.api file.' ;
91
+ }
92
+
93
+ if ( tokenGuard ( userToken ) ) {
94
+ return 'Please login first via pasty login <username>' ;
95
+ }
96
+
82
97
const response = await fetch ( API_URLS . apiPost , {
83
98
body : `api_dev_key=${ apiToken } &api_user_key=${ userToken } &api_option=delete&api_paste_key=${ pasteId } ` ,
84
99
headers : {
@@ -92,16 +107,26 @@ export async function deletePaste(pasteId, apiToken, userToken) {
92
107
return response . status === 200 ? `Success! ${ text } ` : `Error! ${ text } ` ;
93
108
}
94
109
95
- export async function createPaste ( argv , apiToken , userToken ) {
110
+ async function createPaste ( argv , apiToken , userToken ) {
111
+ if ( tokenGuard ( apiToken ) ) {
112
+ return 'Please provide your pastebin.com API token in the ~/.pasty.api file.' ;
113
+ }
114
+
115
+ if ( tokenGuard ( userToken ) ) {
116
+ return 'Please login first via pasty login <username>' ;
117
+ }
118
+
96
119
const { file, string, format, visibility, expiry, folder, name } = argv ;
97
120
98
- if ( format && ! FORMAT_CHOICES . includes ( format ) ) {
121
+ if ( ! FORMAT_CHOICES . includes ( format ) ) {
99
122
return 'Error! Format option is not supported by pastebin. See https://pastebin.com/doc_api#8 for supported formats' ;
100
123
}
101
124
102
125
const mappedVisibility = mapToVisiblityCode ( visibility ) ;
103
126
const pasteText = file ? readFileSync ( file , 'utf-8' ) . trim ( ) : string ;
104
127
128
+ console . log ( name , pasteText ) ;
129
+
105
130
const response = await fetch ( API_URLS . apiPost , {
106
131
body : `api_dev_key=${ apiToken } &api_user_key=${ userToken } &api_option=paste&api_paste_code=${ pasteText } &api_paste_name=${ name } &api_paste_format=${ format } &api_paste_private=${ mappedVisibility } ` ,
107
132
headers : {
@@ -112,5 +137,5 @@ export async function createPaste(argv, apiToken, userToken) {
112
137
113
138
const text = await response . text ( ) ;
114
139
115
- return response . status === 200 ? `Succes ! ${ text } ` : `Error! ${ text } ` ;
140
+ return response . status === 200 ? `Success ! ${ text } ` : `Error! ${ text } ` ;
116
141
}
0 commit comments