build: revert back to downloading cldr-data directly rather than via npm (#39341)

Revert back to downloading cldr-data directly as the npm package seems
to no longer be maintained and additionally, it carries a ~350mb cost
in our node modules that is unnecessarily downloaded by most developers
and on CI.

PR Close #39341
This commit is contained in:
Joey Perrott
2020-10-20 09:01:23 -07:00
committed by Andrew Kushnir
parent 25e14327ea
commit 200b770b85
8 changed files with 237 additions and 99 deletions

View File

@ -6,14 +6,29 @@
* found in the LICENSE file at https://angular.io/license
*/
const path = require('path');
const fs = require('fs');
module.exports = {
extract: gulp => done => {
if (!fs.existsSync(path.join(__dirname, 'cldr/cldr-data'))) {
throw new Error(`You must run "gulp cldr:download" before you can extract the data`);
}
const extract = require('./cldr/extract');
return extract(gulp, done);
},
download: gulp => done => {
const cldrDownloader = require('cldr-data-downloader');
const cldrDataFolder = path.join(__dirname, 'cldr/cldr-data');
if (fs.existsSync(cldrDataFolder)) {
fs.rmdirSync(cldrDataFolder, {recursive: true});
} else {
fs.mkdirSync(cldrDataFolder);
}
cldrDownloader(path.join(__dirname, 'cldr/cldr-urls.json'), cldrDataFolder, {}, done);
},
closure: gulp => done => {
const {RELATIVE_I18N_DATA_FOLDER} = require('./cldr/extract');
// tslint:disable-next-line:no-console

View File

@ -0,0 +1,86 @@
// tslint:disable:file-header
/**
* Npm module for Unicode CLDR JSON data
*
* @license
* Copyright 2013 Rafael Xavier de Souza
* Released under the MIT license
* https://github.com/rxaviers/cldr-data-npm/blob/master/LICENSE-MIT
*/
'use strict';
const JSON_EXTENSION = /^(.*)\.json$/;
const assert = require('assert');
const _fs = require('fs');
const _path = require('path');
function argsToArray(arg) {
return [].slice.call(arg, 0);
}
function jsonFiles(dirName) {
const fileList = _fs.readdirSync(_path.join(__dirname, 'cldr-data', dirName));
return fileList.reduce(function(sum, file) {
if (JSON_EXTENSION.test(file)) {
return sum.concat(file.match(JSON_EXTENSION)[1]);
}
}, []);
}
function cldrData(path /*, ...*/) {
assert(
typeof path === 'string',
'must include path (e.g., "main/en/numbers" or "supplemental/likelySubtags")');
if (arguments.length > 1) {
return argsToArray(arguments).reduce(function(sum, path) {
sum.push(cldrData(path));
return sum;
}, []);
}
return require('./cldr-data/' + path);
}
function mainPathsFor(locales) {
return locales.reduce(function(sum, locale) {
const mainFiles = jsonFiles(_path.join('main', locale));
mainFiles.forEach(function(mainFile) {
sum.push(_path.join('main', locale, mainFile));
});
return sum;
}, []);
}
function supplementalPaths() {
const supplementalFiles = jsonFiles('supplemental');
return supplementalFiles.map(function(supplementalFile) {
return _path.join('supplemental', supplementalFile);
});
}
Object.defineProperty(cldrData, 'availableLocales', {
get: function() {
return cldrData('availableLocales').availableLocales;
}
});
cldrData.all = function() {
const paths = supplementalPaths().concat(mainPathsFor(this.availableLocales));
return cldrData.apply({}, paths);
};
cldrData.entireMainFor = function(locale /*, ...*/) {
assert(typeof locale === 'string', 'must include locale (e.g., "en")');
return cldrData.apply({}, mainPathsFor(argsToArray(arguments)));
};
cldrData.entireSupplemental = function() {
return cldrData.apply({}, supplementalPaths());
};
module.exports = cldrData;

View File

@ -0,0 +1,22 @@
{
"core": [
"https://github.com/unicode-cldr/cldr-core/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-segments-modern/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-dates-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-cal-buddhist-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-cal-chinese-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-cal-coptic-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-cal-dangi-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-cal-ethiopic-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-cal-hebrew-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-cal-indian-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-cal-islamic-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-cal-japanese-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-cal-persian-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-cal-roc-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-localenames-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-misc-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-numbers-full/archive/36.0.0.zip",
"https://github.com/unicode-cldr/cldr-units-full/archive/36.0.0.zip"
]
}

View File

@ -46,7 +46,7 @@ const HEADER = `/**
// tslint:disable:no-console
module.exports = (gulp, done) => {
const cldrData = require('cldr-data');
const cldrData = require('./cldr-data');
const LOCALES = cldrData.availableLocales;
console.log(`Loading CLDR data...`);