-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
naftalimurgor
committed
Jul 23, 2023
1 parent
57e498b
commit b363eaa
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* istanbul ignore file */ | ||
var fetch = require('node-fetch') | ||
var fs = require('fs') | ||
var path = require('path') | ||
|
||
var log = console.log | ||
var WORDLISTS = [ | ||
'chinese_simplified', | ||
'chinese_traditional', | ||
'czech', | ||
'english', | ||
'french', | ||
'italian', | ||
'japanese', | ||
'korean', | ||
'portuguese', | ||
'spanish' | ||
] | ||
|
||
function update () { | ||
download().then(function (wordlists) { | ||
var promises = Object.keys(wordlists).map(function (name) { return save(name, wordlists[name]) }) | ||
return Promise.all(promises) | ||
}) | ||
} | ||
|
||
function download () { | ||
var wordlists = {} | ||
|
||
var promises = WORDLISTS.map(function (name) { | ||
return fetchRaw(name).then(toJSON).then(function (wordlist) { wordlists[name] = wordlist }) | ||
}) | ||
|
||
return Promise.all(promises).then(function () { return wordlists }) | ||
} | ||
|
||
function fetchRaw (name) { | ||
var url = 'https://raw.githubusercontent.com/bitcoin/bips/master/bip-0039/' + name + '.txt' | ||
log('download ' + url) | ||
|
||
return fetch(url).then(function (response) { return response.text() }) | ||
} | ||
|
||
function toJSON (content) { | ||
return content.trim().split('\n').map(function (word) { return word.trim() }) | ||
} | ||
|
||
function save (name, wordlist) { | ||
var location = path.join(__dirname, '..', 'ts_src', 'wordlists', name + '.json') | ||
var content = JSON.stringify(wordlist, null, 2) + '\n' | ||
log('save ' + wordlist.length + ' words to ' + location) | ||
|
||
return new Promise(function (resolve, reject) { | ||
fs.writeFile(location, content, function (err) { | ||
if (err) reject(err) | ||
else resolve() | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = { update: update, download: download } |