cldr-data-downloader
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -15,2 +15,5 @@ /** | ||
var request = require("request"); | ||
var path = require("path"); | ||
var fs = require("fs"); | ||
var workingDir = process.cwd(); | ||
@@ -35,3 +38,68 @@ function getRequestOptions(options) { | ||
function fetchFromFilesystem(src) { | ||
var fsDfd = Q.defer(); | ||
// We don't need any options here, so we just grab the url | ||
// and make sure it fits the norms for our platform. | ||
var givenFilePath = path.normalize(src.url); | ||
function notify(state) { | ||
fsDfd.notify(state); | ||
} | ||
var filePath; | ||
// Test if the file path is absolute. If not, then prepend the application | ||
// directory beforehand, so it can be treated as relative from the app root. | ||
if (path.isAbsolute(givenFilePath)) { | ||
filePath = givenFilePath; | ||
} else { | ||
filePath = path.join(workingDir, givenFilePath); | ||
} | ||
console.log("Retrieving `" + filePath + "`"); | ||
// Getting the errors and size right on the progress bar here is not worth all the setup. | ||
// Just fail silently here if there's a problem and the error will correctly bubble in the | ||
// readFile call below. | ||
var totalSize = 0; | ||
try { | ||
var stats = fs.statSync(filePath); | ||
totalSize = stats.size; | ||
} catch (e) {} | ||
// We're gonna go from 0->100 with nothing between for fs.readFile. We could alternatively | ||
// do a readStream, but it seems like overkill for the filesizes. | ||
notify({total: totalSize, received: 0, percent: 0}); | ||
// Async request the file | ||
fs.readFile(filePath, function(error, fileBody) { | ||
if (error) { | ||
error.message = "Error retrieving file from disk.\n" + error.stack + "\n\n" + | ||
"Please report this full log at " + | ||
"https://github.com/rxaviers/cldr-data-downloader"; | ||
fsDfd.reject(error); | ||
throw error; | ||
} else { | ||
notify({total: totalSize, received: fileBody.length, percent: 100}); | ||
fsDfd.resolve(fileBody); | ||
} | ||
}); | ||
return fsDfd.promise; | ||
} | ||
function download(src) { | ||
src.url = src.url.trim(); | ||
// Modify the url to not be a file protocol uri so we can unify handling files. | ||
src.url = src.url.replace(/^file:\/\//i, ""); | ||
// Short circuit the download function and grab it from the filesystem if | ||
// we don't have anything that looks like a protocol (e.g. https/http/ftp/etc). | ||
if (src.url.indexOf("://") === -1) { | ||
return fetchFromFilesystem(src); | ||
} | ||
var downloadDfd = Q.defer(); | ||
@@ -38,0 +106,0 @@ var options = getRequestOptions(src); |
{ | ||
"name": "cldr-data-downloader", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "unicode", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22518
556
4