Skip to content

Commit

Permalink
add experimental new-install command
Browse files Browse the repository at this point in the history
  • Loading branch information
cubicap committed Oct 15, 2023
1 parent d89c431 commit 85e2754
Show file tree
Hide file tree
Showing 9 changed files with 887 additions and 1 deletion.
241 changes: 241 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"clean": "tsc --build --clean",
"lint": "eslint --ext .ts src unit",
"test": "mocha",
"prepack": "npm run lint && npm run clean && npm run build && npm run test"
"prepack": "npm run lint && npm run clean && npm run build && npm run test",
"command": "node ./dist/src/commands/index.js"
},
"bin": {
"jac": "./dist/src/commands/index.js"
Expand All @@ -33,8 +34,11 @@
"chalk": "^5.3.0",
"cli-progress": "^3.12.0",
"crc": "^4.3.2",
"esptool-js": "https://github.com/cubicap/esptool-js/archive/d096cbfa6a8a615cb71ad24941b2b83186c53495.tar.gz",
"get-uri": "^6.0.1",
"node-stream-zip": "^1.15.0",
"serialport": "^12.0.0",
"tar-stream": "^3.1.6",
"typescript": "^5.2.2",
"winston": "^3.10.0"
},
Expand All @@ -44,6 +48,7 @@
"@types/cli-progress": "^3.11.3",
"@types/mocha": "^10.0.2",
"@types/node": "^18.18.1",
"@types/tar-stream": "^2.2.2",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"@typescript-eslint/parser": "^6.7.3",
"chai": "^4.3.10",
Expand Down
3 changes: 3 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import version from "./version.js";
import monitor from "./monitor.js";
import pull from "./pull.js";
import fomat from "./format.js";
import newInstall from "./new-install.js";

jac.addCommand("list-ports", listPorts);
jac.addCommand("serial-socket", serialSocket);
Expand All @@ -86,6 +87,8 @@ jac.addCommand("stop", stop);
jac.addCommand("status", status);
jac.addCommand("version", version);

jac.addCommand("new-install", newInstall);

jac.addCommand("monitor", monitor);


Expand Down
35 changes: 35 additions & 0 deletions src/commands/new-install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Command, Opt } from "./lib/command.js";

import { loadPackage } from "../distribution/package.js";
import { stderr, stdout } from "process";


const cmd = new Command("Test new install command", {
action: async (options: Record<string, string | boolean>) => {
const pkgPath = options["package"] as string;
const port = options["port"] as string;

if (!port) {
stderr.write("Port not specified\n");
throw 1;
}

stdout.write("Loading package...\n");

const pkg = await loadPackage(pkgPath);

stdout.write("Version: " + pkg.getManifest().getVersion() + "\n");
stdout.write("Board: " + pkg.getManifest().getBoard() + "\n");
stdout.write("Platform: " + pkg.getManifest().getPlatform() + "\n");
stdout.write("\n");

await pkg.flash(port);
},
args: [],
options: {
"package": new Opt("Uri to the package file", { required: true })
},
chainable: false
});

export default cmd;
4 changes: 4 additions & 0 deletions src/commands/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"pull.ts",
"format.ts",
"index.ts",
"new-install.ts",
"lib/command.ts",
],
"references": [
Expand All @@ -36,6 +37,9 @@
},
{
"path": "../project"
},
{
"path": "../distribution"
}
]
}
145 changes: 145 additions & 0 deletions src/distribution/esp32/esp32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { Package } from "../package.js";
import { ESPLoader } from "esptool-js";
import { SerialPort } from "serialport";
import { NodeTransport } from "./nodeTransport.js";
import { logger } from "../../util/logger.js";
import cliProgress from "cli-progress";
import { stdout } from "process";


class UploadReporter {
private bar: cliProgress.SingleBar;
private fileIndex: number;

constructor(private files: { size: number, name: string }[]) {
this.fileIndex = 0;
this.bar = this.createBar(this.fileIndex);
}

private createBar(fileIndex: number) {
const fileName = this.files[fileIndex].name;

return new cliProgress.SingleBar({
format: `${fileIndex + 1}/${this.files.length} | {bar} {percentage}% | ${fileName} | {value} / {total}`,
hideCursor: true
}, cliProgress.Presets.rect);
}

public update(fileIndex: number, written: number) {
if (fileIndex !== this.fileIndex) {
this.bar.update(this.bar.getTotal());
this.bar.stop();
this.fileIndex = fileIndex;
this.bar = this.createBar(this.fileIndex);
this.bar.start(this.files[this.fileIndex].size, 0);
}

this.bar.update(written);
}

public start () {
this.bar.start(this.files[this.fileIndex].size, 0);
}

public stop() {
this.bar.update(this.bar.getTotal());
this.bar.stop();
}
}


export async function flash(Package: Package, path: string): Promise<void> {
const config = Package.getManifest().getConfig();

const flashBaud = parseInt(config["flashBaud"] ?? 921600);

const partitions = config["partitions"];
if (!partitions) {
throw new Error("No partitions defined");
}

const list = await SerialPort.list();
const info = list.find((port) => port.path === path);
if (!info) {
throw new Error("Port not found");
}

stdout.write("Connecting to " + info.path + "...\n");

const port = new SerialPort({
path: info.path,
baudRate: 115200,
autoOpen: false
});

const loaderOptions: any = {
debugLogging: false,
transport: new NodeTransport(port),
baudrate: flashBaud,
romBaudrate: 115200,
terminal: {
clean: () => { },
writeLine: (data: any) => { logger.debug(data); },
write: (data: any) => { logger.debug(data); }
}
};
const esploader = new ESPLoader(loaderOptions);

const fileArray: { data: string, address: number, fileName: string }[] = [];
for (const partition of partitions) {
const file = partition["file"];
if (file === undefined) {
throw new Error("No file defined for partition");
}
const address = parseInt(partition["address"]);
if (address === undefined) {
throw new Error("No address defined for partition");
}
const dataBuffer = Package.getData()[file];
if (dataBuffer === undefined) {
throw new Error("File not found in package");
}

fileArray.push({ data: esploader.ui8ToBstr(dataBuffer), address: address, fileName: file });
}

const reporter = new UploadReporter(fileArray.map((file) => {
return {
size: file.data.length,
name: file.fileName
};
}));

try {
await esploader.main_fn();

stdout.write("Detected chip type: " + esploader.chip.CHIP_NAME + "\n");
stdout.write("Flash size: " + await esploader.get_flash_size() + "K\n");

stdout.write("\n");

if (esploader.chip.CHIP_NAME !== config["chip"]) {
throw new Error("Chip type mismatch (expected " + config["chip"] + ", got " + esploader.chip.CHIP_NAME + ")");
}

stdout.write("Writing flash...\n");

reporter.start();
await esploader.write_flash({
fileArray: fileArray,
flashSize: "4MB",
flashMode: "keep",
flashFreq: "keep",
eraseAll: false,
compress: true,
reportProgress: (fileIndex: number, written: number) => {
reporter.update(fileIndex, written);
}
});
}
finally {
reporter.stop();
await esploader.hard_reset();
await esploader.transport.disconnect();
}
}
Loading

0 comments on commit 85e2754

Please sign in to comment.