Skip to content

Commit

Permalink
fix: include serialport package in extension, remove loader hack
Browse files Browse the repository at this point in the history
  • Loading branch information
ssimek committed Oct 7, 2024
1 parent 8210ae4 commit 8b38bad
Show file tree
Hide file tree
Showing 7 changed files with 385 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
out
/node_modules
/webview/node_modules
/binary_modules/*/node_modules
/binary_modules/**/node_modules
*.vsix
.vscode-test
.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ tsconfig.json
vsc-extension-quickstart.md
webpack.config.js
node_modules
binary_modules/electron*
binary_modules

!node_modules/@vscode/webview-ui-toolkit
375 changes: 375 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3005,6 +3005,7 @@
"prebuild-install": "^7.0.1",
"ringbufferjs": "^1.1.0",
"safe-buffer": "^5.2.1",
"serialport": "^10.4.0",
"stream-json": "^1.7.3",
"tmp": "^0.2.1",
"universal-analytics": "^0.5.3",
Expand Down Expand Up @@ -3057,7 +3058,7 @@
"url": "https://github.com/Marus/cortex-debug/issues"
},
"scripts": {
"vscode:prepublish": "npm run lint && webpack --mode production && node dist/docgen.js",
"vscode:prepublish": "npm run lint && webpack --mode production && node dist/docgen.js && npm ci -C binary_modules --omit dev && cp -a binary_modules/node_modules dist/",
"watch": "webpack --mode development --watch",
"compile": "webpack --mode development && node dist/docgen.js",
"test-compile": "tsc -p ./",
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ export class CortexDebugExtension {
Reporting.sendEvent('SWO', 'Source', 'File');
}
else if (e.body.type === 'serial') {
mySession.swoSource = new SerialSWOSource(e.body.device, e.body.baudRate, this.context.extensionPath);
mySession.swoSource = new SerialSWOSource(e.body.device, e.body.baudRate);
Reporting.sendEvent('SWO', 'Source', 'Serial');
}

Expand Down
75 changes: 5 additions & 70 deletions src/frontend/swo/sources/serial.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,17 @@
import { SWORTTSource } from './common';
import { EventEmitter } from 'events';
import * as fs from 'fs';
import * as os from 'os';
import type { SerialPort } from 'serialport';
import * as vscode from 'vscode';
import * as path from 'path';

export function findSerialPortModuleHelp(extensionPath: string) {
return 'Node/npm module "serialport" not found. You can install this in one of two ways\n' +
'1. Install "Serial Monitor" VSCode extension. https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-serial-monitor\n' +
'2. or, you can compile the serialport module locally on your computer. Follow these instructions on a shell prompt\n' +
` cd ${extensionPath}/binary_modules\n` +
` bash ./build.sh ${process.versions['electron']}\n` +
'If you chose to compile locally, make sure NodeJS is installed on your system. Visit https://nodejs.org/en/download/';
}

export function findSerialPortModule(extensionPath: string, useModule) {
const paths = [];
const p = path.normalize(path.join(extensionPath, 'binary_modules', 'electron-' + process.versions['electron'], 'node_modules'));
if (fs.existsSync(p) && fs.existsSync(path.join(p, 'serialport'))) {
paths.push(p);
} else {
const serMonitorExt = 'ms-vscode.vscode-serial-monitor';
const serialMonitor: vscode.Extension<any> = vscode.extensions.getExtension(serMonitorExt);
if (serialMonitor) {
paths.push(path.join(serialMonitor.extensionPath, 'dist', 'node_modules'));
paths.push(path.join(serialMonitor.extensionPath, 'node_modules'));
}
}

let added = false;
for (const p of paths) {
if (fs.existsSync(path.join(p, 'serialport'))) {
if (useModule.paths.indexOf(p) === -1) {
console.log(`Adding ${p} to module search path`);
useModule.paths.push(p);
}
added = true;
}
}
return added;
}

declare function __webpack_require__(name: string): any;
declare function __non_webpack_require__(name: string): any;

export class SerialSWOSource extends EventEmitter implements SWORTTSource {
private serialPort: any = null;
private serialPort: SerialPort = null;
public connected: boolean = false;

constructor(private device: string, private baudRate: number, extensionPath: string) {
constructor(private device: string, private baudRate: number) {
super();

/* While this is a bit ugly - it works around WebPack's mangling of the require statements. eval('require.main') gets us
the main module in a non-mangled form (instead of the current module - but that is not important for our purposes here)
and allows us to modify the paths and load in the serial port from there. We have to wrap it in an eval statement to avoid
webpack mangling */
// tslint:disable-next-line: no-eval
const mainModule = eval('require.main');
const added = findSerialPortModule(extensionPath, mainModule);
if (!added) {
vscode.window.showErrorMessage(findSerialPortModuleHelp(extensionPath));
return;
}

let SerialPort;
try {
SerialPort = mainModule.require('serialport').SerialPort;
if (!SerialPort) {
vscode.window.showErrorMessage(findSerialPortModuleHelp(extensionPath));
return;
}
}
catch (e) {
vscode.window.showErrorMessage(findSerialPortModuleHelp(extensionPath));
return;
}

this.serialPort = new SerialPort(device, { baudRate: baudRate, autoOpen: false });
const { SerialPort } = require('serialport');
this.serialPort = new SerialPort({ path: device, baudRate: baudRate, autoOpen: false });
this.serialPort.on('data', (buffer) => {
this.emit('data', buffer);
});
Expand Down
11 changes: 0 additions & 11 deletions test/suite/serialport.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import * as assert from 'assert';
import * as vscode from 'vscode';
import { findSerialPortModule, findSerialPortModuleHelp } from '../../src/frontend/swo/sources/serial';

suite('Serial Port tests', () => {
const extensionPath = vscode.extensions.getExtension('marus25.cortex-debug').extensionPath;
const added = findSerialPortModule(extensionPath, module);
// console.log(findSerialPortModuleHelp(extensionPath));
test('Serial Port exists', async () => {
if (!added) {
console.log(findSerialPortModuleHelp(extensionPath));
assert.fail('Could not find serialport module');
}
});
test('Serial Port list', async () => {
let SerialPort;
try {
Expand Down

0 comments on commit 8b38bad

Please sign in to comment.