Skip to content

Commit 1da0167

Browse files
committed
Proper cli setup and fixed bug
1 parent 0449ccf commit 1da0167

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

lib/main.js renamed to bin/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env node
2+
13
const yargs = require('yargs');
24
const { hideBin } = require('yargs/helpers');
35
const { readFileSync } = require('fs');
@@ -8,7 +10,7 @@ const {
810
listPastes,
911
loginUser,
1012
logout,
11-
} = require('./api-functions.js');
13+
} = require('../lib/api-functions.js');
1214

1315
let userToken;
1416
let apiToken;

lib/api-functions.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,17 @@ async function createPaste(argv, apiToken, userToken) {
118118

119119
const { file, string, format, visibility, expiry, folder, name } = argv;
120120

121+
if (!file && !string) {
122+
return 'You need to supply either -f (--file) OR -s (--string)';
123+
}
124+
121125
if (!FORMAT_CHOICES.includes(format)) {
122126
return 'Error! Format option is not supported by pastebin. See https://pastebin.com/doc_api#8 for supported formats';
123127
}
124128

125129
const mappedVisibility = mapToVisiblityCode(visibility);
126130
const pasteText = file ? readFileSync(file, 'utf-8').trim() : string;
127131

128-
console.log(name, pasteText);
129-
130132
const response = await fetch(API_URLS.apiPost, {
131133
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}`,
132134
headers: {

package-lock.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
"directories": {
77
"lib": "lib"
88
},
9+
"bin": {
10+
"pasty": "bin/main.js"
11+
},
12+
"files": [
13+
"lib/",
14+
"bin/"
15+
],
916
"scripts": {
1017
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose --watch"
1118
},

tests/api-functions.spec.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('apiFunctions', () => {
123123
__setResponseText(responseText);
124124

125125
const apiRes = await createPaste(
126-
{ format: 'text' },
126+
{ format: 'text', string: 'string' },
127127
'api token',
128128
'user token'
129129
);
@@ -159,24 +159,38 @@ describe('apiFunctions', () => {
159159
__setStatusCode(500);
160160

161161
const apiRes = await createPaste(
162-
{ format: 'text' },
162+
{ format: 'text', string: 'string' },
163163
'api token',
164164
'user token'
165165
);
166166

167167
expect(apiRes).toBe(`Error! ${responseText}`);
168168
});
169169

170-
it('should return error if fomit is not supported by pastebin', async () => {
170+
it('should return error if format is not supported by pastebin', async () => {
171+
const resonseText =
172+
'Error! Format option is not supported by pastebin. See https://pastebin.com/doc_api#8 for supported formats';
173+
171174
const apiRes = await createPaste(
172-
{ format: 'does not exist' },
175+
{ format: 'does not exist', string: 'string' },
173176
'api token',
174177
'user token'
175178
);
176179

177-
expect(apiRes).toBe(
178-
'Error! Format option is not supported by pastebin. See https://pastebin.com/doc_api#8 for supported formats'
180+
expect(apiRes).toBe(resonseText);
181+
});
182+
183+
it('should return error if neither file or string are supplied', async () => {
184+
const responseText =
185+
'You need to supply either -f (--file) OR -s (--string)';
186+
187+
const apiRes = await createPaste(
188+
{ format: 'text' },
189+
'api token',
190+
'user token'
179191
);
192+
193+
expect(apiRes).toBe(responseText);
180194
});
181195
});
182196

0 commit comments

Comments
 (0)