Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

update-data-and-image-paths.js 6.7 KB

You have to be logged in to leave a comment. Sign In
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
  1. #!/usr/bin/env node
  2. // [start-readme]
  3. //
  4. // This script is run on a writer's machine while developing Early Access content locally. It
  5. // updates the data and image paths to either include `early-access` or remove it.
  6. //
  7. // [end-readme]
  8. const fs = require('fs')
  9. const path = require('path')
  10. const program = require('commander')
  11. const walk = require('walk-sync')
  12. const { escapeRegExp, last } = require('lodash')
  13. const yaml = require('js-yaml')
  14. const patterns = require('../../lib/patterns')
  15. const earlyAccessContent = path.posix.join(process.cwd(), 'content/early-access')
  16. const earlyAccessData = path.posix.join(process.cwd(), 'data/early-access')
  17. const earlyAccessImages = path.posix.join(process.cwd(), 'assets/images/early-access')
  18. program
  19. .description('Update data and image paths.')
  20. .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.')
  21. .option('-a, --add', 'Add "early-access" to data and image paths.')
  22. .option('-r, --remove', 'Remove "early-access" from data and image paths.')
  23. .parse(process.argv)
  24. if (!(program.add || program.remove)) {
  25. console.error('Error! Must specify either `--add` or `--remove`.')
  26. process.exit(1)
  27. }
  28. let earlyAccessContentAndDataFiles
  29. if (program.pathToEarlyAccessContentFile) {
  30. earlyAccessContentAndDataFiles = path.posix.join(process.cwd(), program.pathToEarlyAccessContentFile)
  31. if (!fs.existsSync(earlyAccessContentAndDataFiles)) {
  32. console.error(`Error! ${program.pathToEarlyAccessContentFile} can't be found. Make sure the path starts with 'content/early-access'.`)
  33. process.exit(1)
  34. }
  35. earlyAccessContentAndDataFiles = [earlyAccessContentAndDataFiles]
  36. } else {
  37. // Gather the EA content and data files
  38. earlyAccessContentAndDataFiles = walk(earlyAccessContent, { includeBasePath: true, directories: false })
  39. .concat(walk(earlyAccessData, { includeBasePath: true, directories: false }))
  40. }
  41. // Update the EA content and data files
  42. earlyAccessContentAndDataFiles
  43. .forEach(file => {
  44. const oldContents = fs.readFileSync(file, 'utf8')
  45. // Get all the data references in each file that exist in data/early-access
  46. const dataRefs = (oldContents.match(patterns.dataReference) || [])
  47. .filter(dataRef => dataRef.includes('variables') ? checkVariable(dataRef) : checkReusable(dataRef))
  48. // Get all the image references in each file that exist in assets/images/early-access
  49. const imageRefs = (oldContents.match(patterns.imagePath) || [])
  50. .filter(imageRef => checkImage(imageRef))
  51. const replacements = {}
  52. if (program.add) {
  53. dataRefs
  54. // Since we're adding early-access to the path, filter for those that do not already include it
  55. .filter(dataRef => !dataRef.includes('data early-access.'))
  56. // Add to the { oldRef: newRef } replacements object
  57. .forEach(dataRef => {
  58. replacements[dataRef] = dataRef.replace(/({% data )(.*)/, '$1early-access.$2')
  59. })
  60. imageRefs
  61. // Since we're adding early-access to the path, filter for those that do not already include it
  62. .filter(imageRef => !imageRef.split('/').includes('early-access'))
  63. // Add to the { oldRef: newRef } replacements object
  64. .forEach(imageRef => {
  65. replacements[imageRef] = imageRef.replace('/assets/images/', '/assets/images/early-access/')
  66. })
  67. }
  68. if (program.remove) {
  69. dataRefs
  70. // Since we're removing early-access from the path, filter for those that include it
  71. .filter(dataRef => dataRef.includes('{% data early-access.'))
  72. // Add to the { oldRef: newRef } replacements object
  73. .forEach(dataRef => {
  74. replacements[dataRef] = dataRef.replace('early-access.', '')
  75. })
  76. imageRefs
  77. // Since we're removing early-access from the path, filter for those that include it
  78. .filter(imageRef => imageRef.split('/').includes('early-access'))
  79. // Add to the { oldRef: newRef } replacements object
  80. .forEach(imageRef => {
  81. replacements[imageRef] = imageRef.replace('/assets/images/early-access/', '/assets/images/')
  82. })
  83. }
  84. // Return early if nothing to replace
  85. if (!Object.keys(replacements).length) {
  86. return
  87. }
  88. // Make the replacement in the content
  89. let newContents = oldContents
  90. Object.entries(replacements).forEach(([oldRef, newRef]) => {
  91. newContents = newContents.replace(new RegExp(escapeRegExp(oldRef), 'g'), newRef)
  92. })
  93. // Write the updated content
  94. fs.writeFileSync(file, newContents)
  95. })
  96. console.log('Done! Run "git status" in your docs-early-access checkout to see the changes.\n')
  97. function checkVariable (dataRef) {
  98. // Get the data filepath from the data reference,
  99. // where the data reference looks like: {% data variables.foo.bar %}
  100. // and the data filepath looks like: data/variables/foo.yml with key of 'bar'.
  101. const variablePathArray = dataRef.match(/{% data (.*?) %}/)[1].split('.')
  102. // If early access is part of the path, remove it (since the path below already includes it)
  103. .filter(n => n !== 'early-access')
  104. // Given a string `variables.foo.bar` split into an array, we want the last segment 'bar', which is the variable key.
  105. // Then pop 'bar' off the array because it's not really part of the filepath.
  106. // The filepath we want is `variables/foo.yml`.
  107. const variableKey = last(variablePathArray); variablePathArray.pop()
  108. const variablePath = path.posix.join(earlyAccessData, `${variablePathArray.join('/')}.yml`)
  109. // If the variable file doesn't exist in data/early-access, exclude it
  110. if (!fs.existsSync(variablePath)) return false
  111. // If the variable file exists but doesn't have the referenced key, exclude it
  112. const variableFileContent = yaml.safeLoad(fs.readFileSync(variablePath, 'utf8'))
  113. return variableFileContent[variableKey]
  114. }
  115. function checkReusable (dataRef) {
  116. // Get the data filepath from the data reference,
  117. // where the data reference looks like: {% data reusables.foo.bar %}
  118. // and the data filepath looks like: data/reusables/foo/bar.md.
  119. const reusablePath = dataRef.match(/{% data (.*?) %}/)[1].split('.')
  120. // If early access is part of the path, remove it (since the path below already includes it)
  121. .filter(n => n !== 'early-access')
  122. .join('/')
  123. // If the reusable file doesn't exist in data/early-access, exclude it
  124. return fs.existsSync(`${path.posix.join(earlyAccessData, reusablePath)}.md`)
  125. }
  126. function checkImage (imageRef) {
  127. const imagePath = imageRef
  128. .replace('/assets/images/', '')
  129. // If early access is part of the path, remove it (since the path below already includes it)
  130. .replace('early-access', '')
  131. // If the image file doesn't exist in assets/images/early-access, exclude it
  132. return fs.existsSync(path.posix.join(earlyAccessImages, imagePath))
  133. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...