forked from machatman/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-versioning-in-files.js
executable file
·70 lines (57 loc) · 2.25 KB
/
update-versioning-in-files.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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const walk = require('walk-sync')
const frontmatter = require('@github-docs/frontmatter')
const contentPath = path.join(process.cwd(), 'content')
const dataPath = path.join(process.cwd(), 'data')
const contentFiles = walk(contentPath, { includeBasePath: true, directories: false })
.filter(file => file.endsWith('.md'))
.filter(file => !file.endsWith('README.md'))
const dataFiles = walk(dataPath, { includeBasePath: true, directories: false })
.filter(file => file.includes('data/reusables') || file.includes('data/variables'))
.filter(file => !file.endsWith('README.md'))
dataFiles
.forEach(file => {
const content = fs.readFileSync(file, 'utf8')
// Update Liquid in data files
const newContent = updateLiquid(content)
fs.writeFileSync(file, newContent)
})
contentFiles
.forEach(file => {
const { data, content } = frontmatter(fs.readFileSync(file, 'utf8'))
// Update Liquid in content files
const newContent = content ? updateLiquid(content) : ''
// Update versions frontmatter
if (data) {
if (!data.versions && data.productVersions) {
data.versions = data.productVersions
Object.keys(data.versions).forEach(version => {
// update 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
delete data.versions.enterprise
}
})
}
delete data.productVersions
// Update Liquid in frontmatter props
Object.keys(data)
// Only process a subset of props
.filter(key => key === 'title' || key === 'intro' || key === 'product')
.forEach(key => {
data[key] = updateLiquid(data[key])
})
}
fs.writeFileSync(file, frontmatter.stringify(newContent, data, { lineWidth: 10000 }))
})
function updateLiquid (content) {
return content
.replace(/page.version/g, 'currentVersion')
.replace(/["'](?:')?dotcom["'](?:')?/g, '"free-pro-team@latest"')
.replace(/["'](?:')?(2\.\d{2})["'](?:')?/g, '"enterprise-server@$1"')
}