Skip to content

Commit 51e443e

Browse files
committed
feat: show command to open paste in default browser
1 parent dcf6c90 commit 51e443e

File tree

6 files changed

+808
-385
lines changed

6 files changed

+808
-385
lines changed

bin/main.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#!/usr/bin/env node
22

3-
const yargs = require('yargs');
4-
const { hideBin } = require('yargs/helpers');
5-
const { readFileSync } = require('fs');
6-
const { homedir } = require('os');
7-
const {
3+
import yargs from 'yargs';
4+
import { hideBin } from 'yargs/helpers';
5+
import { readFileSync }from 'fs';
6+
import { homedir } from 'os';
7+
import {
88
createPaste,
99
deletePaste,
1010
listPastes,
1111
loginUser,
1212
logout,
1313
getUserInfo,
14-
} = require('../lib/api-functions.js');
14+
} from '../lib/api-functions.js';
15+
import { openInBrowser } from '../lib/util.js';
1516

1617
let userToken;
1718
let apiToken;
@@ -144,5 +145,17 @@ yargs(hideBin(process.argv))
144145
console.log(await getUserInfo(apiToken, userToken));
145146
}
146147
)
148+
.command(
149+
'show <paste id>',
150+
'opens specified pate in your default browser',
151+
(yargs) => {
152+
return yargs.positional('pasteid', {
153+
describe: 'id of the paste you want to open in browser'
154+
});
155+
},
156+
async (argv) => {
157+
await openInBrowser(argv.pasteid);
158+
}
159+
)
147160
.demandCommand(1)
148161
.parse();

lib/api-functions.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
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 {
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, existsSync } from 'fs';
5+
import { homedir } from 'os';
6+
import { table } from 'table';
7+
import {
88
getListTable,
99
mapToVisiblityCode,
1010
tokenGuard,
1111
getUserData,
12-
} = require('./util.js');
13-
14-
module.exports = {
15-
loginUser,
16-
listPastes,
17-
logout,
18-
deletePaste,
19-
createPaste,
20-
getUserInfo,
21-
};
22-
23-
async function loginUser(argv, apiToken) {
12+
} from './util.js';
13+
14+
export async function loginUser(argv, apiToken) {
2415
if (tokenGuard(apiToken)) {
2516
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
2617
}
@@ -56,7 +47,7 @@ async function loginUser(argv, apiToken) {
5647
}
5748
}
5849

59-
async function listPastes(amount, apiToken, userToken) {
50+
export async function listPastes(amount, apiToken, userToken) {
6051
if (tokenGuard(apiToken)) {
6152
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
6253
}
@@ -83,15 +74,15 @@ async function listPastes(amount, apiToken, userToken) {
8374
}
8475
}
8576

86-
function logout() {
77+
export function logout() {
8778
if (existsSync(`${homedir}/.pasty.user`)) {
8879
rmSync(`${homedir()}/.pasty.user`);
8980
return 'Successfully logged you out.';
9081
}
9182
return "You're currently not logged in (could not find ~/.pasty.user)";
9283
}
9384

94-
async function deletePaste(pasteId, apiToken, userToken) {
85+
export async function deletePaste(pasteId, apiToken, userToken) {
9586
if (tokenGuard(apiToken)) {
9687
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
9788
}
@@ -113,7 +104,7 @@ async function deletePaste(pasteId, apiToken, userToken) {
113104
return response.status === 200 ? `Success! ${text}` : `Error! ${text}`;
114105
}
115106

116-
async function createPaste(argv, apiToken, userToken) {
107+
export async function createPaste(argv, apiToken, userToken) {
117108
if (tokenGuard(apiToken)) {
118109
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
119110
}
@@ -152,7 +143,7 @@ async function createPaste(argv, apiToken, userToken) {
152143
return response.status === 200 ? `Success! ${text}` : `Error! ${text}`;
153144
}
154145

155-
async function getUserInfo(apiToken, userToken) {
146+
export async function getUserInfo(apiToken, userToken) {
156147
if (tokenGuard(apiToken)) {
157148
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
158149
}

0 commit comments

Comments
 (0)