-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
Β·161 lines (139 loc) Β· 5.85 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env node
import { program } from 'commander'; // Import all exports as 'program'
import fs from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import { PackageManager } from './logic/package-manager.js';
import { DeviceManager } from './logic/board/device-manager.js';
import { printPackagesWithHighlights } from './logic/format.js';
import { isCustomPackage } from 'upy-packager';
import inquirer from 'inquirer';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Extract the command version from the package.json file
const version = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'))).version;
const ARDUINO_VID = 0x2341;
const packageManager = new PackageManager();
const deviceManager = new DeviceManager();
/**
* Prompt the user to select a board from the list of connected devices.
* @returns {Promise<SerialDevice>} The selected board
*/
async function promptForBoard(boards) {
const boardOptions = boards.map((board) => {
return { "name": `${board.manufacturer} ${board.name} at ${board.serialPort}`, "value": board };
});
const selection = await inquirer.prompt([
{ type: 'list', name: 'selectedPort', message: 'Please select the desired board:', choices: boardOptions }
]);
const selectedPort = selection.selectedPort; // Use port as unique identifier
return boards.find(board => board.serialPort === selectedPort);
}
async function installPackage(packageName, selectedBoard) {
console.log(`π¦ Installing ${packageName} on '${selectedBoard.name}' (SN: ${selectedBoard.serialNumber})`);
// If it's a package with a custom URL, install it directly
if (isCustomPackage(packageName)) {
await packageManager.installPackageFromURL(packageName, selectedBoard);
return;
}
// Extract the package version from the package name if it's provided.
// e.g. '[email protected]' -> 'v1.3.3'
const packageVersion = packageName.split("@")[1];
const packageNameWithoutVersion = packageName.split("@")[0];
// Get the package from the registry using the package name and version
// WARNING: This requires the registry to have versioned packages
const aPackage = await packageManager.getPackage(packageNameWithoutVersion, packageVersion);
if (aPackage.required_runtime) {
const boardRuntime = await deviceManager.getMicroPythonVersion(selectedBoard);
const matchesRequirement = await packageManager.matchesRequiredRuntime(aPackage, boardRuntime);
if (!matchesRequirement) {
console.warn(`π¨ Package '${aPackage.name}' requires a different runtime version (${aPackage.required_runtime}) than the one running on the board (${boardRuntime}).`);
const response = await inquirer.prompt([
{ type: 'confirm', name: 'continue', message: 'Do you want to continue with the installation?', default: false }
]);
if (!response.continue) {
console.error(`π
Installation of '${packageName}' skipped.`);
return;
}
} else {
console.log(`π Board runtime version ${boardRuntime} is compatible with the package requirements.`);
}
}
await packageManager.installPackage(aPackage, selectedBoard);
}
// Main function to handle the command-line interface
export async function main() {
program
.version(version)
.description('upy-package - A command-line tool to install MicroPython packages on Arduino boards');
program
.command('list')
.description('List packages from registries')
.action(async () => {
try {
const packageList = await packageManager.getPackageList();
const packages = packageList ? packageList.map((pkg) => `π¦ ${pkg.name}`).join('\n') : null;
if (packages && packages.length > 0) {
console.log(packages);
} else {
console.log("π€· No packages found.")
}
} catch (error) {
console.error(error.message);
process.exit(1);
}
});
program
.command('info <package>')
.description('Get information about a package')
.action(async (packageName) => {
const packageInfo = (await packageManager.getPackage(packageName)).toString();
if (packageInfo) {
console.log(packageInfo);
} else {
console.log(`π€· No package found with name ${packageName}`);
}
});
program
.command('find <pattern>')
.description('Find packages using the supplied search pattern')
.action(async (pattern) => {
const packages = await packageManager.findPackages(pattern);
printPackagesWithHighlights(packages, pattern);
});
program
.command('install')
.argument('<package-names...>', 'Package names or URLs to install')
// .option('--path <target-path>', 'Target path to install the package(s) to')
.option('--debug', 'Enable debug output')
.description('Install MicroPython packages on a connected Arduino board')
.action(async (packageNames, options) => {
const connectedDevices = await deviceManager.getConnectedDevices(ARDUINO_VID);
let selectedBoard;
if(connectedDevices.length === 0) {
console.error(`π€· No connected Arduino board found. Please connect a board and try again.`);
process.exit(1);
} else if(connectedDevices.length === 1) {
selectedBoard = connectedDevices[0];
} else {
selectedBoard = await promptForBoard(connectedDevices);
}
try {
for(const packageName of packageNames) {
await installPackage(packageName, selectedBoard);
}
console.log(`β
Done.`);
} catch (error) {
console.error(`β ${error.message}`);
if (options.debug) {
console.error(error.stack);
}
process.exit(1);
}
});
program.parse(process.argv);
if (program.args.length === 0) {
program.help(); // Display help if no command is provided
}
}
main();