Skip to content

Commit 3ea0c1b

Browse files
committed
feat: Pull from GitHub instead of npm
- fetch from ICU’s GitHub release instead of npm (ICU v50+) - set env FULL_ICU_PREFER_NPM to prefer npm instead - add .eslint
1 parent 72ddf0c commit 3ea0c1b

File tree

13 files changed

+251
-4
lines changed

13 files changed

+251
-4
lines changed

.eslintrc.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
"env": {
3+
"commonjs": true,
4+
"es6": true,
5+
"node": true
6+
},
7+
"extends": "eslint:recommended",
8+
"globals": {
9+
"Atomics": "readonly",
10+
"SharedArrayBuffer": "readonly"
11+
},
12+
"parserOptions": {
13+
"ecmaVersion": 2018
14+
},
15+
"rules": {
16+
}
17+
};

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules
22
*.dat
33
npm-debug.log
44
/yarn.lock
5+
package-lock.json
6+
/.nyc_output

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
node_modules
33
.svn
44
.git
5-
npm-debug.log
5+
npm-debug.log
6+
/test

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: node_js
2+
node_js:
3+
- '8'
4+
- '10'
5+
- '11'
6+
- '12'
7+
script:
8+
- npm install
9+
- npm t
10+
cache:
11+
directories:
12+
- node_modules
13+
- ".nvm"
14+
# this is a comment

install-gh.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (C) 2015-2016 IBM Corporation and Others. All Rights Reserved.
2+
3+
// Install by using spawn
4+
5+
// var fs = require('fs');
6+
const {URL} = require('url');
7+
const process = require('process');
8+
const myFetch = require('./myFetch');
9+
// const yauzl = require('yauzl');
10+
11+
// var isglobal = process.env.npm_config_global === 'true';
12+
13+
module.exports = async function installFromGithub(fullIcu, advice) {
14+
var icupkg = fullIcu.icupkg;
15+
var icudat = fullIcu.icudat;
16+
17+
// var cmdPath = nodePath = process.env.npm_node_execpath;
18+
19+
// var npmPath = process.env.npm_execpath;
20+
21+
// var args;
22+
// https://github.com/unicode-org/icu/releases/download/release-51-3/icu4c-51_3-src.zip
23+
const _baseUrl = process.env.FULL_ICU_BASEURL || 'https://github.com/unicode-org/icu/releases/';
24+
const baseUrl = new URL(_baseUrl);
25+
const versionsAsHyphen = fullIcu.icuver.replace(/\./g, '-');
26+
const versionsAsUnderscore = fullIcu.icuver.replace(/\./g, '_');
27+
const tag = `release-${versionsAsHyphen}`;
28+
const file = `icu4c-${versionsAsUnderscore}-src.zip`;
29+
const fullUrl = new URL(`./download/${tag}/${file}`, baseUrl);
30+
console.log(fullUrl.toString());
31+
const [srcZip, tmpd] = await myFetch(fullUrl);
32+
33+
console.log(srcZip, tmpd);
34+
// now, unpack it
35+
36+
/*
37+
if(spawned.error) {
38+
throw(spawned.error);
39+
} else if(spawned.status !== 0) {
40+
throw(Error(cmdPath + ' ' + args.join(' ') + ' --> status ' + spawned.status));
41+
} else {
42+
var datPath;
43+
if(fs.existsSync(icudat)) {
44+
console.log(' √ ' + icudat + " (existing link?)");
45+
} else if(!fs.existsSync(datPath)) {
46+
console.log(' • ' + ' (no ' + icudat + ' at ‘' + datPath+'’)');
47+
} else {
48+
try {
49+
fs.linkSync(datPath, icudat);
50+
console.log(' √ ' + icudat + " (link)");
51+
} catch(e) {
52+
fs.symlinkSync(datPath, icudat);
53+
console.log(' √ ' + icudat + " (symlink)");
54+
}
55+
}
56+
if(!fullIcu.haveDat()) {
57+
throw Error('Somehow failed to install ' + icudat);
58+
} else {
59+
advice();
60+
}
61+
}*/
62+
};

myFetch.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (C) 2015-2016 IBM Corporation and Others. All Rights Reserved.
2+
3+
const os = require('os');
4+
var path = require('path');
5+
var fs = require('fs');
6+
7+
function getFetcher(u) {
8+
if(u.protocol === 'https:') return require('https');
9+
if(u.protocol === 'http:') return require('http');
10+
return null;
11+
}
12+
13+
/**
14+
* @param {URL} fullUrl url to fetch
15+
* @returns {Promse<String[]>} filename, tmpdir
16+
*/
17+
function myFetch(fullUrl) {
18+
return new Promise((resolve, reject) => {
19+
const fetcher = getFetcher(fullUrl);
20+
console.log('Fetch:', fullUrl.toString());
21+
if(!fetcher) {
22+
return reject(Error(`Unknown URL protocol ${fullUrl.protocol} in ${fullUrl.toString()}`));
23+
}
24+
25+
fetcher.get(fullUrl, res => {
26+
const length = res.headers['content-length'];
27+
if(res.statusCode === 302 && res.headers.location) {
28+
return resolve(myFetch(new URL(res.headers.location)));
29+
} else if(res.statusCode !== 200) {
30+
return reject(Error(`Bad status code ${res.statusCode}`));
31+
}
32+
const tmpd = fs.mkdtempSync(os.tmpdir());
33+
const tmpf = path.join(tmpd, 'icu-download.zip');
34+
let gotSoFar = 0;
35+
console.dir(tmpd);
36+
37+
res.on('data', data => {
38+
gotSoFar += data.length;
39+
fs.appendFileSync(tmpf, data);
40+
// console.dir(res.headers);
41+
process.stdout.write(`${gotSoFar}/${length}\r`);
42+
// console.log(`chunk: ${data.length}`);
43+
});
44+
res.on('end', () => {
45+
resolve([tmpf, tmpd]);
46+
console.log(`${gotSoFar}/${length}\n`);
47+
});
48+
res.on('error', error => {
49+
fs.unlinkSync(tmpf);
50+
fs.rmdirSync(tmpd);
51+
console.error(error);
52+
return reject(error);
53+
});
54+
});
55+
});
56+
}
57+
58+
module.exports = myFetch;

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
"version": "1.3.5-0",
44
"description": "install 'full-icu' data for your current node",
55
"scripts": {
6+
<<<<<<< HEAD
67
"lint": "standard",
78
"postinstall": "node postinstall.js"
9+
=======
10+
"postinstall": "node postinstall.js",
11+
"test": "tap test/*.js",
12+
"lint": "eslint *.js test/*.js"
13+
>>>>>>> 652cffa (some lint)
814
},
915
"keywords": [
1016
"icu4c"
@@ -22,7 +28,13 @@
2228
"bugs": {
2329
"url": "https://github.com/unicode-org/full-icu-npm/issues"
2430
},
31+
"dependencies": {
32+
"yauzl": "^2.10.0"
33+
},
2534
"devDependencies": {
26-
"standard": "^16.0.3"
35+
"standard": "^16.0.3",
36+
"eslint": "^6.8.0",
37+
"eslint-plugin-header": "^3.0.0",
38+
"tap": "^14.10.6"
2739
}
2840
}

postinstall.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,16 @@ function advice () {
6060
console.log("... will show “enero”. If it shows “January” you don't have full data.")
6161
}
6262

63-
// install by using spawn
64-
const npmInstall = require('./install-spawn')
63+
// Choose install method
64+
var npmInstall;
65+
66+
// GitHub has v50+ as releases
67+
// Experimentally, pull from GitHub for little endian
68+
if ( fullIcu.icuend === 'l' && !process.env.FULL_ICU_PREFER_NPM ) {
69+
npmInstall = require('./install-gh');
70+
} else {
71+
npmInstall = require('./install-spawn');
72+
}
6573

6674
if (fs.existsSync(fullIcu.icudat)) {
6775
console.log('√ ' + fullIcu.icudat + ' Already there (for Node ' + fullIcu.nodever + ' and small-icu ' + fullIcu.icuver + ')')

test/data/haystack.zip

1.68 KB
Binary file not shown.

0 commit comments

Comments
 (0)