webide-cli 0.0.1-alpha.8 → 0.0.1-beta.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.
Files changed (2) hide show
  1. package/index.js +79 -46
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -4,6 +4,9 @@ const { version } = require('./package.json');
4
4
  const { createClient } = require('@supabase/supabase-js');
5
5
  const fs = require('fs');
6
6
  const path = require('path');
7
+ const readline = require('readline');
8
+
9
+ const termCols = process.stdout.columns;
7
10
 
8
11
  const SUPABASE_URL = "https://dbmp-xbgmorqeur6oh81z.database.nocode.cn";
9
12
  const SUPABASE_ANON_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlzcyI6InN1cGFiYXNlIiwiaWF0IjoxNzQ2OTc5MjAwLCJleHAiOjE5MDQ3NDU2MDB9.11QbQ5OW_m10vblDXAlw1Qq7Dve5Swzn12ILo7-9IXY";
@@ -37,60 +40,66 @@ program
37
40
  .command('add <name>')
38
41
  .description('Add a new library.')
39
42
  .action(async (libname) => {
40
- fs.access(workdir + "/webapp.json", fs.constants.F_OK, (err) => {
43
+ fs.access(workdir + "/webapp.json", fs.constants.F_OK, async (err) => {
41
44
  if (err) {
42
45
  console.error("webapp.json not found in current directory. Please make sure you are in a WebIDE project directory.");
43
46
  process.exit(114514);
44
47
  }
45
- });
46
- fs.mkdirSync(path.join(workdir, 'libs', libname), { recursive: true });
47
- try {
48
- const load = loading(`Checking if library "${libname}" exists...`);
49
- if (await checkFileExists(libname + "/lib.json")) {
50
- load();
51
- console.log(`Library "${libname}" already exists.`);
52
- console.log(`Start downloading...`);
53
- const downloadingTip = `Downloading library "${libname}" ...`;
54
- let load2 = loading(downloadingTip);
55
- const libJsonData = await getFile(libname + "/lib.json");
56
- const libJson = JSON.parse(await libJsonData.text());
57
- const { name, author, description, version } = libJson;
58
- load2();
59
- console.log(`Library info: `);
60
- console.log(` Name: ${name}`);
61
- console.log(` Version: ${version}`);
62
- console.log(` Author: ${author || 'Unknown'}`);
63
- console.log(` Description: ${description || 'No description'}`);
64
- load2 = loading(downloadingTip);
65
- await download(libname, path.join(workdir, 'libs', libname));
66
- load2();
67
- console.log(`Library "${libname}" downloaded successfully.`);
68
- } else {
69
- load();
70
- console.error(`Library "${libname}" does not exist in the database.`);
48
+ fs.mkdirSync(path.join(workdir, 'libs', libname), { recursive: true });
49
+ try {
50
+ const load = loading(`Checking if library "${libname}" exists...`);
51
+ if (await checkFileExists(libname + "/lib.json")) {
52
+ load();
53
+ console.log(`Library "${libname}" already exists.`);
54
+ console.log(`Start downloading...`);
55
+ const downloadingTip = `Downloading library "${libname}" ...`;
56
+ let load2 = loading(downloadingTip);
57
+ const libJsonData = await getFile(libname + "/lib.json");
58
+ const libJson = JSON.parse(await libJsonData.text());
59
+ const { name, author, description, version } = libJson;
60
+ load2();
61
+ console.log(`Library info: `);
62
+ console.log(` Name: ${name}`);
63
+ console.log(` Version: ${version}`);
64
+ console.log(` Author: ${author || 'Unknown'}`);
65
+ console.log(` Description: ${description || 'No description'}`);
66
+ load2 = loading(downloadingTip);
67
+ await download(libname, path.join(workdir, 'libs', libname));
68
+ load2();
69
+ console.log(`Library "${libname}" downloaded successfully.`);
70
+ } else {
71
+ load();
72
+ console.error(`Library "${libname}" does not exist in the database.`);
73
+ process.exit(114514);
74
+ }
75
+ } catch (error) {
76
+ fs.rmSync(path.join(workdir, 'libs', libname), { recursive: true, force: true });
77
+ console.error("\r\nAn error occurred:", error.stack);
71
78
  process.exit(114514);
72
79
  }
73
- } catch (error) {
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
- }
80
+ });
78
81
  });
79
82
 
80
83
  program
81
84
  .command('remove <name>')
82
85
  .description('Remove a library')
83
86
  .action(function (libname) {
84
- const libPath = path.join(workdir, 'libs', libname);
85
- if (fs.existsSync(libPath)) {
86
- fs.rmSync(libPath, { recursive: true, force: true });
87
- console.log(`Library "${libname}" removed.`);
88
- } else {
89
- console.error(`Library "${libname}" does not exist.`);
90
- }
87
+ fs.access(workdir + "/webapp.json", fs.constants.F_OK, (err) => {
88
+ if (err) {
89
+ console.error("webapp.json not found in current directory. Please make sure you are in a WebIDE project directory.");
90
+ process.exit(114514);
91
+ }
92
+ const libPath = path.join(workdir, 'libs', libname);
93
+ if (fs.existsSync(libPath)) {
94
+ fs.rmSync(libPath, { recursive: true, force: true });
95
+ console.log(`Library "${libname}" removed.`);
96
+ } else {
97
+ console.error(`Library "${libname}" does not exist.`);
98
+ }
99
+ });
91
100
  });
92
101
 
93
- function truncateMessage(msg, maxLength = 50) {
102
+ function truncateMessage(msg, maxLength = termCols) {
94
103
  if (msg.length <= maxLength) return msg;
95
104
  return msg.substring(0, maxLength - 3) + '...';
96
105
  }
@@ -121,20 +130,18 @@ async function getFile(path) {
121
130
  return data;
122
131
  }
123
132
 
124
- function loading(msg) {
133
+ async function loading(msg) {
125
134
  const spinnerChars = ['⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
126
- const terminalWidth = process.stdout.columns || 80; // 获取终端宽度
135
+ const terminalWidth = process.stdout.columns;
127
136
  let i = 0;
128
137
  let linesUsed = Math.ceil((spinnerChars[0].length + msg.length + 1) / terminalWidth);
129
-
138
+ if (termCols < msg.length + 14 + workdir.length) process.stdout.write('\n');
130
139
  const interval = setInterval(() => {
131
140
  const displayText = `${spinnerChars[i++]} ${msg}`;
132
141
  linesUsed = Math.ceil(displayText.length / terminalWidth);
133
-
134
- // 清除之前占用的行
135
142
  let clearLines = '';
136
143
  for (let j = 0; j < linesUsed - 1; j++) {
137
- clearLines += '\x1b[1F\x1b[K'; // 移动到上一行并清除
144
+ clearLines += '\x1b[1F\x1b[K';
138
145
  }
139
146
  clearLines += '\r\x1b[K';
140
147
  process.stdout.write(clearLines + displayText);
@@ -175,4 +182,30 @@ async function search(keyword) {
175
182
  const { data, error } = await bucket.list('');
176
183
  if (error) throw error;
177
184
  return data.filter(file => file.name.includes(keyword));
185
+ }
186
+
187
+ function getCursorPosition() {
188
+ return new Promise((resolve) => {
189
+ const rl = readline.createInterface({
190
+ input: process.stdin,
191
+ output: process.stdout
192
+ });
193
+
194
+ // 发送查询光标位置的ANSI序列
195
+ process.stdout.write('\x1B[6n');
196
+
197
+ process.stdin.once('data', (data) => {
198
+ // 解析响应 \x1B[row;colR
199
+ const match = data.toString().match(/\[(\d+);(\d+)R/);
200
+ if (match) {
201
+ const row = parseInt(match[1]);
202
+ const col = parseInt(match[2]);
203
+ resolve({ row, col });
204
+ } else {
205
+ resolve(null);
206
+ }
207
+
208
+ rl.close();
209
+ });
210
+ });
178
211
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webide-cli",
3
- "version": "0.0.1-alpha.8",
3
+ "version": "0.0.1-beta.0",
4
4
  "description": "A CLI for NEW WebIDE",
5
5
  "keywords": [
6
6
  "WebIDE",