redeem-cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +25 -0
- package/readme.md +5 -0
- package/src/main.js +172 -0
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "redeem-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Saves and Opens your favourite websites",
|
|
5
|
+
"files": [
|
|
6
|
+
"src/main.js"
|
|
7
|
+
],
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node src/main.js"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"bin": {
|
|
17
|
+
"favs": "src/main.js"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"better-sqlite3": "^12.8.0",
|
|
21
|
+
"dotenv": "^17.4.1",
|
|
22
|
+
"open": "^11.0.0",
|
|
23
|
+
"sqlite3": "^6.0.1"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/readme.md
ADDED
package/src/main.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {exec} from 'child_process';
|
|
3
|
+
import open, { apps } from 'open';
|
|
4
|
+
import dotenv from 'dotenv';
|
|
5
|
+
import Database from 'better-sqlite3';
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
dotenv.config();
|
|
11
|
+
|
|
12
|
+
const args = process.argv.slice(2);
|
|
13
|
+
const command = args[0];
|
|
14
|
+
const favorite =args[1];
|
|
15
|
+
const url =args[2];
|
|
16
|
+
|
|
17
|
+
let db;
|
|
18
|
+
const dbPath='favorites.db';
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
function init(){
|
|
22
|
+
console.log('Initializing database...');
|
|
23
|
+
db=new Database(dbPath);
|
|
24
|
+
|
|
25
|
+
const createTable=`
|
|
26
|
+
CREATE TABLE IF NOT EXISTS favorites (
|
|
27
|
+
id INTEGER PRIMARY KEY,
|
|
28
|
+
name TEXT NOT NULL,
|
|
29
|
+
url TEXT NOT NULL
|
|
30
|
+
)
|
|
31
|
+
`;
|
|
32
|
+
db.exec(createTable);
|
|
33
|
+
|
|
34
|
+
const data =[
|
|
35
|
+
{name:'goog', url:'https://google.com'},
|
|
36
|
+
{name:'social', url:'https://twitter.com'},
|
|
37
|
+
{name:'code', url:'https://leetcode.com'},
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
const insertData = db.prepare(`
|
|
41
|
+
INSERT OR IGNORE INTO favorites (name, url) VALUES (?, ?)
|
|
42
|
+
`);
|
|
43
|
+
|
|
44
|
+
data.forEach((favorite) => {
|
|
45
|
+
insertData.run(favorite.name, favorite.url);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function checkBrowser(){
|
|
50
|
+
const browser = process.env.BROWSER;
|
|
51
|
+
if (!browser) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const browserLower = browser.toLocaleLowerCase();
|
|
55
|
+
|
|
56
|
+
switch(browserLower){
|
|
57
|
+
case 'chrome':
|
|
58
|
+
return 'chrome';
|
|
59
|
+
case 'firefox':
|
|
60
|
+
return 'firefox';
|
|
61
|
+
case 'edge':
|
|
62
|
+
return 'edge';
|
|
63
|
+
default:
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
function displayMenu(){
|
|
71
|
+
console.log('open <favourite> : Open a saved favourite.');
|
|
72
|
+
console.log('ls : List all saved favourites.');
|
|
73
|
+
console.log('add <favourite> <url> : Add a new favourite.');
|
|
74
|
+
console.log('rm <favorite> : Remove a saved favorite.');
|
|
75
|
+
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function openFav(favorite){
|
|
79
|
+
const row=db.prepare('SELECT * FROM favorites WHERE name = ?').get(favorite);
|
|
80
|
+
|
|
81
|
+
if (!row) {
|
|
82
|
+
console.log('favorite', favorite, 'does not exist.');
|
|
83
|
+
process.exit(1);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const url = row.url;
|
|
88
|
+
console.log('opening', url);
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
const appName = checkBrowser();
|
|
92
|
+
if(appName){
|
|
93
|
+
await open(url, {app: appName});
|
|
94
|
+
} else {
|
|
95
|
+
await open(url);
|
|
96
|
+
}
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.error('Error opening URL:', error.message);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
function add(favorite, url){
|
|
105
|
+
|
|
106
|
+
db.prepare('INSERT INTO favorites (name, url) VALUES (?, ?)').run(
|
|
107
|
+
favorite,
|
|
108
|
+
url
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
console.log('Adding', favorite, 'with URL', url);
|
|
112
|
+
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function rm(favorite){
|
|
116
|
+
db.prepare('DELETE FROM favorites WHERE name = ?').run(favorite);
|
|
117
|
+
console.log('Removing', favorite);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function ls(){
|
|
121
|
+
const favorite=db.prepare('SELECT * FROM favorites').all();
|
|
122
|
+
console.log('All favorites');
|
|
123
|
+
favorite.forEach((favorite)=>{
|
|
124
|
+
console.log(`${favorite.name}: ${favorite.url}`);
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
if(!fs.existsSync('favorites.db')){
|
|
131
|
+
init();
|
|
132
|
+
|
|
133
|
+
}else{
|
|
134
|
+
db=new Database(dbPath);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
(async () => {
|
|
139
|
+
if(!command||command === 'help'){
|
|
140
|
+
displayMenu();
|
|
141
|
+
|
|
142
|
+
}else{
|
|
143
|
+
switch(command){
|
|
144
|
+
case 'ls':
|
|
145
|
+
ls();
|
|
146
|
+
break;
|
|
147
|
+
case 'open':
|
|
148
|
+
if(!favorite){
|
|
149
|
+
displayMenu();
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
await openFav(favorite);
|
|
153
|
+
break;
|
|
154
|
+
case 'add':
|
|
155
|
+
if(!favorite ||!url){
|
|
156
|
+
displayMenu();
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
add(favorite, url);
|
|
160
|
+
break;
|
|
161
|
+
case 'rm':
|
|
162
|
+
if(!favorite){
|
|
163
|
+
displayMenu();
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
rm(favorite);
|
|
167
|
+
break;
|
|
168
|
+
default:
|
|
169
|
+
console.log('Invalid command. Use --help for usage information.');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
})();
|