| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- const { fork } = require('child_process');
- const fs = require('fs');
- const path = require('path');
- const WebSocket = require('ws'); // 引入 WebSocket 库
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Initialize codes.
- AutoLoadPlugins();
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //You can load the plugins in global field, if the next line codes are uncommented.
- //AutoLoadGlobalPlugins();
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Sample codes.
- let ws_global = null; // 用于保存 WebSocket 实例,全局可访问
- // 暴露变量,由 callback.js 负责注册监听
- exports.OnMonacoSaveRequest = null; // 实现保存时的回调
- exports.OnMonacoOpenRequest = null; // 实现打开时的回调
- exports.StartupMonacoEdit = function (cb) {
- // 启动 monaco 的 Node.js 服务端(server.js)
- fork(path.join(unit.dir, 'monaco', 'server.js'))
- .on('message', msg => {
- if (msg.type === 'ready') { cb(); }
- });
- // 启动 WebSocket 服务器,监听 3999 端口
- const wss = new WebSocket.Server({ port: 3999 });
- // 当前端连接建立时的回调
- wss.on('connection', ws => {
- ws_global = ws;
- console.log("WebSocket 连接成功");
- // 监听来自前端的消息
- ws.on('message', msg => {
- try {
- const data = JSON.parse(msg); // 尝试解析消息内容为 JSON 对象
- if (data.type === 'save') {
- // 收到保存请求,调用外部定义的保存回调函数
- exports.OnMonacoSaveRequest(data.content, filepath => {
- // 执行实际文件保存操作
- fs.writeFileSync(filepath, data.content, 'utf-8');
- // 将保存结果通过 WebSocket 返回给前端
- ws.send(JSON.stringify({
- type: 'save_success',
- filename: path.basename(filepath), // 提取文件名
- fullpath: filepath // 完整路径
- }));
- });
- } else if (data.type === 'open') {
- // 收到打开请求,调用外部定义的打开回调函数
- exports.OnMonacoOpenRequest(filepath => {
- // 读取文件内容
- const content = fs.readFileSync(filepath, 'utf-8');
- // 将内容通过 WebSocket 发送给前端
- ws.send(JSON.stringify({
- type: 'open_success',
- filename: path.basename(filepath),
- fullpath: filepath,
- content: content
- }));
- });
- }
- } catch (err) {
- // 捕获解析或处理过程中的异常
- console.error("WebSocket 消息解析失败:", err);
- }
- });
- });
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Put you codes here
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // AutoLoadPlugins Function Implement Start.
- function extractPluginName(filename) {
- const regex = /js_ext_comx_(.*?)-(native)/;
- const match = filename.match(regex);
- if (match) {
- return match[1];
- }
- return null;
- }
- function isSysPluginName(filename) {
- const knownPlugins = ['kul', 'qt', 'qtshell', 'resource', 'mutex', 'encrypt', 'encryptshell'];
- return knownPlugins.includes(extractPluginName(filename));
- }
- function requirePlugin(filedir) {
- const cache = {};
- requirePlugin = function(filedir) {
- const filename = path.basename(filedir);
-
- if(isSysPluginName(filename)){return;}
- if (cache[filename]) {
- //console.log(cache[filename], 'has been loaded');
- } else {
- cache[filename] = filedir;
- require(filedir);
- }
- };
-
- requirePlugin(filedir);
- }
- function AutoLoadPlugins() {
- var plugin_dir = (__dirname + '/../addon/');
- if (!fs.existsSync(plugin_dir)) {
- return;
- }
-
- var files = fs.readdirSync(plugin_dir);
- files.forEach(function(filename) {
- var filedir = path.join(plugin_dir, filename);
- var stats = fs.statSync(filedir);
- if (!stats.isDirectory()) {
- if (filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') {
- requirePlugin(filedir);
- }
-
- if (filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') {
- requirePlugin(filedir);
- }
- if (filedir.indexOf('-darwin.node') !== -1 && require('os').platform() === 'darwin') {
- requirePlugin(filedir);
- }
- }
- });
- }
- function AutoLoadGlobalPlugins() {
- var plugin_dir = (process.env.COMX_SDK + 'addon/');
- if (!fs.existsSync(plugin_dir)) {
- return;
- }
-
- var files = fs.readdirSync(plugin_dir);
- files.forEach(function(filename) {
- var filedir = path.join(plugin_dir, filename);
- var stats = fs.statSync(filedir);
- if (!stats.isDirectory()) {
- if (filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') {
- requirePlugin(filedir);
- }
-
- if (filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') {
- requirePlugin(filedir);
- }
- if (filedir.indexOf('-darwin.node') !== -1 && require('os').platform() === 'darwin') {
- requirePlugin(filedir);
- }
- }
- });
- }
- //AutoLoadPlugins Function Implement End.
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // ide_info Function Implement Start.
- exports.ide_info = (msg) => {
- if (process.send) {
- process.send({
- type: 'debug',
- info: msg
- });
- }
- };
- //ide_info Function Implement End.
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|