This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
forked from Vince-F/cldr-data-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (92 loc) · 2.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Download tool for Unicode CLDR JSON data
*
* Copyright 2013 Rafael Xavier de Souza
* Released under the MIT license
* https://github.com/rxaviers/cldr-data-downloader/blob/master/LICENSE-MIT
*/
"use strict";
var assert = require("assert");
var AvailableLocales = require("./lib/available_locales");
var download = require("./lib/download");
var isUrl = require("./lib/util").isUrl;
var progress = require("./lib/progress");
var Q = require("q");
var readJSON = require("./lib/util").readJSON;
var State = require("./lib/state");
var unpack = require("./lib/unpack");
Q.longStackSupport = true;
function alwaysArray(arrayOrSomething) {
return Array.isArray(arrayOrSomething) ?
arrayOrSomething :
arrayOrSomething ? [arrayOrSomething] : [];
}
/**
* fn( srcUrl, destPath [, options], callback )
*/
module.exports = function(srcUrl, destPath, options, callback) {
var error, state;
if (callback === undefined && typeof options === "function") {
callback = options;
options = {};
}
assert(typeof srcUrl === "string", "must include srcUrl (e.g., " +
"\"http://www.unicode.org/Public/cldr/26/json.zip\")");
assert(typeof destPath === "string", "must include destPath (e.g., " +
"\"./cldr\")");
assert(typeof options === "object", "invalid options");
assert(typeof callback === "function", "must include callback function");
Q.try(function() {
// Is srcUrl a config file?
if (!isUrl(srcUrl) && (/.json$/i).test(srcUrl)) {
// Read its URL.
options.srcUrlKey = options.srcUrlKey || "core";
srcUrl = readJSON(srcUrl)[options.srcUrlKey];
}
// Is it already installed?
state = new State(srcUrl, destPath);
if (!options.force && state.isInstalled()) {
error = new Error("Already downloaded and unpacked, quitting... Use " +
"`options.force = true` to override.");
error.code = "E_ALREADY_INSTALLED";
throw error;
}
// Download
}).then(function() {
var srcUrls = alwaysArray(srcUrl);
if (options.filterRe) {
var filterRe = options.filterRe;
if (typeof filterRe === "string") {
filterRe = new RegExp(filterRe);
}
srcUrls = srcUrls.filter(function(url) {
return filterRe.test(url);
});
}
return Q.all(srcUrls.map(function(srcUrl) {
return download({
url: srcUrl
});
})).progress(progress(srcUrls.length));
// Unpack
}).then(unpack({
path: destPath
// Generate available locales.
})).then(function() {
try {
new AvailableLocales(destPath).write();
} catch(error) {
error.message = "Error generating available locales. " + error.message;
throw error;
}
// Save installation state.
}).then(function() {
try {
state.write();
} catch(error) {
error.message = "Error saving installation state. " + error.message;
throw error;
}
// Done
}).nodeify(callback);
};