Use diskpart to rescan the drives

This commit is contained in:
Juan Cruz Viotti 2015-01-28 14:41:53 -04:00
parent dcebbc0044
commit 8b0ce36955
2 changed files with 36 additions and 22 deletions

View File

@ -1,31 +1,32 @@
os = require('os')
fs = require('fs')
childProcess = require('child_process')
eventStream = require('event-stream')
progressStream = require('progress-stream')
blockAligner = (blockSize) ->
return eventStream.through (chunk) ->
size = chunk.length % blockSize
IS_WINDOWS = os.platform() is 'win32'
if size isnt 0
newChunk = new Buffer(chunk.length + (blockSize - size))
chunk.copy(newChunk)
chunk = newChunk
@emit('data', chunk)
exports.rescanDrives = (callback) ->
return callback() if not IS_WINDOWS
diskpartRescanScriptPath = path.join(__dirname, 'scripts', 'diskpart_rescan')
childProcess.exec "diskpart /s #{diskpartRescanScriptPath}", {}, (error) ->
console.log("DISKPART RESULT: #{arguments}")
return callback(error)
exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) ->
if not fs.existsSync(imagePath)
return callback(new Error("Invalid OS image: #{imagePath}"))
if not fs.existsSync(devicePath)
if not IS_WINDOWS and not fs.existsSync(devicePath)
return callback(new Error("Invalid device: #{devicePath}"))
imageFile = fs.createReadStream(imagePath)
deviceFile = fs.createWriteStream devicePath,
# Required by Windows to work correctly
flags: 'r+'
flags: 'rs+'
imageFileSize = fs.statSync(imagePath).size
@ -36,8 +37,13 @@ exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) ->
if options.progress
progress.on('progress', options.onProgress)
async.waterfall([
(callback) ->
exports.rescanDrives(callback)
(callback) ->
imageFile
.pipe(blockAligner(512))
.pipe(progress)
.pipe(deviceFile)
@ -49,3 +55,10 @@ exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) ->
return callback(error)
.on('close', callback)
(callback) ->
exports.rescanDrives(callback)
], callback)

View File

@ -0,0 +1 @@
rescan