webide-cli 0.0.1-alpha.7 → 0.0.1-alpha.9
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/index.js +70 -42
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,6 +5,8 @@ const { createClient } = require('@supabase/supabase-js');
|
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
const path = require('path');
|
|
7
7
|
|
|
8
|
+
const termCols = process.stdout.columns;
|
|
9
|
+
|
|
8
10
|
const SUPABASE_URL = "https://dbmp-xbgmorqeur6oh81z.database.nocode.cn";
|
|
9
11
|
const SUPABASE_ANON_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlzcyI6InN1cGFiYXNlIiwiaWF0IjoxNzQ2OTc5MjAwLCJleHAiOjE5MDQ3NDU2MDB9.11QbQ5OW_m10vblDXAlw1Qq7Dve5Swzn12ILo7-9IXY";
|
|
10
12
|
const FILE_BUCKET = "jslib";
|
|
@@ -37,64 +39,75 @@ program
|
|
|
37
39
|
.command('add <name>')
|
|
38
40
|
.description('Add a new library.')
|
|
39
41
|
.action(async (libname) => {
|
|
40
|
-
fs.access(workdir + "/webapp.json", fs.constants.F_OK, (err) => {
|
|
42
|
+
fs.access(workdir + "/webapp.json", fs.constants.F_OK, async (err) => {
|
|
41
43
|
if (err) {
|
|
42
44
|
console.error("webapp.json not found in current directory. Please make sure you are in a WebIDE project directory.");
|
|
43
45
|
process.exit(114514);
|
|
44
46
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
47
|
+
fs.mkdirSync(path.join(workdir, 'libs', libname), { recursive: true });
|
|
48
|
+
try {
|
|
49
|
+
const load = loading(`Checking if library "${libname}" exists...`);
|
|
50
|
+
if (await checkFileExists(libname + "/lib.json")) {
|
|
51
|
+
load();
|
|
52
|
+
console.log(`Library "${libname}" already exists.`);
|
|
53
|
+
console.log(`Start downloading...`);
|
|
54
|
+
const downloadingTip = `Downloading library "${libname}" ...`;
|
|
55
|
+
let load2 = loading(downloadingTip);
|
|
56
|
+
const libJsonData = await getFile(libname + "/lib.json");
|
|
57
|
+
const libJson = JSON.parse(await libJsonData.text());
|
|
58
|
+
const { name, author, description, version } = libJson;
|
|
59
|
+
load2();
|
|
60
|
+
console.log(`Library info: `);
|
|
61
|
+
console.log(` Name: ${name}`);
|
|
62
|
+
console.log(` Version: ${version}`);
|
|
63
|
+
console.log(` Author: ${author || 'Unknown'}`);
|
|
64
|
+
console.log(` Description: ${description || 'No description'}`);
|
|
65
|
+
load2 = loading(downloadingTip);
|
|
66
|
+
await download(libname, path.join(workdir, 'libs', libname));
|
|
67
|
+
load2();
|
|
68
|
+
console.log(`Library "${libname}" downloaded successfully.`);
|
|
69
|
+
} else {
|
|
70
|
+
load();
|
|
71
|
+
console.error(`Library "${libname}" does not exist in the database.`);
|
|
72
|
+
process.exit(114514);
|
|
73
|
+
}
|
|
74
|
+
} catch (error) {
|
|
75
|
+
fs.rmSync(path.join(workdir, 'libs', libname), { recursive: true, force: true });
|
|
76
|
+
console.error("\r\nAn error occurred:", error.stack);
|
|
71
77
|
process.exit(114514);
|
|
72
78
|
}
|
|
73
|
-
}
|
|
74
|
-
fs.rmSync(path.join(workdir, 'libs', libname), { recursive: true, force: true });
|
|
75
|
-
console.error("\r\nAn error occurred:", error.stack);
|
|
76
|
-
process.exit(114514);
|
|
77
|
-
}
|
|
79
|
+
});
|
|
78
80
|
});
|
|
79
81
|
|
|
80
82
|
program
|
|
81
83
|
.command('remove <name>')
|
|
82
84
|
.description('Remove a library')
|
|
83
85
|
.action(function (libname) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
86
|
+
fs.access(workdir + "/webapp.json", fs.constants.F_OK, (err) => {
|
|
87
|
+
if (err) {
|
|
88
|
+
console.error("webapp.json not found in current directory. Please make sure you are in a WebIDE project directory.");
|
|
89
|
+
process.exit(114514);
|
|
90
|
+
}
|
|
91
|
+
const libPath = path.join(workdir, 'libs', libname);
|
|
92
|
+
if (fs.existsSync(libPath)) {
|
|
93
|
+
fs.rmSync(libPath, { recursive: true, force: true });
|
|
94
|
+
console.log(`Library "${libname}" removed.`);
|
|
95
|
+
} else {
|
|
96
|
+
console.error(`Library "${libname}" does not exist.`);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
91
99
|
});
|
|
92
100
|
|
|
101
|
+
function truncateMessage(msg, maxLength = termCols) {
|
|
102
|
+
if (msg.length <= maxLength) return msg;
|
|
103
|
+
return msg.substring(0, maxLength - 3) + '...';
|
|
104
|
+
}
|
|
105
|
+
|
|
93
106
|
program
|
|
94
107
|
.command('search <keyword>')
|
|
95
108
|
.description('Search for libraries')
|
|
96
109
|
.action(async function (keyword) {
|
|
97
|
-
const load = loading(`Searching for libraries with keyword "${keyword}" ...`);
|
|
110
|
+
const load = loading(`Searching for libraries with keyword "${truncateMessage(keyword)}" ...`);
|
|
98
111
|
const results = await search(keyword);
|
|
99
112
|
load();
|
|
100
113
|
console.log("\r\nSearch results(" + results.length + " items):");
|
|
@@ -118,14 +131,29 @@ async function getFile(path) {
|
|
|
118
131
|
|
|
119
132
|
function loading(msg) {
|
|
120
133
|
const spinnerChars = ['⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
|
|
134
|
+
const terminalWidth = process.stdout.columns;
|
|
121
135
|
let i = 0;
|
|
136
|
+
let linesUsed = Math.ceil((spinnerChars[0].length + msg.length + 1) / terminalWidth);
|
|
137
|
+
if (termCols < msg.lrngth) process.stdout.write('\n');
|
|
122
138
|
const interval = setInterval(() => {
|
|
123
|
-
|
|
139
|
+
const displayText = `${spinnerChars[i++]} ${msg}`;
|
|
140
|
+
linesUsed = Math.ceil(displayText.length / terminalWidth);
|
|
141
|
+
let clearLines = '';
|
|
142
|
+
for (let j = 0; j < linesUsed - 1; j++) {
|
|
143
|
+
clearLines += '\x1b[1F\x1b[K';
|
|
144
|
+
}
|
|
145
|
+
clearLines += '\r\x1b[K';
|
|
146
|
+
process.stdout.write(clearLines + displayText);
|
|
124
147
|
i = i % spinnerChars.length;
|
|
125
148
|
}, 50);
|
|
126
149
|
return () => {
|
|
127
150
|
clearInterval(interval);
|
|
128
|
-
|
|
151
|
+
let clearLines = '';
|
|
152
|
+
for (let j = 0; j < linesUsed - 1; j++) {
|
|
153
|
+
clearLines += '\x1b[1F\x1b[K';
|
|
154
|
+
}
|
|
155
|
+
clearLines += '\r\x1b[K';
|
|
156
|
+
process.stdout.write(clearLines);
|
|
129
157
|
}
|
|
130
158
|
}
|
|
131
159
|
|