generated from Adiltonzs/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-data-and-image-paths.js
executable file
·175 lines (147 loc) · 6.68 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/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 walk from 'walk-sync'
import { escapeRegExp, last } from 'lodash-es'
import yaml from 'js-yaml'
import patterns from '../../lib/patterns.js'
const earlyAccessContent = path.posix.join(process.cwd(), 'content/early-access')
const earlyAccessData = path.posix.join(process.cwd(), 'data/early-access')
const earlyAccessImages = path.posix.join(process.cwd(), 'assets/images/early-access')
program
.description('Update data and image paths.')
.option(
'-p, --path-to-early-access-content-file <PATH>',
'Path to a specific content file. Defaults to all Early Access content 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, pathToEarlyAccessContentFile } = program.opts()
if (!(add || remove)) {
console.error('Error! Must specify either `--add` or `--remove`.')
process.exit(1)
}
let earlyAccessContentAndDataFiles
if (pathToEarlyAccessContentFile) {
earlyAccessContentAndDataFiles = path.posix.join(process.cwd(), pathToEarlyAccessContentFile)
if (!fs.existsSync(earlyAccessContentAndDataFiles)) {
console.error(
`Error! ${pathToEarlyAccessContentFile} can't be found. Make sure the path starts with 'content/early-access'.`
)
process.exit(1)
}
earlyAccessContentAndDataFiles = [earlyAccessContentAndDataFiles]
} else {
// Gather the EA content and data files
earlyAccessContentAndDataFiles = walk(earlyAccessContent, {
includeBasePath: true,
directories: false,
}).concat(walk(earlyAccessData, { includeBasePath: true, directories: false }))
}
// Update the EA content and data files
earlyAccessContentAndDataFiles.forEach((file) => {
const oldContents = fs.readFileSync(file, 'utf8')
// Get all the data references in each file that exist in data/early-access
const dataRefs = (oldContents.match(patterns.dataReference) || []).filter((dataRef) =>
dataRef.includes('variables') ? checkVariable(dataRef) : checkReusable(dataRef)
)
// Get all the image references in each file that exist in assets/images/early-access
const imageRefs = (oldContents.match(patterns.imagePath) || []).filter((imageRef) =>
checkImage(imageRef)
)
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('data early-access.'))
// Add to the { oldRef: newRef } replacements object
.forEach((dataRef) => {
replacements[dataRef] = dataRef.replace(/({% data )(.*)/, '$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('{% data early-access.'))
// Add to the { oldRef: newRef } replacements object
.forEach((dataRef) => {
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')
function checkVariable(dataRef) {
// Get the data filepath from the data reference,
// where the data reference looks like: {% data variables.foo.bar %}
// and the data filepath looks like: data/variables/foo.yml with key of 'bar'.
const variablePathArray = dataRef
.match(/{% data (.*?) %}/)[1]
.split('.')
// If early access is part of the path, remove it (since the path below already includes it)
.filter((n) => n !== 'early-access')
// Given a string `variables.foo.bar` split into an array, we want the last segment 'bar', which is the variable key.
// Then pop 'bar' off the array because it's not really part of the filepath.
// The filepath we want is `variables/foo.yml`.
const variableKey = last(variablePathArray)
variablePathArray.pop()
const variablePath = path.posix.join(earlyAccessData, `${variablePathArray.join('/')}.yml`)
// If the variable file doesn't exist in data/early-access, exclude it
if (!fs.existsSync(variablePath)) return false
// If the variable file exists but doesn't have the referenced key, exclude it
const variableFileContent = yaml.load(fs.readFileSync(variablePath, 'utf8'))
return variableFileContent[variableKey]
}
function checkReusable(dataRef) {
// Get the data filepath from the data reference,
// where the data reference looks like: {% data reusables.foo.bar %}
// and the data filepath looks like: data/reusables/foo/bar.md.
const reusablePath = dataRef
.match(/{% data (.*?) %}/)[1]
.split('.')
// If early access is part of the path, remove it (since the path below already includes it)
.filter((n) => n !== 'early-access')
.join('/')
// If the reusable file doesn't exist in data/early-access, exclude it
return fs.existsSync(`${path.posix.join(earlyAccessData, reusablePath)}.md`)
}
function checkImage(imageRef) {
const imagePath = imageRef
.replace('/assets/images/', '')
// If early access is part of the path, remove it (since the path below already includes it)
.replace('early-access', '')
// If the image file doesn't exist in assets/images/early-access, exclude it
return fs.existsSync(path.posix.join(earlyAccessImages, imagePath))
}