Skip to content

Commit

Permalink
Update build script
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 11, 2019
1 parent e0f27bc commit 35183a6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
*.log
node_modules/
archive.zip
iso-639-3.js
iso-639-3.min.js
yarn.lock
82 changes: 55 additions & 27 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ var fs = require('fs')
var path = require('path')
var https = require('https')
var concat = require('concat-stream')
var unzip = require('unzip')
var yauzl = require('yauzl')
var dsv = require('d3-dsv')
var bail = require('bail')

var found
var found = false

var SCOPES = {
var scopes = {
I: 'individual',
M: 'macrolanguage',
S: 'special'
}

var TYPES = {
var types = {
A: 'ancient',
C: 'constructed',
E: 'extinct',
Expand All @@ -25,31 +25,60 @@ var TYPES = {
S: 'special'
}

// Note:
// You can find download links here:
// <https://iso639-3.sil.org/code_tables/download_tables>
// Just get the complete code tables in UTF-8.

https
.request(
'https://iso639-3.sil.org/sites/iso639-3/files/downloads/iso-639-3_Code_Tables_20180123.zip',
'https://iso639-3.sil.org/sites/iso639-3/files/downloads/iso-639-3_Code_Tables_20190408.zip',
onrequest
)
.end()

process.on('exit', onexit)

function onexit() {
if (!found) {
throw new Error('Could not find expected file')
}
function onrequest(res) {
res
.pipe(fs.createWriteStream('archive.zip'))
.on('close', onclose)
.on('error', bail)
}

function onrequest(response) {
response.pipe(new unzip.Parse()).on('entry', onentry)
function onclose() {
yauzl.open('archive.zip', {lazyEntries: true}, onopen)
}

function onentry(entry) {
if (path.basename(entry.path) === 'iso-639-3_20180123.tab') {
function onopen(err, archive) {
bail(err)

read()

archive.on('entry', onentry)
archive.on('end', onend)

function onentry(entry) {
if (path.basename(entry.fileName) !== 'iso-639-3_20190408.tab') {
return read()
}

found = true
entry.pipe(concat(onconcat))
} else {
entry.autodrain()
archive.openReadStream(entry, onreadstream)
}

function onreadstream(err, rs) {
bail(err)
rs.pipe(concat(onconcat)).on('error', bail)
rs.on('end', read)
}

function read() {
archive.readEntry()
}
}

function onend() {
if (!found) {
throw new Error('File not found')
}
}

Expand All @@ -59,15 +88,14 @@ function onconcat(body) {
fs.writeFile('index.json', JSON.stringify(data, 0, 2) + '\n', bail)
}

function mapper(language) {
function mapper(d) {
return {
name: language.Ref_Name || null,
type: TYPES[language.Language_Type],
scope: SCOPES[language.Scope],
iso6393: language['Id'], // There’s a `<U+FEFF>`
// in there, I don’t know why, but meh.
iso6392B: language.Part2B || null,
iso6392T: language.Part2T || null,
iso6391: language.Part1 || null
name: d.Ref_Name || null,
type: types[d.d_Type],
scope: scopes[d.Scope],
iso6393: d.Id,
iso6392B: d.Part2B || null,
iso6392T: d.Part2T || null,
iso6391: d.Part1 || null
}
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.4.0",
"tinyify": "^2.5.0",
"unzip": "^0.1.11",
"xo": "^0.24.0"
"xo": "^0.24.0",
"yauzl": "^2.10.0"
},
"scripts": {
"generate": "node build",
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"build-bundle": "browserify . -s iso6393 -o iso-639-3.js",
"build-mangle": "browserify . -s iso6393 -p tinyify -o iso-639-3.min.js",
"build-bundle": "browserify index.json -s iso6393 -o iso-639-3.js",
"build-mangle": "browserify index.json -s iso6393 -p tinyify -o iso-639-3.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test": "npm run format && npm run build && npm run test-api"
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Normal, single language (example: `eng` for `English`).

[author]: https://wooorm.com

[iso]: http://www-01.sil.org/iso639-3
[iso]: https://iso639-3.sil.org

[type]: #type

Expand Down

0 comments on commit 35183a6

Please sign in to comment.