webide-cli 0.0.1-beta.3 → 0.0.1-beta.5

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.
Files changed (3) hide show
  1. package/README.md +2 -0
  2. package/index.js +62 -17
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -45,4 +45,6 @@ webide config delete <key>
45
45
  webide config list
46
46
  # 显示所有第三方库需引入的文件
47
47
  webide imports
48
+ # 更新WebIDE CLI
49
+ webide update
48
50
  ```
package/index.js CHANGED
@@ -6,6 +6,7 @@ const fs = require('fs');
6
6
  const os = require('os');
7
7
  const path = require('path');
8
8
  const readline = require('readline');
9
+ const { exec } = require('child_process');
9
10
 
10
11
  const termCols = process.stdout.columns;
11
12
  const termRows = process.stdout.rows;
@@ -17,16 +18,22 @@ const FILE_BUCKET = "jslib";
17
18
  const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
18
19
  const bucket = supabase.storage.from(FILE_BUCKET);
19
20
 
21
+ const homedir = process.env.HOME || os.homedir();
20
22
  const workdir = process.cwd();
21
23
  const workspacedir = process.env.WEBIDE_WORKSPACE;
22
24
  const webideVersionName = process.env.WEBIDE_VERSION_NAME || 'Unknown';
23
25
  const webideVersionCode = process.env.WEBIDE_VERSION_CODE || 'Unknown';
24
- const homedir = process.env.HOME || os.homedir();
26
+ const projectDir = process.env.WEBIDE_PROJECT_DIR;
25
27
  const configdir = path.join(homedir, '.webide.json');
26
28
 
29
+ if (!projectDir) {
30
+ console.error("Cannot read WEBIDE_PROJECT_DIR environment variable.");
31
+ process.exit(114514);
32
+ }
33
+
27
34
  program
28
35
  .name('webide')
29
- .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)
30
37
  .version(version)
31
38
 
32
39
  program
@@ -45,6 +52,7 @@ program
45
52
  console.log(" WebIDE Version Name:", webideVersionName);
46
53
  console.log(" WebIDE Version Code:", webideVersionCode);
47
54
  console.log(" Working directory:", workdir);
55
+ console.log(" Project directory:", projectDir);
48
56
  console.log(" Workspace directory:", workspacedir);
49
57
  console.log(" Terminal columns:", termCols);
50
58
  console.log(" Terminal rows:", termRows);
@@ -61,12 +69,12 @@ program
61
69
  .command('add <name>')
62
70
  .description('Add a new library.')
63
71
  .action(async (libname) => {
64
- fs.access(workdir + "/webapp.json", fs.constants.F_OK, async (err) => {
72
+ fs.access(projectDir + "/webapp.json", fs.constants.F_OK, async (err) => {
65
73
  if (err) {
66
74
  console.error("webapp.json not found in current directory. Please make sure you are in a WebIDE project directory.");
67
75
  process.exit(114514);
68
76
  }
69
- fs.mkdirSync(path.join(workdir, 'libs', libname), { recursive: true });
77
+ fs.mkdirSync(path.join(projectDir, 'libs', libname), { recursive: true });
70
78
  try {
71
79
  const load = loading(`Checking if library "${libname}" exists...`);
72
80
  if (await checkFileExists(libname + "/lib.json")) {
@@ -87,11 +95,11 @@ program
87
95
  if (imports && imports.length > 0) {
88
96
  console.log(` Imports:`);
89
97
  for (let i = 0; i < imports.length; i++) {
90
- console.log(` ${path.join(workdir, 'libs', libname, imports[i])}`);
98
+ console.log(` ${path.join(projectDir, 'libs', libname, imports[i])}`);
91
99
  }
92
100
  }
93
101
  load2 = loading(downloadingTip);
94
- await download(libname, path.join(workdir, 'libs', libname));
102
+ await download(libname, path.join(projectDir, 'libs', libname));
95
103
  load2();
96
104
  console.log(`Library "${libname}" downloaded successfully.`);
97
105
  } else {
@@ -100,7 +108,7 @@ program
100
108
  process.exit(114514);
101
109
  }
102
110
  } catch (error) {
103
- fs.rmSync(path.join(workdir, 'libs', libname), { recursive: true, force: true });
111
+ fs.rmSync(path.join(projectDir, 'libs', libname), { recursive: true, force: true });
104
112
  console.error("\r\nAn error occurred:", error.stack);
105
113
  process.exit(114514);
106
114
  }
@@ -111,12 +119,12 @@ program
111
119
  .command('remove <name>')
112
120
  .description('Remove a library')
113
121
  .action(function (libname) {
114
- fs.access(workdir + "/webapp.json", fs.constants.F_OK, (err) => {
122
+ fs.access(projectDir + "/webapp.json", fs.constants.F_OK, (err) => {
115
123
  if (err) {
116
124
  console.error("webapp.json not found in current directory. Please make sure you are in a WebIDE project directory.");
117
125
  process.exit(114514);
118
126
  }
119
- const libPath = path.join(workdir, 'libs', libname);
127
+ const libPath = path.join(projectDir, 'libs', libname);
120
128
  if (fs.existsSync(libPath)) {
121
129
  fs.rmSync(libPath, { recursive: true, force: true });
122
130
  console.log(`Library "${libname}" removed.`);
@@ -207,12 +215,12 @@ program
207
215
  .command('imports')
208
216
  .description('List all imports')
209
217
  .action(function () {
210
- fs.access(workdir + "/webapp.json", fs.constants.F_OK, (err) => {
218
+ fs.access(projectDir + "/webapp.json", fs.constants.F_OK, (err) => {
211
219
  if (err) {
212
220
  console.error("webapp.json not found in current directory. Please make sure you are in a WebIDE project directory.");
213
221
  process.exit(114514);
214
222
  }
215
- const libsDir = path.join(workdir, 'libs');
223
+ const libsDir = path.join(projectDir, 'libs');
216
224
  const files = [];
217
225
  if (!fs.existsSync(libsDir)) {
218
226
  console.log("No libraries imported.");
@@ -237,7 +245,48 @@ program
237
245
  }
238
246
  }
239
247
  }
240
- fs.writeFileSync(path.join(workdir, 'imports.txt'), files.join('\n'));
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 (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);
241
290
  });
242
291
  });
243
292
 
@@ -259,7 +308,7 @@ function loading(msg) {
259
308
  const terminalWidth = process.stdout.columns;
260
309
  let i = 0;
261
310
  let linesUsed = Math.ceil((spinnerChars[0].length + msg.length + 1) / terminalWidth);
262
- 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');
263
312
  const interval = setInterval(() => {
264
313
  const displayText = `${spinnerChars[i++]} ${msg}`;
265
314
  linesUsed = Math.ceil(displayText.length / terminalWidth);
@@ -314,12 +363,8 @@ function getCursorPosition() {
314
363
  input: process.stdin,
315
364
  output: process.stdout
316
365
  });
317
-
318
- // 发送查询光标位置的ANSI序列
319
366
  process.stdout.write('\x1B[6n');
320
-
321
367
  process.stdin.once('data', (data) => {
322
- // 解析响应 \x1B[row;colR
323
368
  const match = data.toString().match(/\[(\d+);(\d+)R/);
324
369
  if (match) {
325
370
  const row = parseInt(match[1]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webide-cli",
3
- "version": "0.0.1-beta.3",
3
+ "version": "0.0.1-beta.5",
4
4
  "description": "A CLI for NEW WebIDE",
5
5
  "keywords": [
6
6
  "WebIDE",