forked from nodejs/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (40 loc) · 1.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const assert = require('assert')
const path = require('path')
const walk = require('walk-sync')
const { chain } = require('lodash')
const { supportedVersions } = require('./package.json')
const separator = '/'
const contentDir = path.join(__dirname, 'content')
const allPages = walk(contentDir, { directories: false })
.filter(filename => filename.endsWith('.md'))
.map(filename => {
const fullPath = path.join(contentDir, filename)
const nodeVersion = filename.split(separator)[0]
const locale = filename.split(separator)[1]
const filePath = filename
.split(separator)
.slice(2)
.join(separator)
return { locale, nodeVersion, filePath, fullPath }
})
async function getPages (nodeMajor, locale) {
// set defaults
nodeMajor = nodeMajor || supportedVersions[0] // latest
locale = locale || 'en-US'
assert(supportedVersions.includes(nodeMajor), `Invalid major version of Node.js: ${nodeMajor}. Valid versions are ${supportedVersions.join(', ')}`)
return allPages
.filter(page => page.nodeVersion === nodeMajor)
.filter(page => page.locale === locale)
}
const locales = chain(allPages)
.map('locale')
.uniq()
.compact()
.sort()
.value()
module.exports = {
allPages,
getPages,
locales,
supportedVersions
}