Skip to content

Commit 0a05e36

Browse files
committed
Move 'listCoreSerialPorts' to the utils
1 parent e6b576a commit 0a05e36

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/project/tasks.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
* the root directory of this source tree.
77
*/
88

9-
import * as pioNodeHelpers from 'platformio-node-helpers';
10-
119
import { IS_WINDOWS, STATUS_BAR_PRIORITY_START } from '../constants';
10+
import { disposeSubscriptions, listCoreSerialPorts } from '../utils';
1211
import { getProjectItemState, updateProjectItemState } from './helpers';
1312
import ProjectTasksTreeProvider from './task-tree';
14-
import { disposeSubscriptions } from '../utils';
1513
import { extension } from '../main';
1614
import path from 'path';
1715
import vscode from 'vscode';
@@ -275,21 +273,14 @@ export default class ProjectTaskManager {
275273
}
276274

277275
async pickProjectPort() {
278-
const script = `
279-
import json
280-
from platformio.public import list_serial_ports
281-
282-
print(json.dumps(list_serial_ports()))
283-
`;
284-
const output = await pioNodeHelpers.core.getCorePythonCommandOutput(['-c', script]);
285-
const serialPorts = JSON.parse(output.trim());
276+
const serialPorts = await listCoreSerialPorts();
286277
const pickedItem = await vscode.window.showQuickPick(
287278
[
288279
{ label: 'Auto' },
289280
...serialPorts.map((port) => ({
290281
label: port.port,
291282
description: [port.description, port.hwid]
292-
.filter((value) => value !== 'n/a')
283+
.filter((value) => !!value)
293284
.join(' | '),
294285
})),
295286
{ label: 'Custom...' },

src/utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,21 @@ export function getIDEManifest() {
6060
export function getIDEVersion() {
6161
return getIDEManifest().version;
6262
}
63+
64+
export async function listCoreSerialPorts() {
65+
const script = `
66+
import json
67+
from platformio.public import list_serial_ports
68+
69+
print(json.dumps(list_serial_ports()))
70+
`;
71+
const output = await pioNodeHelpers.core.getCorePythonCommandOutput(['-c', script]);
72+
return JSON.parse(output.trim()).map((item) => {
73+
for (const key of ['description', 'hwid']) {
74+
if (item[key] === 'n/a') {
75+
item[key] = undefined;
76+
}
77+
}
78+
return item;
79+
});
80+
}

0 commit comments

Comments
 (0)