forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-frontmatter.js
executable file
·72 lines (63 loc) · 2.64 KB
/
update-frontmatter.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const frontmatter = require('@github-docs/frontmatter')
const { flatten } = require('lodash')
const patterns = require('../../lib/patterns')
const walk = require('walk-sync')
const dirsToProcess = ['content', 'translations']
const allFiles = flatten(dirsToProcess.map(dir => {
return walk(path.join(process.cwd(), dir), { includeBasePath: true, directories: false })
.filter(file => !file.endsWith('README.md'))
.filter(file => !(file.endsWith('LICENSE') || file.endsWith('LICENSE-CODE')))
// we only want to process frontmatter in content files in translations, so skip data files
// this is very brittle but works well enough for this script
// (note data files are updated in script/new-versioning/update-content.js)
.filter(file => !file.includes('/data/'))
}))
allFiles.forEach(file => {
const contents = fs.readFileSync(file, 'utf8')
const { data, content } = frontmatter(contents)
if (!data.versions) {
data.versions = data.productVersions
Object.keys(data.versions).forEach(version => {
// process dotcom, actions, rest, etc.
if (version !== 'enterprise') {
data.versions['free-pro-team'] = data.versions[version]
delete data.versions[version]
} else {
data.versions['enterprise-server'] = data.versions.enterprise
// TODO we are not adding these WIP versions yet
// we can run a modified version of this script later to add them
// data.versions['enterprise-cloud'] = '*'
// data.versions['private-instances'] = '*'
delete data.versions.enterprise
}
})
}
// remove hardcoded version numbers in redirect frontmatter
// fix for https://github.com/github/docs-internal/issues/10835
if (data.redirect_from) {
data.redirect_from = Array.from([data.redirect_from]).flat().filter(oldPath => {
return !patterns.getEnterpriseVersionNumber.test(oldPath)
})
}
delete data.productVersions
// update some oneoff content files
if (file.endsWith('content/index.md')) {
data.versions['enterprise-server'] = '*'
// TODO we are not adding these WIP versions yet
// we can run a modified version of this script later to add them
// data.versions['enterprise-cloud'] = '*'
// data.versions['private-instances'] = '*'
}
if (file.endsWith('content/github/index.md')) {
data.title = 'GitHub.com'
delete data.shortTitle
}
if (file.endsWith('content/admin/index.md')) {
data.title = 'Enterprise Administrators'
delete data.shortTitle
}
fs.writeFileSync(file, frontmatter.stringify(content, data, { lineWidth: 10000 }))
})