Skip to content

Commit 0034ab8

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 Fixes: #36
1 parent 72ddf0c commit 0034ab8

File tree

14 files changed

+262
-7
lines changed

14 files changed

+262
-7
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: 'standard',
8+
globals: {
9+
Atomics: 'readonly',
10+
SharedArrayBuffer: 'readonly'
11+
},
12+
parserOptions: {
13+
ecmaVersion: 2018
14+
},
15+
rules: {
16+
}
17+
}

.github/workflows/lint.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Lint
33
on: [push, pull_request]
44

55
jobs:
6-
build:
6+
lint:
77
runs-on: ubuntu-latest
88
steps:
99
- uses: actions/checkout@v2
@@ -12,3 +12,12 @@ jobs:
1212
node-version: 16
1313
- run: npm i
1414
- run: npm run lint
15+
test:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v2
19+
- uses: actions/setup-node@v1
20+
with:
21+
node-version: 16
22+
- run: npm i
23+
- run: npm t

.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+
const icupkg = fullIcu.icupkg
15+
const 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+
const path = require('path')
5+
const 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: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"version": "1.3.5-0",
44
"description": "install 'full-icu' data for your current node",
55
"scripts": {
6-
"lint": "standard",
7-
"postinstall": "node postinstall.js"
6+
"lint": "standard && eslint *.js test/*.js",
7+
"postinstall": "node postinstall.js",
8+
"test": "tap test/*.js"
89
},
910
"keywords": [
1011
"icu4c"
@@ -22,7 +23,17 @@
2223
"bugs": {
2324
"url": "https://github.com/unicode-org/full-icu-npm/issues"
2425
},
26+
"dependencies": {
27+
"yauzl": "^2.10.0"
28+
},
2529
"devDependencies": {
26-
"standard": "^16.0.3"
30+
"eslint": "^7.7.0",
31+
"eslint-config-standard": "^16.0.3",
32+
"eslint-plugin-header": "^3.0.0",
33+
"eslint-plugin-import": "^2.24.2",
34+
"eslint-plugin-node": "^11.1.0",
35+
"eslint-plugin-promise": "^4.2.1",
36+
"standard": "^16.0.3",
37+
"tap": "^15.0.10"
2738
}
2839
}

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+
let 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 + ')')

0 commit comments

Comments
 (0)