Skip to content

Commit 8b38bad

Browse files
committed
fix: include serialport package in extension, remove loader hack
1 parent 8210ae4 commit 8b38bad

File tree

7 files changed

+385
-85
lines changed

7 files changed

+385
-85
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
out
22
/node_modules
33
/webview/node_modules
4-
/binary_modules/*/node_modules
4+
/binary_modules/**/node_modules
55
*.vsix
66
.vscode-test
77
.DS_Store

.vscodeignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ tsconfig.json
1010
vsc-extension-quickstart.md
1111
webpack.config.js
1212
node_modules
13-
binary_modules/electron*
13+
binary_modules
1414

1515
!node_modules/@vscode/webview-ui-toolkit

package-lock.json

+375
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3005,6 +3005,7 @@
30053005
"prebuild-install": "^7.0.1",
30063006
"ringbufferjs": "^1.1.0",
30073007
"safe-buffer": "^5.2.1",
3008+
"serialport": "^10.4.0",
30083009
"stream-json": "^1.7.3",
30093010
"tmp": "^0.2.1",
30103011
"universal-analytics": "^0.5.3",
@@ -3057,7 +3058,7 @@
30573058
"url": "https://github.com/Marus/cortex-debug/issues"
30583059
},
30593060
"scripts": {
3060-
"vscode:prepublish": "npm run lint && webpack --mode production && node dist/docgen.js",
3061+
"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/",
30613062
"watch": "webpack --mode development --watch",
30623063
"compile": "webpack --mode development && node dist/docgen.js",
30633064
"test-compile": "tsc -p ./",

src/frontend/extension.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ export class CortexDebugExtension {
760760
Reporting.sendEvent('SWO', 'Source', 'File');
761761
}
762762
else if (e.body.type === 'serial') {
763-
mySession.swoSource = new SerialSWOSource(e.body.device, e.body.baudRate, this.context.extensionPath);
763+
mySession.swoSource = new SerialSWOSource(e.body.device, e.body.baudRate);
764764
Reporting.sendEvent('SWO', 'Source', 'Serial');
765765
}
766766

src/frontend/swo/sources/serial.ts

+5-70
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,17 @@
11
import { SWORTTSource } from './common';
22
import { EventEmitter } from 'events';
3-
import * as fs from 'fs';
4-
import * as os from 'os';
3+
import type { SerialPort } from 'serialport';
54
import * as vscode from 'vscode';
6-
import * as path from 'path';
7-
8-
export function findSerialPortModuleHelp(extensionPath: string) {
9-
return 'Node/npm module "serialport" not found. You can install this in one of two ways\n' +
10-
'1. Install "Serial Monitor" VSCode extension. https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-serial-monitor\n' +
11-
'2. or, you can compile the serialport module locally on your computer. Follow these instructions on a shell prompt\n' +
12-
` cd ${extensionPath}/binary_modules\n` +
13-
` bash ./build.sh ${process.versions['electron']}\n` +
14-
'If you chose to compile locally, make sure NodeJS is installed on your system. Visit https://nodejs.org/en/download/';
15-
}
16-
17-
export function findSerialPortModule(extensionPath: string, useModule) {
18-
const paths = [];
19-
const p = path.normalize(path.join(extensionPath, 'binary_modules', 'electron-' + process.versions['electron'], 'node_modules'));
20-
if (fs.existsSync(p) && fs.existsSync(path.join(p, 'serialport'))) {
21-
paths.push(p);
22-
} else {
23-
const serMonitorExt = 'ms-vscode.vscode-serial-monitor';
24-
const serialMonitor: vscode.Extension<any> = vscode.extensions.getExtension(serMonitorExt);
25-
if (serialMonitor) {
26-
paths.push(path.join(serialMonitor.extensionPath, 'dist', 'node_modules'));
27-
paths.push(path.join(serialMonitor.extensionPath, 'node_modules'));
28-
}
29-
}
30-
31-
let added = false;
32-
for (const p of paths) {
33-
if (fs.existsSync(path.join(p, 'serialport'))) {
34-
if (useModule.paths.indexOf(p) === -1) {
35-
console.log(`Adding ${p} to module search path`);
36-
useModule.paths.push(p);
37-
}
38-
added = true;
39-
}
40-
}
41-
return added;
42-
}
43-
44-
declare function __webpack_require__(name: string): any;
45-
declare function __non_webpack_require__(name: string): any;
465

476
export class SerialSWOSource extends EventEmitter implements SWORTTSource {
48-
private serialPort: any = null;
7+
private serialPort: SerialPort = null;
498
public connected: boolean = false;
509

51-
constructor(private device: string, private baudRate: number, extensionPath: string) {
10+
constructor(private device: string, private baudRate: number) {
5211
super();
5312

54-
/* While this is a bit ugly - it works around WebPack's mangling of the require statements. eval('require.main') gets us
55-
the main module in a non-mangled form (instead of the current module - but that is not important for our purposes here)
56-
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
57-
webpack mangling */
58-
// tslint:disable-next-line: no-eval
59-
const mainModule = eval('require.main');
60-
const added = findSerialPortModule(extensionPath, mainModule);
61-
if (!added) {
62-
vscode.window.showErrorMessage(findSerialPortModuleHelp(extensionPath));
63-
return;
64-
}
65-
66-
let SerialPort;
67-
try {
68-
SerialPort = mainModule.require('serialport').SerialPort;
69-
if (!SerialPort) {
70-
vscode.window.showErrorMessage(findSerialPortModuleHelp(extensionPath));
71-
return;
72-
}
73-
}
74-
catch (e) {
75-
vscode.window.showErrorMessage(findSerialPortModuleHelp(extensionPath));
76-
return;
77-
}
78-
79-
this.serialPort = new SerialPort(device, { baudRate: baudRate, autoOpen: false });
13+
const { SerialPort } = require('serialport');
14+
this.serialPort = new SerialPort({ path: device, baudRate: baudRate, autoOpen: false });
8015
this.serialPort.on('data', (buffer) => {
8116
this.emit('data', buffer);
8217
});

test/suite/serialport.test.ts

-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
import * as assert from 'assert';
2-
import * as vscode from 'vscode';
3-
import { findSerialPortModule, findSerialPortModuleHelp } from '../../src/frontend/swo/sources/serial';
42

53
suite('Serial Port tests', () => {
6-
const extensionPath = vscode.extensions.getExtension('marus25.cortex-debug').extensionPath;
7-
const added = findSerialPortModule(extensionPath, module);
8-
// console.log(findSerialPortModuleHelp(extensionPath));
9-
test('Serial Port exists', async () => {
10-
if (!added) {
11-
console.log(findSerialPortModuleHelp(extensionPath));
12-
assert.fail('Could not find serialport module');
13-
}
14-
});
154
test('Serial Port list', async () => {
165
let SerialPort;
176
try {

0 commit comments

Comments
 (0)