pxt-calliope/cmds/cmds.ts

61 lines
2.3 KiB
TypeScript
Raw Normal View History

2016-11-29 09:06:09 +01:00
/// <reference path="../node_modules/pxt-core/typings/node/node.d.ts"/>
/// <reference path="../node_modules/pxt-core/built/pxtlib.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
export function deployCoreAsync(res: ts.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");
return Promise.resolve(0);
2016-03-28 06:22:06 +02:00
}
console.log(`copy ${ts.pxtc.BINARY_HEX} to ` + drives.join(", "));
let writeHexFile = (filename: string) => {
return writeFileAsync(filename + ts.pxtc.BINARY_HEX, res.outfiles[ts.pxtc.BINARY_HEX])
.then(() => console.log("wrote hex file to " + filename));
};
return Promise.map(drives, d => writeHexFile(d))
.then(() => drives.length);
});
2016-03-28 06:22:06 +02:00
}
function getBitDrivesAsync(): Promise<string[]> {
if (process.platform == "win32") {
2016-09-06 09:12:57 +02:00
const 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-09-06 09:12:57 +02:00
const 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-09-06 09:12:57 +02:00
} else if (process.platform == "linux") {
const rx = new RegExp(pxt.appTarget.compile.deployDrives)
const user = process.env["USER"]
return readDirAsync(`/media/${user}`)
.then(lst => lst.filter(s => rx.test(s)).map(s => `/media/${user}/${s}/`))
2016-03-28 06:22:06 +02:00
} else {
return Promise.resolve([])
}
2016-04-07 20:30:22 +02:00
}