forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-data-and-image-paths.js
executable file
·137 lines (115 loc) · 4.79 KB
/
update-data-and-image-paths.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env node
// [start-readme]
//
// This script is run on a writer's machine while developing Early Access content locally. It
// updates the data and image paths to either include `early-access` or remove it.
//
// [end-readme]
import fs from 'fs'
import path from 'path'
import { program } from 'commander'
import walkFiles from '../helpers/walk-files.js'
import { escapeRegExp } from 'lodash-es'
import patterns from '../../lib/patterns.js'
program
.description('Update data and image paths.')
.option(
'-p, --early-access-path <PATH>',
'Early access filepath. Defaults to all Early Access content and data files if not provided.'
)
.option('-a, --add', 'Add "early-access" to data and image paths.')
.option('-r, --remove', 'Remove "early-access" from data and image paths.')
.parse(process.argv)
const { add, remove, earlyAccessPath } = program.opts()
if (!(add || remove)) {
console.error('Error! Must specify either `--add` or `--remove`.')
process.exit(1)
}
if (earlyAccessPath && !earlyAccessPath.startsWith('content/early-access')) {
console.error(`Error! Make sure ${earlyAccessPath} starts with 'content/early-access'.`)
process.exit(1)
}
const allEarlyAccessFiles = walkFiles('content', '.md', { includeEarlyAccess: true }).concat(
walkFiles('data', ['.md', '.yml'], { includeEarlyAccess: true })
)
let selectFiles = allEarlyAccessFiles
if (earlyAccessPath) {
const contentFiles = allEarlyAccessFiles.filter((file) => file.includes(earlyAccessPath))
// We also need to include any reusable files that are referenced in the selected content files.
const referencedDataFiles = []
contentFiles.forEach((file) => {
const contents = fs.readFileSync(file, 'utf8')
const dataRefs = contents.match(patterns.dataReference) || []
const filepaths = dataRefs
.filter((dataRef) => dataRef.includes('reusables') && dataRef.includes('early-access'))
.map((dataRef) => {
const filepath = dataRef
.match(/{% (?:data|indented_data_reference) (\S*?) .*%}/)[1]
.split('.')
.join('/')
return path.posix.join(process.cwd(), 'data', `${filepath}.md`)
})
referencedDataFiles.push(...filepaths)
})
const dataFiles = allEarlyAccessFiles.filter((file) => {
return referencedDataFiles.some((f) =>
f.includes(file.replace('data/reusables', 'data/early-access/reusables'))
)
})
selectFiles = contentFiles.concat(dataFiles)
}
// Update the EA content and data files
selectFiles.forEach((file) => {
const oldContents = fs.readFileSync(file, 'utf8')
const dataRefs = oldContents.match(patterns.dataReference) || []
const imageRefs = oldContents.match(patterns.imagePath) || []
const replacements = {}
if (add) {
dataRefs
// Since we're adding early-access to the path, filter for those that do not already include it
.filter((dataRef) => !dataRef.includes(' early-access.'))
// Add to the { oldRef: newRef } replacements object
.forEach((dataRef) => {
replacements[dataRef] = dataRef.replace(
/({% (?:data|indented_data_reference) )(.*)/,
'$1early-access.$2'
)
})
imageRefs
// Since we're adding early-access to the path, filter for those that do not already include it
.filter((imageRef) => !imageRef.split('/').includes('early-access'))
// Add to the { oldRef: newRef } replacements object
.forEach((imageRef) => {
replacements[imageRef] = imageRef.replace('/assets/images/', '/assets/images/early-access/')
})
}
if (remove) {
dataRefs
// Since we're removing early-access from the path, filter for those that include it
.filter((dataRef) => dataRef.includes(' early-access.'))
// Add to the { oldRef: newRef } replacements object
.forEach((dataRef) => {
replacements[dataRef] = dataRef.replace('early-access.', '').replace('-alt.', '.')
// replacements[dataRef] = dataRef.replace('early-access.', '')
})
imageRefs
// Since we're removing early-access from the path, filter for those that include it
.filter((imageRef) => imageRef.split('/').includes('early-access'))
// Add to the { oldRef: newRef } replacements object
.forEach((imageRef) => {
replacements[imageRef] = imageRef.replace('/assets/images/early-access/', '/assets/images/')
})
}
// Return early if nothing to replace
if (!Object.keys(replacements).length) {
return
}
// Make the replacement in the content
let newContents = oldContents
Object.entries(replacements).forEach(([oldRef, newRef]) => {
newContents = newContents.replace(new RegExp(escapeRegExp(oldRef), 'g'), newRef)
})
// Write the updated content
fs.writeFileSync(file, newContents)
})
console.log('Done! Run "git status" in your docs-early-access checkout to see the changes.\n')