pxt-calliope/cmds/cmds.ts

44 lines
1.5 KiB
TypeScript
Raw Normal View History

2016-04-07 20:30:22 +02:00
/// <reference path="../node_modules/pxt-core/built/pxt.d.ts"/>
2016-03-28 06:22:06 +02:00
2016-05-24 23:06:25 +02:00
import * as fs from "fs";
import * as path from "path";
import * as child_process from "child_process";
2016-03-28 06:22:06 +02:00
let writeFileAsync: any = Promise.promisify(fs.writeFile)
let execAsync: (cmd: string, options?: { cwd?: string }) => Promise<Buffer> = Promise.promisify(child_process.exec)
2016-04-07 20:30:22 +02:00
export function deployCoreAsync(res: ts.pxt.CompileResult) {
2016-03-28 06:22:06 +02:00
return getBitDrivesAsync()
.then(drives => {
if (drives.length == 0) {
console.log("cannot find any drives to deploy to")
} else {
2016-05-24 23:06:25 +02:00
console.log(`copy ${ts.pxt.BINARY_HEX} to ` + drives.join(", "))
2016-03-28 06:22:06 +02:00
}
return Promise.map(drives, d =>
2016-05-24 23:06:25 +02:00
writeFileAsync(d + ts.pxt.BINARY_HEX, res.outfiles[ts.pxt.BINARY_HEX])
2016-03-28 06:22:06 +02:00
.then(() => {
console.log("wrote hex file to " + d)
}))
})
.then(() => { })
}
function getBitDrivesAsync(): Promise<string[]> {
if (process.platform == "win32") {
2016-03-28 06:53:38 +02:00
return execAsync("wmic PATH Win32_LogicalDisk get DeviceID, VolumeName, FileSystem")
2016-03-28 06:22:06 +02:00
.then(buf => {
let res: string[] = []
buf.toString("utf8").split(/\n/).forEach(ln => {
let m = /^([A-Z]:).* MICROBIT/.exec(ln)
if (m) {
res.push(m[1] + "/")
}
})
return res
})
} else {
return Promise.resolve([])
}
2016-04-07 20:30:22 +02:00
}