|
@@ -1,28 +1,40 @@
|
|
|
#!/usr/bin/env node
|
|
#!/usr/bin/env node
|
|
|
-'use strict';
|
|
|
|
|
-
|
|
|
|
|
-var socket_io = require('socket.io');
|
|
|
|
|
-var child_process = require('child_process');
|
|
|
|
|
-var fs = require('fs/promises');
|
|
|
|
|
-var path = require('path');
|
|
|
|
|
-var net = require('net');
|
|
|
|
|
-var events = require('events');
|
|
|
|
|
-var createTunnel = require('@d8d-localtunnel/client');
|
|
|
|
|
-var http = require('http');
|
|
|
|
|
-var chokidar = require('chokidar');
|
|
|
|
|
-var fs$1 = require('fs');
|
|
|
|
|
-var anymatch = require('anymatch');
|
|
|
|
|
-
|
|
|
|
|
-function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
|
|
|
|
|
+"use strict";
|
|
|
|
|
+var __create = Object.create;
|
|
|
|
|
+var __defProp = Object.defineProperty;
|
|
|
|
|
+var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
|
|
+var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
|
|
+var __getProtoOf = Object.getPrototypeOf;
|
|
|
|
|
+var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
|
|
|
+var __copyProps = (to, from, except, desc) => {
|
|
|
|
|
+ if (from && typeof from === "object" || typeof from === "function") {
|
|
|
|
|
+ for (let key of __getOwnPropNames(from))
|
|
|
|
|
+ if (!__hasOwnProp.call(to, key) && key !== except)
|
|
|
|
|
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
|
|
|
+ }
|
|
|
|
|
+ return to;
|
|
|
|
|
+};
|
|
|
|
|
+var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
|
|
|
+ // If the importer is in node compatibility mode or this is not an ESM
|
|
|
|
|
+ // file that has been converted to a CommonJS file using a Babel-
|
|
|
|
|
+ // compatible transform (i.e. "__esModule" has not been set), then set
|
|
|
|
|
+ // "default" to the CommonJS "module.exports" for node compatibility.
|
|
|
|
|
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
|
|
|
+ mod
|
|
|
|
|
+));
|
|
|
|
|
|
|
|
-var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
|
|
|
-var path__default = /*#__PURE__*/_interopDefault(path);
|
|
|
|
|
-var net__default = /*#__PURE__*/_interopDefault(net);
|
|
|
|
|
-var createTunnel__default = /*#__PURE__*/_interopDefault(createTunnel);
|
|
|
|
|
-var chokidar__default = /*#__PURE__*/_interopDefault(chokidar);
|
|
|
|
|
-var anymatch__default = /*#__PURE__*/_interopDefault(anymatch);
|
|
|
|
|
|
|
+// src/lib/index.ts
|
|
|
|
|
+var import_socket = require("socket.io");
|
|
|
|
|
+var import_child_process2 = require("child_process");
|
|
|
|
|
+var import_promises = __toESM(require("fs/promises"), 1);
|
|
|
|
|
+var import_path = __toESM(require("path"), 1);
|
|
|
|
|
|
|
|
-var ServerDetector = class extends events.EventEmitter {
|
|
|
|
|
|
|
+// src/lib/ServerDetector.ts
|
|
|
|
|
+var import_net = __toESM(require("net"), 1);
|
|
|
|
|
+var import_child_process = require("child_process");
|
|
|
|
|
+var import_events = require("events");
|
|
|
|
|
+var import_client = __toESM(require("@d8d-localtunnel/client"), 1);
|
|
|
|
|
+var ServerDetector = class extends import_events.EventEmitter {
|
|
|
constructor(config) {
|
|
constructor(config) {
|
|
|
super();
|
|
super();
|
|
|
this.checkInterval = null;
|
|
this.checkInterval = null;
|
|
@@ -144,7 +156,7 @@ var ServerDetector = class extends events.EventEmitter {
|
|
|
}
|
|
}
|
|
|
async getChildProcessPorts(parentPid) {
|
|
async getChildProcessPorts(parentPid) {
|
|
|
return new Promise((resolve) => {
|
|
return new Promise((resolve) => {
|
|
|
- child_process.exec(`pgrep -P ${parentPid}`, async (error, stdout) => {
|
|
|
|
|
|
|
+ (0, import_child_process.exec)(`pgrep -P ${parentPid}`, async (error, stdout) => {
|
|
|
if (error) {
|
|
if (error) {
|
|
|
console.error("[ServerDetector] pgrep \u6267\u884C\u5931\u8D25:", error);
|
|
console.error("[ServerDetector] pgrep \u6267\u884C\u5931\u8D25:", error);
|
|
|
resolve([]);
|
|
resolve([]);
|
|
@@ -167,7 +179,7 @@ var ServerDetector = class extends events.EventEmitter {
|
|
|
}
|
|
}
|
|
|
async getProcessPorts(pid) {
|
|
async getProcessPorts(pid) {
|
|
|
return new Promise((resolve) => {
|
|
return new Promise((resolve) => {
|
|
|
- child_process.exec(`lsof -i -P -n -p ${pid} | grep LISTEN`, (error, stdout) => {
|
|
|
|
|
|
|
+ (0, import_child_process.exec)(`lsof -i -P -n -p ${pid} | grep LISTEN`, (error, stdout) => {
|
|
|
if (error) {
|
|
if (error) {
|
|
|
resolve([]);
|
|
resolve([]);
|
|
|
return;
|
|
return;
|
|
@@ -191,7 +203,7 @@ var ServerDetector = class extends events.EventEmitter {
|
|
|
// 抽取处理找到服务器的逻辑
|
|
// 抽取处理找到服务器的逻辑
|
|
|
async handleServerFound(port2) {
|
|
async handleServerFound(port2) {
|
|
|
try {
|
|
try {
|
|
|
- const tunnel = await createTunnel__default.default({
|
|
|
|
|
|
|
+ const tunnel = await (0, import_client.default)({
|
|
|
port: port2,
|
|
port: port2,
|
|
|
host: "https://pre.d8d.fun"
|
|
host: "https://pre.d8d.fun"
|
|
|
});
|
|
});
|
|
@@ -211,7 +223,7 @@ var ServerDetector = class extends events.EventEmitter {
|
|
|
}
|
|
}
|
|
|
async checkPort(port2) {
|
|
async checkPort(port2) {
|
|
|
return new Promise((resolve) => {
|
|
return new Promise((resolve) => {
|
|
|
- const tester = net__default.default.createConnection({
|
|
|
|
|
|
|
+ const tester = import_net.default.createConnection({
|
|
|
port: port2,
|
|
port: port2,
|
|
|
host: "localhost",
|
|
host: "localhost",
|
|
|
timeout: 1e3
|
|
timeout: 1e3
|
|
@@ -258,7 +270,7 @@ var ServerDetector = class extends events.EventEmitter {
|
|
|
return vitePort;
|
|
return vitePort;
|
|
|
}
|
|
}
|
|
|
return new Promise((resolve) => {
|
|
return new Promise((resolve) => {
|
|
|
- child_process.exec(`lsof -i -P -n | grep LISTEN | grep node`, (error, stdout) => {
|
|
|
|
|
|
|
+ (0, import_child_process.exec)(`lsof -i -P -n | grep LISTEN | grep node`, (error, stdout) => {
|
|
|
if (error) {
|
|
if (error) {
|
|
|
console.log("[ServerDetector] Error finding actual port:", error);
|
|
console.log("[ServerDetector] Error finding actual port:", error);
|
|
|
resolve(null);
|
|
resolve(null);
|
|
@@ -335,6 +347,11 @@ var ServerDetector = class extends events.EventEmitter {
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+// src/lib/index.ts
|
|
|
|
|
+var import_http = require("http");
|
|
|
|
|
+var import_chokidar = __toESM(require("chokidar"), 1);
|
|
|
|
|
+var import_fs = require("fs");
|
|
|
|
|
+
|
|
|
// src/lib/constants.ts
|
|
// src/lib/constants.ts
|
|
|
var PROCESS_CONFIG = {
|
|
var PROCESS_CONFIG = {
|
|
|
// 进程终止超时时间(毫秒)
|
|
// 进程终止超时时间(毫秒)
|
|
@@ -375,6 +392,9 @@ var WATCH_CONFIG = {
|
|
|
usePolling: false
|
|
usePolling: false
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
+
|
|
|
|
|
+// src/lib/index.ts
|
|
|
|
|
+var import_anymatch = __toESM(require("anymatch"), 1);
|
|
|
var WebContainerServer = class {
|
|
var WebContainerServer = class {
|
|
|
constructor(config = {}) {
|
|
constructor(config = {}) {
|
|
|
this.workspaces = /* @__PURE__ */ new Map();
|
|
this.workspaces = /* @__PURE__ */ new Map();
|
|
@@ -384,17 +404,17 @@ var WebContainerServer = class {
|
|
|
this.config = {
|
|
this.config = {
|
|
|
port: config.port || SERVER_CONFIG.DEFAULT_PORT,
|
|
port: config.port || SERVER_CONFIG.DEFAULT_PORT,
|
|
|
host: config.host || SERVER_CONFIG.DEFAULT_HOST,
|
|
host: config.host || SERVER_CONFIG.DEFAULT_HOST,
|
|
|
- workspaceRoot: config.workspaceRoot || path__default.default.join(process.cwd(), SERVER_CONFIG.DEFAULT_WORKSPACE_DIR)
|
|
|
|
|
|
|
+ workspaceRoot: config.workspaceRoot || import_path.default.join(process.cwd(), SERVER_CONFIG.DEFAULT_WORKSPACE_DIR)
|
|
|
};
|
|
};
|
|
|
- this.httpServer = http.createServer();
|
|
|
|
|
- this.io = new socket_io.Server(this.httpServer, {
|
|
|
|
|
|
|
+ this.httpServer = (0, import_http.createServer)();
|
|
|
|
|
+ this.io = new import_socket.Server(this.httpServer, {
|
|
|
cors: SERVER_CONFIG.CORS
|
|
cors: SERVER_CONFIG.CORS
|
|
|
});
|
|
});
|
|
|
this.setupSocketHandlers();
|
|
this.setupSocketHandlers();
|
|
|
}
|
|
}
|
|
|
async start() {
|
|
async start() {
|
|
|
try {
|
|
try {
|
|
|
- await fs__default.default.mkdir(this.config.workspaceRoot, { recursive: true });
|
|
|
|
|
|
|
+ await import_promises.default.mkdir(this.config.workspaceRoot, { recursive: true });
|
|
|
const requiredCommands = ["git", "npm", "node"];
|
|
const requiredCommands = ["git", "npm", "node"];
|
|
|
for (const cmd of requiredCommands) {
|
|
for (const cmd of requiredCommands) {
|
|
|
const exists = await this.checkCommandExists(cmd);
|
|
const exists = await this.checkCommandExists(cmd);
|
|
@@ -419,12 +439,12 @@ var WebContainerServer = class {
|
|
|
setupSocketHandlers() {
|
|
setupSocketHandlers() {
|
|
|
this.io.on("connection", async (socket) => {
|
|
this.io.on("connection", async (socket) => {
|
|
|
const workspaceId = Math.random().toString(36).substr(2, 9);
|
|
const workspaceId = Math.random().toString(36).substr(2, 9);
|
|
|
- const workspacePath = path__default.default.join(this.config.workspaceRoot, workspaceId);
|
|
|
|
|
|
|
+ const workspacePath = import_path.default.join(this.config.workspaceRoot, workspaceId);
|
|
|
this.workspaces.set(socket.id, workspacePath);
|
|
this.workspaces.set(socket.id, workspacePath);
|
|
|
this.processes.set(socket.id, /* @__PURE__ */ new Map());
|
|
this.processes.set(socket.id, /* @__PURE__ */ new Map());
|
|
|
console.log(`Client connected. Workspace created at: ${workspacePath}`);
|
|
console.log(`Client connected. Workspace created at: ${workspacePath}`);
|
|
|
try {
|
|
try {
|
|
|
- await fs__default.default.mkdir(workspacePath, { recursive: true });
|
|
|
|
|
|
|
+ await import_promises.default.mkdir(workspacePath, { recursive: true });
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
console.error("Failed to configure NPM mirrors:", error);
|
|
console.error("Failed to configure NPM mirrors:", error);
|
|
|
}
|
|
}
|
|
@@ -515,12 +535,12 @@ var WebContainerServer = class {
|
|
|
console.log(`[Watch] \u5FFD\u7565\u7684\u6587\u4EF6\u6A21\u5F0F:`, ignore);
|
|
console.log(`[Watch] \u5FFD\u7565\u7684\u6587\u4EF6\u6A21\u5F0F:`, ignore);
|
|
|
console.log(`[Watch] ignoreInitial:`, ignoreInitial);
|
|
console.log(`[Watch] ignoreInitial:`, ignoreInitial);
|
|
|
const watchPatterns = [glob, `${glob}/*`, `${glob}/**/*`];
|
|
const watchPatterns = [glob, `${glob}/*`, `${glob}/**/*`];
|
|
|
- const watcher = chokidar__default.default.watch(watchPatterns, {
|
|
|
|
|
|
|
+ const watcher = import_chokidar.default.watch(watchPatterns, {
|
|
|
cwd: workspacePath,
|
|
cwd: workspacePath,
|
|
|
...WATCH_CONFIG.WATCH_OPTIONS,
|
|
...WATCH_CONFIG.WATCH_OPTIONS,
|
|
|
...ignoreInitial !== void 0 ? { ignoreInitial } : {},
|
|
...ignoreInitial !== void 0 ? { ignoreInitial } : {},
|
|
|
ignored: (path3) => {
|
|
ignored: (path3) => {
|
|
|
- const shouldIgnore = anymatch__default.default(ignore, path3);
|
|
|
|
|
|
|
+ const shouldIgnore = (0, import_anymatch.default)(ignore, path3);
|
|
|
if (shouldIgnore) {
|
|
if (shouldIgnore) {
|
|
|
console.log(`[Watch] \u5FFD\u7565\u8DEF\u5F84: ${path3}`);
|
|
console.log(`[Watch] \u5FFD\u7565\u8DEF\u5F84: ${path3}`);
|
|
|
}
|
|
}
|
|
@@ -531,7 +551,7 @@ var WebContainerServer = class {
|
|
|
const fullPath = `${workspacePath}/${path3}`;
|
|
const fullPath = `${workspacePath}/${path3}`;
|
|
|
let stats = null;
|
|
let stats = null;
|
|
|
try {
|
|
try {
|
|
|
- stats = await fs$1.promises.stat(fullPath).catch(() => null);
|
|
|
|
|
|
|
+ stats = await import_fs.promises.stat(fullPath).catch(() => null);
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
}
|
|
}
|
|
|
socket.emit("fs:change", {
|
|
socket.emit("fs:change", {
|
|
@@ -592,9 +612,9 @@ var WebContainerServer = class {
|
|
|
console.log(`\u6B63\u5728\u7EC8\u6B62\u5DE5\u4F5C\u533A\u8FDB\u7A0B: ${workspacePath}`);
|
|
console.log(`\u6B63\u5728\u7EC8\u6B62\u5DE5\u4F5C\u533A\u8FDB\u7A0B: ${workspacePath}`);
|
|
|
await this.killAllProcesses(socket.id);
|
|
await this.killAllProcesses(socket.id);
|
|
|
console.log(`\u6B63\u5728\u6E05\u7406\u5DE5\u4F5C\u533A\u76EE\u5F55: ${workspacePath}`);
|
|
console.log(`\u6B63\u5728\u6E05\u7406\u5DE5\u4F5C\u533A\u76EE\u5F55: ${workspacePath}`);
|
|
|
- const exists = await fs__default.default.access(workspacePath).then(() => true).catch(() => false);
|
|
|
|
|
|
|
+ const exists = await import_promises.default.access(workspacePath).then(() => true).catch(() => false);
|
|
|
if (exists) {
|
|
if (exists) {
|
|
|
- await fs__default.default.rm(workspacePath, { recursive: true, force: true });
|
|
|
|
|
|
|
+ await import_promises.default.rm(workspacePath, { recursive: true, force: true });
|
|
|
console.log(`\u5DE5\u4F5C\u533A\u76EE\u5F55\u5DF2\u6E05\u7406: ${workspacePath}`);
|
|
console.log(`\u5DE5\u4F5C\u533A\u76EE\u5F55\u5DF2\u6E05\u7406: ${workspacePath}`);
|
|
|
} else {
|
|
} else {
|
|
|
console.log(`\u5DE5\u4F5C\u533A\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u6E05\u7406: ${workspacePath}`);
|
|
console.log(`\u5DE5\u4F5C\u533A\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u6E05\u7406: ${workspacePath}`);
|
|
@@ -635,7 +655,7 @@ var WebContainerServer = class {
|
|
|
}
|
|
}
|
|
|
await new Promise((resolve, reject) => {
|
|
await new Promise((resolve, reject) => {
|
|
|
const pid = process2.pid;
|
|
const pid = process2.pid;
|
|
|
- const pkill = child_process.spawn("pkill", ["-TERM", "-P", pid.toString()]);
|
|
|
|
|
|
|
+ const pkill = (0, import_child_process2.spawn)("pkill", ["-TERM", "-P", pid.toString()]);
|
|
|
pkill.on("close", async () => {
|
|
pkill.on("close", async () => {
|
|
|
try {
|
|
try {
|
|
|
process2.kill("SIGINT");
|
|
process2.kill("SIGINT");
|
|
@@ -645,7 +665,7 @@ var WebContainerServer = class {
|
|
|
await new Promise((resolveWait) => {
|
|
await new Promise((resolveWait) => {
|
|
|
const timeout = setTimeout(() => {
|
|
const timeout = setTimeout(() => {
|
|
|
try {
|
|
try {
|
|
|
- child_process.spawn("pkill", ["-KILL", "-P", pid.toString()]);
|
|
|
|
|
|
|
+ (0, import_child_process2.spawn)("pkill", ["-KILL", "-P", pid.toString()]);
|
|
|
process2.kill("SIGKILL");
|
|
process2.kill("SIGKILL");
|
|
|
console.log(
|
|
console.log(
|
|
|
`\u8FDB\u7A0B ${pid} (\u7EC8\u7AEF ${terminalId}) \u5DF2\u5F3A\u5236\u7EC8\u6B62`
|
|
`\u8FDB\u7A0B ${pid} (\u7EC8\u7AEF ${terminalId}) \u5DF2\u5F3A\u5236\u7EC8\u6B62`
|
|
@@ -689,16 +709,16 @@ var WebContainerServer = class {
|
|
|
console.log(`Socket ${socketId} \u7684\u6240\u6709\u8FDB\u7A0B\u5DF2\u6E05\u7406\u5B8C\u6210`);
|
|
console.log(`Socket ${socketId} \u7684\u6240\u6709\u8FDB\u7A0B\u5DF2\u6E05\u7406\u5B8C\u6210`);
|
|
|
}
|
|
}
|
|
|
async handleMount(workspacePath, files) {
|
|
async handleMount(workspacePath, files) {
|
|
|
- await fs__default.default.mkdir(workspacePath, { recursive: true });
|
|
|
|
|
|
|
+ await import_promises.default.mkdir(workspacePath, { recursive: true });
|
|
|
await this.createFiles(workspacePath, files);
|
|
await this.createFiles(workspacePath, files);
|
|
|
}
|
|
}
|
|
|
async createFiles(basePath, files) {
|
|
async createFiles(basePath, files) {
|
|
|
for (const [name, item] of Object.entries(files)) {
|
|
for (const [name, item] of Object.entries(files)) {
|
|
|
- const fullPath = path__default.default.join(basePath, name);
|
|
|
|
|
|
|
+ const fullPath = import_path.default.join(basePath, name);
|
|
|
if ("file" in item) {
|
|
if ("file" in item) {
|
|
|
- await fs__default.default.writeFile(fullPath, item.file.contents);
|
|
|
|
|
|
|
+ await import_promises.default.writeFile(fullPath, item.file.contents);
|
|
|
} else if ("directory" in item) {
|
|
} else if ("directory" in item) {
|
|
|
- await fs__default.default.mkdir(fullPath, { recursive: true });
|
|
|
|
|
|
|
+ await import_promises.default.mkdir(fullPath, { recursive: true });
|
|
|
await this.createFiles(fullPath, item.directory.contents);
|
|
await this.createFiles(fullPath, item.directory.contents);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -706,7 +726,7 @@ var WebContainerServer = class {
|
|
|
async checkCommandExists(command) {
|
|
async checkCommandExists(command) {
|
|
|
try {
|
|
try {
|
|
|
await new Promise((resolve, reject) => {
|
|
await new Promise((resolve, reject) => {
|
|
|
- const check = child_process.spawn("which", [command]);
|
|
|
|
|
|
|
+ const check = (0, import_child_process2.spawn)("which", [command]);
|
|
|
check.on("close", (code) => {
|
|
check.on("close", (code) => {
|
|
|
if (code === 0) {
|
|
if (code === 0) {
|
|
|
resolve();
|
|
resolve();
|
|
@@ -730,7 +750,7 @@ var WebContainerServer = class {
|
|
|
throw new Error(`\u547D\u4EE4 '${command}' \u672A\u5B89\u88C5\u3002\u8BF7\u786E\u4FDD\u7CFB\u7EDF\u5DF2\u5B89\u88C5\u8BE5\u547D\u4EE4\u5E76\u6DFB\u52A0\u5230 PATH \u73AF\u5883\u53D8\u91CF\u4E2D\u3002`);
|
|
throw new Error(`\u547D\u4EE4 '${command}' \u672A\u5B89\u88C5\u3002\u8BF7\u786E\u4FDD\u7CFB\u7EDF\u5DF2\u5B89\u88C5\u8BE5\u547D\u4EE4\u5E76\u6DFB\u52A0\u5230 PATH \u73AF\u5883\u53D8\u91CF\u4E2D\u3002`);
|
|
|
}
|
|
}
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
- const process2 = child_process.spawn(command, args, {
|
|
|
|
|
|
|
+ const process2 = (0, import_child_process2.spawn)(command, args, {
|
|
|
cwd: workspacePath,
|
|
cwd: workspacePath,
|
|
|
stdio: ["pipe", "pipe", "pipe"],
|
|
stdio: ["pipe", "pipe", "pipe"],
|
|
|
shell: true
|
|
shell: true
|
|
@@ -771,12 +791,15 @@ var WebContainerServer = class {
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
+ let stdout = "";
|
|
|
|
|
+ let stderr = "";
|
|
|
process2.on("SIGINT", () => {
|
|
process2.on("SIGINT", () => {
|
|
|
console.log(`Process ${process2.pid} received SIGINT`);
|
|
console.log(`Process ${process2.pid} received SIGINT`);
|
|
|
process2.kill("SIGINT");
|
|
process2.kill("SIGINT");
|
|
|
});
|
|
});
|
|
|
process2.stdout.on("data", (data) => {
|
|
process2.stdout.on("data", (data) => {
|
|
|
const output = data.toString();
|
|
const output = data.toString();
|
|
|
|
|
+ stdout += output;
|
|
|
console.log(`[${command}] Process ${process2.pid} stdout:`, output);
|
|
console.log(`[${command}] Process ${process2.pid} stdout:`, output);
|
|
|
this.io.to(socketId).emit("process:output", {
|
|
this.io.to(socketId).emit("process:output", {
|
|
|
type: "stdout",
|
|
type: "stdout",
|
|
@@ -786,6 +809,7 @@ var WebContainerServer = class {
|
|
|
});
|
|
});
|
|
|
process2.stderr.on("data", (data) => {
|
|
process2.stderr.on("data", (data) => {
|
|
|
const output = data.toString();
|
|
const output = data.toString();
|
|
|
|
|
+ stderr += output;
|
|
|
console.error(`[${command}] stderr:`, output);
|
|
console.error(`[${command}] stderr:`, output);
|
|
|
this.io.to(socketId).emit("process:output", {
|
|
this.io.to(socketId).emit("process:output", {
|
|
|
type: "stderr",
|
|
type: "stderr",
|
|
@@ -832,35 +856,35 @@ var WebContainerServer = class {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
async handleList(workspacePath, relativePath) {
|
|
async handleList(workspacePath, relativePath) {
|
|
|
- const fullPath = path__default.default.join(workspacePath, relativePath);
|
|
|
|
|
- const entries = await fs__default.default.readdir(fullPath, { withFileTypes: true });
|
|
|
|
|
|
|
+ const fullPath = import_path.default.join(workspacePath, relativePath);
|
|
|
|
|
+ const entries = await import_promises.default.readdir(fullPath, { withFileTypes: true });
|
|
|
return entries.map((entry) => ({
|
|
return entries.map((entry) => ({
|
|
|
name: entry.name,
|
|
name: entry.name,
|
|
|
type: entry.isDirectory() ? "directory" : "file"
|
|
type: entry.isDirectory() ? "directory" : "file"
|
|
|
}));
|
|
}));
|
|
|
}
|
|
}
|
|
|
async handleRead(workspacePath, relativePath) {
|
|
async handleRead(workspacePath, relativePath) {
|
|
|
- const fullPath = path__default.default.join(workspacePath, relativePath);
|
|
|
|
|
- return fs__default.default.readFile(fullPath, "utf-8");
|
|
|
|
|
|
|
+ const fullPath = import_path.default.join(workspacePath, relativePath);
|
|
|
|
|
+ return import_promises.default.readFile(fullPath, "utf-8");
|
|
|
}
|
|
}
|
|
|
async handleWrite(workspacePath, relativePath, content) {
|
|
async handleWrite(workspacePath, relativePath, content) {
|
|
|
- const fullPath = path__default.default.join(workspacePath, relativePath);
|
|
|
|
|
- await fs__default.default.mkdir(path__default.default.dirname(fullPath), {
|
|
|
|
|
|
|
+ const fullPath = import_path.default.join(workspacePath, relativePath);
|
|
|
|
|
+ await import_promises.default.mkdir(import_path.default.dirname(fullPath), {
|
|
|
recursive: true,
|
|
recursive: true,
|
|
|
mode: FS_CONFIG.DIR_MODE
|
|
mode: FS_CONFIG.DIR_MODE
|
|
|
});
|
|
});
|
|
|
- await fs__default.default.writeFile(fullPath, content, {
|
|
|
|
|
|
|
+ await import_promises.default.writeFile(fullPath, content, {
|
|
|
encoding: FS_CONFIG.ENCODING,
|
|
encoding: FS_CONFIG.ENCODING,
|
|
|
mode: FS_CONFIG.FILE_MODE
|
|
mode: FS_CONFIG.FILE_MODE
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
async handleDelete(workspacePath, relativePath) {
|
|
async handleDelete(workspacePath, relativePath) {
|
|
|
- const fullPath = path__default.default.join(workspacePath, relativePath);
|
|
|
|
|
- await fs__default.default.rm(fullPath, { recursive: true, force: true });
|
|
|
|
|
|
|
+ const fullPath = import_path.default.join(workspacePath, relativePath);
|
|
|
|
|
+ await import_promises.default.rm(fullPath, { recursive: true, force: true });
|
|
|
}
|
|
}
|
|
|
async handleMkdir(workspacePath, relativePath) {
|
|
async handleMkdir(workspacePath, relativePath) {
|
|
|
- const fullPath = path__default.default.join(workspacePath, relativePath);
|
|
|
|
|
- await fs__default.default.mkdir(fullPath, { recursive: true });
|
|
|
|
|
|
|
+ const fullPath = import_path.default.join(workspacePath, relativePath);
|
|
|
|
|
+ await import_promises.default.mkdir(fullPath, { recursive: true });
|
|
|
}
|
|
}
|
|
|
stop() {
|
|
stop() {
|
|
|
return new Promise(async (resolve) => {
|
|
return new Promise(async (resolve) => {
|
|
@@ -881,15 +905,15 @@ var WebContainerServer = class {
|
|
|
const workspaces = Array.from(this.workspaces.values());
|
|
const workspaces = Array.from(this.workspaces.values());
|
|
|
console.log(`\u9700\u8981\u6E05\u7406\u7684\u5DE5\u4F5C\u533A: ${workspaces.join(", ")}`);
|
|
console.log(`\u9700\u8981\u6E05\u7406\u7684\u5DE5\u4F5C\u533A: ${workspaces.join(", ")}`);
|
|
|
try {
|
|
try {
|
|
|
- const rootExists = await fs__default.default.access(this.config.workspaceRoot).then(() => true).catch(() => false);
|
|
|
|
|
|
|
+ const rootExists = await import_promises.default.access(this.config.workspaceRoot).then(() => true).catch(() => false);
|
|
|
if (rootExists) {
|
|
if (rootExists) {
|
|
|
await Promise.all(
|
|
await Promise.all(
|
|
|
workspaces.map(async (workspacePath) => {
|
|
workspaces.map(async (workspacePath) => {
|
|
|
try {
|
|
try {
|
|
|
- const exists = await fs__default.default.access(workspacePath).then(() => true).catch(() => false);
|
|
|
|
|
|
|
+ const exists = await import_promises.default.access(workspacePath).then(() => true).catch(() => false);
|
|
|
if (exists) {
|
|
if (exists) {
|
|
|
console.log(`\u6B63\u5728\u6E05\u7406\u5DE5\u4F5C\u533A: ${workspacePath}`);
|
|
console.log(`\u6B63\u5728\u6E05\u7406\u5DE5\u4F5C\u533A: ${workspacePath}`);
|
|
|
- await fs__default.default.rm(workspacePath, { recursive: true, force: true });
|
|
|
|
|
|
|
+ await import_promises.default.rm(workspacePath, { recursive: true, force: true });
|
|
|
console.log(`\u5DE5\u4F5C\u533A\u5DF2\u6E05\u7406: ${workspacePath}`);
|
|
console.log(`\u5DE5\u4F5C\u533A\u5DF2\u6E05\u7406: ${workspacePath}`);
|
|
|
} else {
|
|
} else {
|
|
|
console.log(`\u5DE5\u4F5C\u533A\u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u6E05\u7406: ${workspacePath}`);
|
|
console.log(`\u5DE5\u4F5C\u533A\u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u6E05\u7406: ${workspacePath}`);
|
|
@@ -900,9 +924,9 @@ var WebContainerServer = class {
|
|
|
})
|
|
})
|
|
|
);
|
|
);
|
|
|
try {
|
|
try {
|
|
|
- const rootContents = await fs__default.default.readdir(this.config.workspaceRoot);
|
|
|
|
|
|
|
+ const rootContents = await import_promises.default.readdir(this.config.workspaceRoot);
|
|
|
if (rootContents.length === 0) {
|
|
if (rootContents.length === 0) {
|
|
|
- await fs__default.default.rm(this.config.workspaceRoot, {
|
|
|
|
|
|
|
+ await import_promises.default.rm(this.config.workspaceRoot, {
|
|
|
recursive: true,
|
|
recursive: true,
|
|
|
force: true
|
|
force: true
|
|
|
});
|
|
});
|
|
@@ -945,7 +969,7 @@ var WebContainerServer = class {
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
await new Promise((resolve, reject) => {
|
|
await new Promise((resolve, reject) => {
|
|
|
- const pkill = child_process.spawn("pkill", ["-TERM", "-P", pid.toString()]);
|
|
|
|
|
|
|
+ const pkill = (0, import_child_process2.spawn)("pkill", ["-TERM", "-P", pid.toString()]);
|
|
|
pkill.on("close", async () => {
|
|
pkill.on("close", async () => {
|
|
|
try {
|
|
try {
|
|
|
process2.kill("SIGINT");
|
|
process2.kill("SIGINT");
|
|
@@ -953,7 +977,7 @@ var WebContainerServer = class {
|
|
|
await new Promise((resolveWait) => {
|
|
await new Promise((resolveWait) => {
|
|
|
const timeout = setTimeout(() => {
|
|
const timeout = setTimeout(() => {
|
|
|
try {
|
|
try {
|
|
|
- child_process.spawn("pkill", ["-KILL", "-P", pid.toString()]);
|
|
|
|
|
|
|
+ (0, import_child_process2.spawn)("pkill", ["-KILL", "-P", pid.toString()]);
|
|
|
process2.kill("SIGKILL");
|
|
process2.kill("SIGKILL");
|
|
|
console.log(`\u8FDB\u7A0B ${pid} \u5DF2\u5F3A\u5236\u7EC8\u6B62`);
|
|
console.log(`\u8FDB\u7A0B ${pid} \u5DF2\u5F3A\u5236\u7EC8\u6B62`);
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
@@ -987,9 +1011,12 @@ var WebContainerServer = class {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
+
|
|
|
|
|
+// src/start.ts
|
|
|
|
|
+var import_path2 = __toESM(require("path"), 1);
|
|
|
var port = parseInt(process.env.PORT || "3000", 10);
|
|
var port = parseInt(process.env.PORT || "3000", 10);
|
|
|
var host = process.env.HOST || "localhost";
|
|
var host = process.env.HOST || "localhost";
|
|
|
-var workspaceRoot = process.env.WORKSPACE_ROOT || path__default.default.join(process.cwd(), "workspaces");
|
|
|
|
|
|
|
+var workspaceRoot = process.env.WORKSPACE_ROOT || import_path2.default.join(process.cwd(), "workspaces");
|
|
|
var server = new WebContainerServer({
|
|
var server = new WebContainerServer({
|
|
|
port,
|
|
port,
|
|
|
host,
|
|
host,
|