Skip to content

Commit 5db8dcc

Browse files
committed
Small refactoring
1 parent 62dc2f9 commit 5db8dcc

File tree

2 files changed

+33
-45
lines changed

2 files changed

+33
-45
lines changed

lib/api-functions.js

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import { getListTable, mapToVisiblityCode, tokenGuard } from './util.js';
88

99
export async function loginUser(argv, apiToken) {
1010
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.';
1512
}
1613

1714
const username = argv.username;
@@ -34,27 +31,24 @@ export async function loginUser(argv, apiToken) {
3431
method: 'POST',
3532
});
3633

34+
// text is either the user token or an error from api
35+
const text = await response.text();
36+
3737
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}`;
4140
} else {
42-
console.log(await response.text());
43-
process.exit(1);
41+
return `Error! ${text}`;
4442
}
4543
}
4644

4745
export async function listPastes(amount, apiToken, userToken) {
4846
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.';
5348
}
5449

5550
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>';
5852
}
5953

6054
const response = await fetch(API_URLS.apiPost, {
@@ -69,18 +63,18 @@ export async function listPastes(amount, apiToken, userToken) {
6963
if (response.status === 200) {
7064
const tableData = getListTable(text);
7165

72-
console.log(table(tableData));
66+
return table(tableData);
7367
} else {
74-
console.log(`Error! ${text}`);
68+
return `Error! ${text}`;
7569
}
7670
}
7771

7872
export function logout() {
7973
try {
8074
rmSync(`${homedir()}/.pasty.user`);
81-
console.log('Successfully logged you out.');
75+
return 'Successfully logged you out.';
8276
} catch (e) {
83-
console.log("You're currently not logged in");
77+
return "You're currently not logged in";
8478
}
8579
}
8680

@@ -95,21 +89,14 @@ export async function deletePaste(pasteId, apiToken, userToken) {
9589

9690
const text = await response.text();
9791

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}`;
10393
}
10494

10595
export async function createPaste(argv, apiToken, userToken) {
10696
const { file, string, format, visibility, expiry, folder, name } = argv;
10797

10898
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';
113100
}
114101

115102
const mappedVisibility = mapToVisiblityCode(visibility);
@@ -125,9 +112,5 @@ export async function createPaste(argv, apiToken, userToken) {
125112

126113
const text = await response.text();
127114

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}`;
133116
}

lib/main.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ yargs(hideBin(process.argv))
3636
describe: 'your username',
3737
});
3838
},
39-
(argv) => {
40-
loginUser(argv, apiToken);
39+
async (argv) => {
40+
const response = await loginUser(argv, apiToken);
41+
console.log(response);
4142
}
4243
)
4344
.command(
@@ -49,8 +50,9 @@ yargs(hideBin(process.argv))
4950
describe: 'the amount of pastes to be displayed',
5051
});
5152
},
52-
(argv) => {
53-
listPastes(argv.amount, apiToken, userToken);
53+
async (argv) => {
54+
const response = await listPastes(argv.amount, apiToken, userToken);
55+
console.log(response);
5456
}
5557
)
5658
.command(
@@ -61,13 +63,14 @@ yargs(hideBin(process.argv))
6163
describe: 'the id of the paste you want to delete',
6264
});
6365
},
64-
(argv) => {
65-
deletePaste(argv.pasteid, apiToken, userToken);
66+
async (argv) => {
67+
const response = await deletePaste(argv.pasteid, apiToken, userToken);
68+
console.log(response);
6669
}
6770
)
6871
.command(
69-
'post',
70-
'post a paste. either from a file or a string',
72+
'create',
73+
'create a paste. either from a file or a string',
7174
(yargs) => {
7275
return yargs
7376
.option('f', {
@@ -106,16 +109,18 @@ yargs(hideBin(process.argv))
106109
})
107110
.conflicts('f', 's');
108111
},
109-
(argv) => {
110-
createPaste(argv, apiToken, userToken);
112+
async (argv) => {
113+
const response = await createPaste(argv, apiToken, userToken);
114+
console.log(response);
111115
}
112116
)
113117
.command(
114118
'logout',
115119
'logs you out',
116120
() => {},
117-
() => {
118-
logout();
121+
async () => {
122+
const response = await logout();
123+
console.log(response);
119124
}
120125
)
121126
.demandCommand(1)

0 commit comments

Comments
 (0)