pxt-calliope/cmds/cmds.ts

52 lines
1.9 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-06-24 08:55:36 +02:00
let readDirAsync = Promise.promisify(fs.readdir)
2016-03-28 06:22:06 +02:00
2016-08-26 15:19:04 +02:00
export function deployCoreAsync(res: pxtc.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-08-26 15:19:04 +02:00
console.log(`copy ${pxtc.BINARY_HEX} to ` + drives.join(", "))
2016-03-28 06:22:06 +02:00
}
return Promise.map(drives, d =>
2016-08-26 15:19:04 +02:00
writeFileAsync(d + pxtc.BINARY_HEX, res.outfiles[pxtc.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-07-28 19:22:05 +02:00
let rx = new RegExp("^([A-Z]:).* " + pxt.appTarget.compile.deployDrives)
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 => {
2016-07-28 19:22:05 +02:00
let m = rx.exec(ln)
2016-03-28 06:22:06 +02:00
if (m) {
res.push(m[1] + "/")
}
})
return res
})
2016-06-24 08:55:36 +02:00
}
else if (process.platform == "darwin") {
2016-07-28 19:22:05 +02:00
let rx = new RegExp(pxt.appTarget.compile.deployDrives)
2016-06-24 08:55:36 +02:00
return readDirAsync("/Volumes")
2016-07-28 19:22:05 +02:00
.then(lst => lst.filter(s => rx.test(s)).map(s => "/Volumes/" + s + "/"))
2016-03-28 06:22:06 +02:00
} else {
return Promise.resolve([])
}
2016-04-07 20:30:22 +02:00
}