@@ -8,10 +8,7 @@ import { getListTable, mapToVisiblityCode, tokenGuard } from './util.js';
8
8
9
9
export async function loginUser ( argv , apiToken ) {
10
10
if ( tokenGuard ( apiToken ) ) {
11
- console . log (
12
- 'Please provide your pastebin.com API token in the ~/.pasty.api file.'
13
- ) ;
14
- process . exit ( 1 ) ;
11
+ return 'Please provide your pastebin.com API token in the ~/.pasty.api file.' ;
15
12
}
16
13
17
14
const username = argv . username ;
@@ -34,27 +31,24 @@ export async function loginUser(argv, apiToken) {
34
31
method : 'POST' ,
35
32
} ) ;
36
33
34
+ // text is either the user token or an error from api
35
+ const text = await response . text ( ) ;
36
+
37
37
if ( response . status === 200 ) {
38
- const userToken = await response . text ( ) ;
39
- writeFileSync ( `${ homedir ( ) } /.pasty.user` , userToken , 'utf-8' ) ;
40
- console . log ( `Success! You're now logged in as ${ username } ` ) ;
38
+ writeFileSync ( `${ homedir ( ) } /.pasty.user` , text , 'utf-8' ) ;
39
+ return `Success! You're now logged in as ${ username } ` ;
41
40
} else {
42
- console . log ( await response . text ( ) ) ;
43
- process . exit ( 1 ) ;
41
+ return `Error! ${ text } ` ;
44
42
}
45
43
}
46
44
47
45
export async function listPastes ( amount , apiToken , userToken ) {
48
46
if ( tokenGuard ( apiToken ) ) {
49
- console . log (
50
- 'Please provide your pastebin.com API token in the ~/.pasty.api file.'
51
- ) ;
52
- process . exit ( 1 ) ;
47
+ return 'Please provide your pastebin.com API token in the ~/.pasty.api file.' ;
53
48
}
54
49
55
50
if ( tokenGuard ( userToken ) ) {
56
- console . log ( 'Please login first via pasty login <username>' ) ;
57
- process . exit ( 1 ) ;
51
+ return 'Please login first via pasty login <username>' ;
58
52
}
59
53
60
54
const response = await fetch ( API_URLS . apiPost , {
@@ -69,18 +63,18 @@ export async function listPastes(amount, apiToken, userToken) {
69
63
if ( response . status === 200 ) {
70
64
const tableData = getListTable ( text ) ;
71
65
72
- console . log ( table ( tableData ) ) ;
66
+ return table ( tableData ) ;
73
67
} else {
74
- console . log ( `Error! ${ text } ` ) ;
68
+ return `Error! ${ text } ` ;
75
69
}
76
70
}
77
71
78
72
export function logout ( ) {
79
73
try {
80
74
rmSync ( `${ homedir ( ) } /.pasty.user` ) ;
81
- console . log ( 'Successfully logged you out.' ) ;
75
+ return 'Successfully logged you out.' ;
82
76
} catch ( e ) {
83
- console . log ( "You're currently not logged in" ) ;
77
+ return "You're currently not logged in" ;
84
78
}
85
79
}
86
80
@@ -95,21 +89,14 @@ export async function deletePaste(pasteId, apiToken, userToken) {
95
89
96
90
const text = await response . text ( ) ;
97
91
98
- if ( response . status === 200 ) {
99
- console . log ( `Success! ${ text } ` ) ;
100
- } else {
101
- console . log ( `Error! ${ text } ` ) ;
102
- }
92
+ return response . status === 200 ? `Success! ${ text } ` : `Error! ${ text } ` ;
103
93
}
104
94
105
95
export async function createPaste ( argv , apiToken , userToken ) {
106
96
const { file, string, format, visibility, expiry, folder, name } = argv ;
107
97
108
98
if ( format && ! FORMAT_CHOICES . includes ( format ) ) {
109
- console . log (
110
- 'Error! Format option is not supported by pastebin. See https://pastebin.com/doc_api#8 for supported formats'
111
- ) ;
112
- process . exit ( 1 ) ;
99
+ return 'Error! Format option is not supported by pastebin. See https://pastebin.com/doc_api#8 for supported formats' ;
113
100
}
114
101
115
102
const mappedVisibility = mapToVisiblityCode ( visibility ) ;
@@ -125,9 +112,5 @@ export async function createPaste(argv, apiToken, userToken) {
125
112
126
113
const text = await response . text ( ) ;
127
114
128
- if ( response . status === 200 ) {
129
- console . log ( text ) ;
130
- } else {
131
- console . log ( `Error! ${ text } ` ) ;
132
- }
115
+ return response . status === 200 ? `Succes! ${ text } ` : `Error! ${ text } ` ;
133
116
}
0 commit comments