my-basic-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 +15 -0
- package/src/app.js +146 -0
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "my-basic-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Basic cli for opening saved urls, file paths, or application .exe paths",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "OnyxEpoch",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node ./src/app.js"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"better-sqlite3": "^13.0.3"
|
|
14
|
+
}
|
|
15
|
+
}
|
package/src/app.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { exec } from "child_process";
|
|
3
|
+
import Database from "better-sqlite3";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
|
|
6
|
+
const args = process.argv.slice(2);
|
|
7
|
+
const command = args[0];
|
|
8
|
+
const favorite = args[1];
|
|
9
|
+
const url = args[2];
|
|
10
|
+
|
|
11
|
+
let db;
|
|
12
|
+
let dbPath = "favorites.db";
|
|
13
|
+
|
|
14
|
+
//---- Init Function ----//
|
|
15
|
+
function init() {
|
|
16
|
+
console.log("initializing database...");
|
|
17
|
+
db = new Database(dbPath);
|
|
18
|
+
|
|
19
|
+
const createTable = `
|
|
20
|
+
CREATE TABLE IF NOT EXISTS favorites(
|
|
21
|
+
id INTEGER PRIMARY KEY,
|
|
22
|
+
name TEXT NOT NULL,
|
|
23
|
+
url TEXT NOT NULL
|
|
24
|
+
)
|
|
25
|
+
`;
|
|
26
|
+
|
|
27
|
+
db.exec(createTable);
|
|
28
|
+
const data = [
|
|
29
|
+
{ name: "goog", url: "https://google.com" },
|
|
30
|
+
{ name: "social", url: "https://instagram.com" },
|
|
31
|
+
{ name: "news", url: "https://yahoo.com" },
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
const insertData = db.prepare(
|
|
35
|
+
"INSERT INTO favorites (name, url) VALUES (?, ?)",
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
data.forEach((favorite) => {
|
|
39
|
+
insertData.run(favorite.name, favorite.url);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//---- Display Usage Function ----//
|
|
44
|
+
function displayUsage() {
|
|
45
|
+
console.log(
|
|
46
|
+
"open <favorite> : Open a saved favorite with default app.",
|
|
47
|
+
);
|
|
48
|
+
console.log(
|
|
49
|
+
"add <favorite> <url/filePath> : Add a new favorite for some url or file path.",
|
|
50
|
+
);
|
|
51
|
+
console.log("rm <favorite> : Remove a saved favorite.");
|
|
52
|
+
console.log("ls : List all favorites.");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//---- Open Favorite Function ----//
|
|
56
|
+
function openFavorite(favorite) {
|
|
57
|
+
const row = db
|
|
58
|
+
.prepare("SELECT * FROM favorites WHERE name = ?")
|
|
59
|
+
.get(favorite);
|
|
60
|
+
|
|
61
|
+
if (!row) {
|
|
62
|
+
console.log("Favorite not found.");
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
const url = row.url;
|
|
66
|
+
|
|
67
|
+
let execCommand;
|
|
68
|
+
switch (process.platform) {
|
|
69
|
+
case "darwin":
|
|
70
|
+
execCommand = `open ${url}`;
|
|
71
|
+
break;
|
|
72
|
+
case "win32":
|
|
73
|
+
execCommand = `start ${url}`;
|
|
74
|
+
break;
|
|
75
|
+
case "linux":
|
|
76
|
+
execCommand = `xdg-open ${url}`;
|
|
77
|
+
break;
|
|
78
|
+
default:
|
|
79
|
+
console.log("Unsupported Platform");
|
|
80
|
+
}
|
|
81
|
+
exec(execCommand, (error, stdout, stderr) => {
|
|
82
|
+
if (error) {
|
|
83
|
+
console.log("error:", error.message);
|
|
84
|
+
}
|
|
85
|
+
if (stderr) {
|
|
86
|
+
console.log("error:", stderr);
|
|
87
|
+
}
|
|
88
|
+
if (error || stderr) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
console.log(stdout);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
//---- Add Function ----//
|
|
96
|
+
function add(favorite, url) {
|
|
97
|
+
db.prepare("INSERT INTO favorites (name, url) VALUES (?, ?)").run(
|
|
98
|
+
favorite,
|
|
99
|
+
url,
|
|
100
|
+
);
|
|
101
|
+
console.log("adding", favorite, url);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
//---- Remove Function ----//
|
|
105
|
+
function rm(favorite) {
|
|
106
|
+
db.prepare("DELETE FROM favorites WHERE name = ?").run(favorite);
|
|
107
|
+
console.log("removing", favorite);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
//---- LS Function ----//
|
|
111
|
+
function ls() {
|
|
112
|
+
const favorites = db.prepare("SELECT * FROM favorites").all();
|
|
113
|
+
console.log("All Favorites:");
|
|
114
|
+
favorites.forEach((favorite) => {
|
|
115
|
+
console.log(`${favorite.name}: ${favorite.url}\n`);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
//---- Main Code ----//
|
|
120
|
+
|
|
121
|
+
if (!fs.existsSync(dbPath)) {
|
|
122
|
+
init();
|
|
123
|
+
} else {
|
|
124
|
+
db = new Database(dbPath);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// check arguments
|
|
128
|
+
const argCount = args.length;
|
|
129
|
+
|
|
130
|
+
const commands = {
|
|
131
|
+
ls: { f: ls, argCount: 1 },
|
|
132
|
+
open: { f: openFavorite, argCount: 2 },
|
|
133
|
+
rm: { f: rm, argCount: 2 },
|
|
134
|
+
add: { f: add, argCount: 3 },
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
if (
|
|
138
|
+
argCount === 0 ||
|
|
139
|
+
!commands[command] ||
|
|
140
|
+
argCount < commands[command].argCount
|
|
141
|
+
) {
|
|
142
|
+
displayUsage();
|
|
143
|
+
process.exit(1);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
commands[command].f(favorite, url);
|