This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
forked from rxaviers/cldr-data-npm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
127 lines (113 loc) · 3.69 KB
/
install.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Npm module for Unicode CLDR JSON data
*
* Copyright 2013 Rafael Xavier de Souza
* Released under the MIT license
* https://github.com/rxaviers/cldr-data-npm/blob/master/LICENSE-MIT
*/
"use strict";
var coverage, parentPackage, peerPackages, srcUrl;
var cldrDownloader = require("cldr-data-downloader");
var glob = require("glob").sync;
var path = require("path");
var child_process = require('child_process');
var options = {};
var isNpm3;
try {
var npmv = child_process.execSync('npm -v').toString('utf8');
isNpm3 = (npmv.split('.')[0] == '3');
} catch(error) {
// child_process.execSync is not available on Node v0.10
// fortunately, we can use ENV variables set by npm do detect its version
// npm_config_user_agent: 'npm/2.15.1 node/v0.10.46 darwin x64'
// npm_config_user_agent: 'npm/3.10.3 node/v6.3.0 darwin x64'
// Note that users can override the value of this config option,
// therefore it's safer to use this method only as a fall-back option.
if (/^npm\/2\./.test(process.env.npm_config_user_agent)) {
isNpm3 = false;
} else {
// Better safe than sorry.
isNpm3 = true;
}
}
try {
parentPackage = require("../../package.json");
} catch(error) {}
try {
peerPackages = glob("../*/package.json").map(function(file) {
try {
return require(path.resolve(file));
} catch(error) {
return {};
}
});
} catch(error) {
console.error(
"Warning: Something weird happened checking whether this is a " +
"peer dependency.", error.message
);
peerPackages = [];
}
if (!isNpm3 && parentPackage &&
!(parentPackage.dependencies && parentPackage.dependencies["cldr-data"]) &&
!(parentPackage.devDependencies && parentPackage.devDependencies["cldr-data"]) &&
peerPackages.some(function(peerPackage) {
return peerPackage.peerDependencies &&
peerPackage.peerDependencies["cldr-data"];
})) {
console.error(
"Warning: Skipping to download CLDR data, because `cldr-data` is a " +
"peer dependency. If you want it to be downloaded, make sure it's " +
"listed under `dependencies` or `devDependencies` of the `package.json`" +
"of your application."
);
return process.exit(0);
}
if (process.env.CLDR_URL) {
console.warn("CLDR_URL is deprecated, use CLDR_DATA_URLS_JSON instead.")
srcUrl = srcUrl.replace(
"http://www.unicode.org/Public/cldr",
process.env.CLDR_URL.replace(/\/$/, "")
);
} else {
if (process.env.CLDR_DATA_URLS_JSON) {
srcUrl = process.env.CLDR_DATA_URLS_JSON;
} else if (parentPackage && parentPackage["cldr-data-urls-json"]) {
srcUrl = parentPackage["cldr-data-urls-json"];
} else {
srcUrl = path.join(__dirname, "./urls.json");
}
}
if (process.env.CLDR_COVERAGE) {
coverage = process.env.CLDR_COVERAGE;
} else if (parentPackage && parentPackage["cldr-data-coverage"] && (
(parentPackage.dependencies && parentPackage.dependencies["cldr-data"]) ||
(parentPackage.devDependencies && parentPackage.devDependencies["cldr-data"])
)) {
coverage = parentPackage["cldr-data-coverage"];
}
if (process.env.CLDR_DATA_URLS_FILTER) {
options.filterRe = process.env.CLDR_DATA_URLS_FILTER;
} else if (parentPackage && parentPackage["cldr-data-urls-filter"]) {
options.filterRe = parentPackage["cldr-data-urls-filter"];
}
if (coverage) {
options.srcUrlKey = coverage;
}
cldrDownloader(
srcUrl,
__dirname,
options,
function(error) {
if (error) {
if (/E_ALREADY_INSTALLED/.test(error.code)) {
error.message = error.message.replace(/Use `options.*/, "Use -f to " +
"override.");
return console.log(error.message);
} else {
console.error("Whops", error.message);
process.exit(1);
}
}
}
);