webide-cli 0.0.1-beta.2 → 0.0.1-beta.4
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/.vscode/settings.json +3 -0
- package/README.md +25 -1
- package/index.js +157 -13
- package/package.json +1 -1
- package/webapp.json +1 -0
package/README.md
CHANGED
|
@@ -23,4 +23,28 @@ webide -v
|
|
|
23
23
|
```bash
|
|
24
24
|
webide -h
|
|
25
25
|
```
|
|
26
|
-
> 安装过程如有问题,请加Q群反馈
|
|
26
|
+
> 安装过程如有问题,请加Q群反馈
|
|
27
|
+
|
|
28
|
+
# 使用方法
|
|
29
|
+
```bash
|
|
30
|
+
# 列出WebIDE CLI相关信息
|
|
31
|
+
webide info
|
|
32
|
+
# 显示当前工作目录
|
|
33
|
+
webide workdir
|
|
34
|
+
# 添加第三方库
|
|
35
|
+
webide add <name>
|
|
36
|
+
# 移除第三方库
|
|
37
|
+
webide remove <name>
|
|
38
|
+
# 设置相关配置
|
|
39
|
+
webide config set <key> <value>
|
|
40
|
+
# 获取相关配置
|
|
41
|
+
webide config get <key>
|
|
42
|
+
# 删除相关配置
|
|
43
|
+
webide config delete <key>
|
|
44
|
+
# 列出所有配置项
|
|
45
|
+
webide config list
|
|
46
|
+
# 显示所有第三方库需引入的文件
|
|
47
|
+
webide imports
|
|
48
|
+
# 更新WebIDE CLI
|
|
49
|
+
webide update
|
|
50
|
+
```
|
package/index.js
CHANGED
|
@@ -3,8 +3,10 @@ const { program } = require('commander');
|
|
|
3
3
|
const { version } = require('./package.json');
|
|
4
4
|
const { createClient } = require('@supabase/supabase-js');
|
|
5
5
|
const fs = require('fs');
|
|
6
|
+
const os = require('os');
|
|
6
7
|
const path = require('path');
|
|
7
8
|
const readline = require('readline');
|
|
9
|
+
const { exec } = require('child_process');
|
|
8
10
|
|
|
9
11
|
const termCols = process.stdout.columns;
|
|
10
12
|
const termRows = process.stdout.rows;
|
|
@@ -16,15 +18,22 @@ const FILE_BUCKET = "jslib";
|
|
|
16
18
|
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
|
|
17
19
|
const bucket = supabase.storage.from(FILE_BUCKET);
|
|
18
20
|
|
|
21
|
+
const homedir = process.env.HOME || os.homedir();
|
|
19
22
|
const workdir = process.cwd();
|
|
20
23
|
const workspacedir = process.env.WEBIDE_WORKSPACE;
|
|
21
24
|
const webideVersionName = process.env.WEBIDE_VERSION_NAME || 'Unknown';
|
|
22
25
|
const webideVersionCode = process.env.WEBIDE_VERSION_CODE || 'Unknown';
|
|
26
|
+
const projectDir = process.env.WEBIDE_PROJECT_DIR;
|
|
27
|
+
const configdir = path.join(homedir, '.webide.json');
|
|
23
28
|
|
|
29
|
+
if (!projectDir) {
|
|
30
|
+
console.error("Cannot read WEBIDE_PROJECT_DIR environment variable.");
|
|
31
|
+
process.exit(114514);
|
|
32
|
+
}
|
|
24
33
|
|
|
25
34
|
program
|
|
26
35
|
.name('webide')
|
|
27
|
-
.description('A CLI for NEW WebIDE.\nJoin QQ group 1050254184 for help and feedback
|
|
36
|
+
.description('A CLI for NEW WebIDE.\nJoin QQ group 1050254184 for help and feedback.\n\nCurrent project directory: ' + projectDir)
|
|
28
37
|
.version(version)
|
|
29
38
|
|
|
30
39
|
program
|
|
@@ -43,6 +52,7 @@ program
|
|
|
43
52
|
console.log(" WebIDE Version Name:", webideVersionName);
|
|
44
53
|
console.log(" WebIDE Version Code:", webideVersionCode);
|
|
45
54
|
console.log(" Working directory:", workdir);
|
|
55
|
+
console.log(" Project directory:", projectDir);
|
|
46
56
|
console.log(" Workspace directory:", workspacedir);
|
|
47
57
|
console.log(" Terminal columns:", termCols);
|
|
48
58
|
console.log(" Terminal rows:", termRows);
|
|
@@ -59,12 +69,12 @@ program
|
|
|
59
69
|
.command('add <name>')
|
|
60
70
|
.description('Add a new library.')
|
|
61
71
|
.action(async (libname) => {
|
|
62
|
-
fs.access(
|
|
72
|
+
fs.access(projectDir + "/webapp.json", fs.constants.F_OK, async (err) => {
|
|
63
73
|
if (err) {
|
|
64
74
|
console.error("webapp.json not found in current directory. Please make sure you are in a WebIDE project directory.");
|
|
65
75
|
process.exit(114514);
|
|
66
76
|
}
|
|
67
|
-
fs.mkdirSync(path.join(
|
|
77
|
+
fs.mkdirSync(path.join(projectDir, 'libs', libname), { recursive: true });
|
|
68
78
|
try {
|
|
69
79
|
const load = loading(`Checking if library "${libname}" exists...`);
|
|
70
80
|
if (await checkFileExists(libname + "/lib.json")) {
|
|
@@ -85,11 +95,11 @@ program
|
|
|
85
95
|
if (imports && imports.length > 0) {
|
|
86
96
|
console.log(` Imports:`);
|
|
87
97
|
for (let i = 0; i < imports.length; i++) {
|
|
88
|
-
console.log(` ${path.join(
|
|
98
|
+
console.log(` ${path.join(projectDir, 'libs', libname, imports[i])}`);
|
|
89
99
|
}
|
|
90
100
|
}
|
|
91
101
|
load2 = loading(downloadingTip);
|
|
92
|
-
await download(libname, path.join(
|
|
102
|
+
await download(libname, path.join(projectDir, 'libs', libname));
|
|
93
103
|
load2();
|
|
94
104
|
console.log(`Library "${libname}" downloaded successfully.`);
|
|
95
105
|
} else {
|
|
@@ -98,7 +108,7 @@ program
|
|
|
98
108
|
process.exit(114514);
|
|
99
109
|
}
|
|
100
110
|
} catch (error) {
|
|
101
|
-
fs.rmSync(path.join(
|
|
111
|
+
fs.rmSync(path.join(projectDir, 'libs', libname), { recursive: true, force: true });
|
|
102
112
|
console.error("\r\nAn error occurred:", error.stack);
|
|
103
113
|
process.exit(114514);
|
|
104
114
|
}
|
|
@@ -109,12 +119,12 @@ program
|
|
|
109
119
|
.command('remove <name>')
|
|
110
120
|
.description('Remove a library')
|
|
111
121
|
.action(function (libname) {
|
|
112
|
-
fs.access(
|
|
122
|
+
fs.access(projectDir + "/webapp.json", fs.constants.F_OK, (err) => {
|
|
113
123
|
if (err) {
|
|
114
124
|
console.error("webapp.json not found in current directory. Please make sure you are in a WebIDE project directory.");
|
|
115
125
|
process.exit(114514);
|
|
116
126
|
}
|
|
117
|
-
const libPath = path.join(
|
|
127
|
+
const libPath = path.join(projectDir, 'libs', libname);
|
|
118
128
|
if (fs.existsSync(libPath)) {
|
|
119
129
|
fs.rmSync(libPath, { recursive: true, force: true });
|
|
120
130
|
console.log(`Library "${libname}" removed.`);
|
|
@@ -142,6 +152,144 @@ program
|
|
|
142
152
|
}
|
|
143
153
|
});
|
|
144
154
|
|
|
155
|
+
const configCmd = program
|
|
156
|
+
.command('config')
|
|
157
|
+
.description('Configuration management');
|
|
158
|
+
|
|
159
|
+
configCmd
|
|
160
|
+
.command('set <key> <value>')
|
|
161
|
+
.description('Set a configuration value')
|
|
162
|
+
.action(function (key, value) {
|
|
163
|
+
const config = fs.existsSync(configdir) ? JSON.parse(fs.readFileSync(configdir, 'utf8')) : {};
|
|
164
|
+
config[key] = value;
|
|
165
|
+
fs.writeFileSync(configdir, JSON.stringify(config, null, 4));
|
|
166
|
+
console.log(`Configuration "${key}" set to "${value}".`);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
configCmd
|
|
170
|
+
.command('get <key>')
|
|
171
|
+
.description('Get a configuration value.')
|
|
172
|
+
.action(function (key) {
|
|
173
|
+
if (!fs.existsSync(configdir)) {
|
|
174
|
+
fs.writeFileSync(configdir, '{}');
|
|
175
|
+
}
|
|
176
|
+
const config = JSON.parse(fs.readFileSync(configdir, 'utf8'));
|
|
177
|
+
if (config.hasOwnProperty(key)) {
|
|
178
|
+
console.log(`${key}=${config[key]}`);
|
|
179
|
+
} else {
|
|
180
|
+
console.error(`Configuration "${key}" does not exist.`);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
configCmd
|
|
185
|
+
.command('delete <key>')
|
|
186
|
+
.description('Delete a configuration')
|
|
187
|
+
.action(function (key) {
|
|
188
|
+
if (!fs.existsSync(configdir)) {
|
|
189
|
+
fs.writeFileSync(configdir, '{}');
|
|
190
|
+
}
|
|
191
|
+
let config = JSON.parse(fs.readFileSync(configdir));
|
|
192
|
+
if (config.hasOwnProperty(key)) {
|
|
193
|
+
delete config[key];
|
|
194
|
+
fs.writeFileSync(configdir, JSON.stringify(config, null, 4));
|
|
195
|
+
console.log(`Deleted configuration "${key}".`);
|
|
196
|
+
} else {
|
|
197
|
+
console.error(`Configuration "${key}" does not exist.`);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
configCmd
|
|
202
|
+
.command('list')
|
|
203
|
+
.description('List all configurations')
|
|
204
|
+
.action(function () {
|
|
205
|
+
if (!fs.existsSync(configdir)) {
|
|
206
|
+
fs.writeFileSync(configdir, '{}');
|
|
207
|
+
}
|
|
208
|
+
const config = JSON.parse(fs.readFileSync(configdir, 'utf8'));
|
|
209
|
+
for (const key in config) {
|
|
210
|
+
console.log(`${key}=${config[key]}`);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
program
|
|
215
|
+
.command('imports')
|
|
216
|
+
.description('List all imports')
|
|
217
|
+
.action(function () {
|
|
218
|
+
fs.access(projectDir + "/webapp.json", fs.constants.F_OK, (err) => {
|
|
219
|
+
if (err) {
|
|
220
|
+
console.error("webapp.json not found in current directory. Please make sure you are in a WebIDE project directory.");
|
|
221
|
+
process.exit(114514);
|
|
222
|
+
}
|
|
223
|
+
const libsDir = path.join(projectDir, 'libs');
|
|
224
|
+
const files = [];
|
|
225
|
+
if (!fs.existsSync(libsDir)) {
|
|
226
|
+
console.log("No libraries imported.");
|
|
227
|
+
process.exit(0);
|
|
228
|
+
}
|
|
229
|
+
const libnames = fs.readdirSync(libsDir);
|
|
230
|
+
if (libnames.length === 0) {
|
|
231
|
+
console.log("No libraries imported.");
|
|
232
|
+
process.exit(0);
|
|
233
|
+
}
|
|
234
|
+
console.log("Libraries import files:");
|
|
235
|
+
for (const libname of libnames) {
|
|
236
|
+
console.log(` ${libname}`);
|
|
237
|
+
const libPath = path.join(libsDir, libname, 'lib.json');
|
|
238
|
+
const libJson = JSON.parse(fs.readFileSync(libPath));
|
|
239
|
+
const imports = libJson.imports || [];
|
|
240
|
+
if (imports.length > 0) {
|
|
241
|
+
for (let i = 0; i < imports.length; i++) {
|
|
242
|
+
const importPath = path.join(libsDir, libname, imports[i]);
|
|
243
|
+
files.push(encodeURIComponent(imports[i]));
|
|
244
|
+
console.log(` ${i == imports.length - 1 ? '└──' : '├──'} ${importPath}`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
fs.writeFileSync(path.join(projectDir, 'imports.txt'), files.join('\n'));
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
program
|
|
253
|
+
.command('update')
|
|
254
|
+
.description('Update WebIDE')
|
|
255
|
+
.action(async function () {
|
|
256
|
+
const load = loading("Checking for updates...");
|
|
257
|
+
const r = await fetch("https://registry.npmjs.org/webide-cli");
|
|
258
|
+
const data = await r.json();
|
|
259
|
+
const timeKeys = Object.keys(data.time);
|
|
260
|
+
const times = [];
|
|
261
|
+
for (let i = 0; i < timeKeys.length; i++) {
|
|
262
|
+
if (timeKeys[i] == 'modified') continue;
|
|
263
|
+
if (timeKeys[i] == 'created') continue;
|
|
264
|
+
times.push(Date.parse(data.time[timeKeys[i]]));
|
|
265
|
+
}
|
|
266
|
+
times.sort((a, b) => b - a);
|
|
267
|
+
const latestVersion = timeKeys.find(key => data.time[key] == new Date(times[0]).toISOString());
|
|
268
|
+
load();
|
|
269
|
+
if (data.version == latestVersion) {
|
|
270
|
+
console.log("You are using the latest version.");
|
|
271
|
+
process.exit(0);
|
|
272
|
+
}
|
|
273
|
+
console.log(`Latest version: ${latestVersion}`);
|
|
274
|
+
const load2 = loading("Updating...");
|
|
275
|
+
exec('npm install -g webide-cli@' + latestVersion, (error, stdout, stderr) => {
|
|
276
|
+
load2();
|
|
277
|
+
if (error) {
|
|
278
|
+
console.error(`Update failed:${error.stack}`);
|
|
279
|
+
if (error.stack.includes('EPERM')) {
|
|
280
|
+
console.error("Error Analysis Results: Premission denied.");
|
|
281
|
+
}
|
|
282
|
+
process.exit(114514);
|
|
283
|
+
}
|
|
284
|
+
if (stderr) {
|
|
285
|
+
console.error(`Update failed:${stderr}`);
|
|
286
|
+
process.exit(114514);
|
|
287
|
+
}
|
|
288
|
+
console.log("Update successful!");
|
|
289
|
+
process.exit(0);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
145
293
|
program
|
|
146
294
|
.parse(process.argv);
|
|
147
295
|
|
|
@@ -160,7 +308,7 @@ function loading(msg) {
|
|
|
160
308
|
const terminalWidth = process.stdout.columns;
|
|
161
309
|
let i = 0;
|
|
162
310
|
let linesUsed = Math.ceil((spinnerChars[0].length + msg.length + 1) / terminalWidth);
|
|
163
|
-
if (termCols < msg.length + 14 + workdir.length) process.stdout.write('\n');
|
|
311
|
+
if (termCols < msg.length + 14 + workdir.replace(homedir, '~').length) process.stdout.write('\n');
|
|
164
312
|
const interval = setInterval(() => {
|
|
165
313
|
const displayText = `${spinnerChars[i++]} ${msg}`;
|
|
166
314
|
linesUsed = Math.ceil(displayText.length / terminalWidth);
|
|
@@ -215,12 +363,8 @@ function getCursorPosition() {
|
|
|
215
363
|
input: process.stdin,
|
|
216
364
|
output: process.stdout
|
|
217
365
|
});
|
|
218
|
-
|
|
219
|
-
// 发送查询光标位置的ANSI序列
|
|
220
366
|
process.stdout.write('\x1B[6n');
|
|
221
|
-
|
|
222
367
|
process.stdin.once('data', (data) => {
|
|
223
|
-
// 解析响应 \x1B[row;colR
|
|
224
368
|
const match = data.toString().match(/\[(\d+);(\d+)R/);
|
|
225
369
|
if (match) {
|
|
226
370
|
const row = parseInt(match[1]);
|
package/package.json
CHANGED
package/webapp.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{}
|