-
Notifications
You must be signed in to change notification settings - Fork 66.3k
Expand file tree
/
Copy pathsync.ts
More file actions
executable file
·110 lines (97 loc) · 4.05 KB
/
sync.ts
File metadata and controls
executable file
·110 lines (97 loc) · 4.05 KB
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
import { readFile, writeFile, copyFile } from 'fs/promises'
import { existsSync } from 'fs'
import walk from 'walk-sync'
import { mkdirp } from 'mkdirp'
import { execFileSync, execSync } from 'child_process'
import path from 'path'
import matter from '@gr2m/gray-matter'
import { rimraf } from 'rimraf'
import { updateContentDirectory } from '../../automated-pipelines/lib/update-markdown'
import { convertContentToDocs } from './convert-markdown-for-docs'
const { targetDirectory, sourceDirectory, frontmatterDefaults, markdownPrefix } = JSON.parse(
await readFile(path.join('src/codeql-cli/lib/config.json'), 'utf-8'),
)
const SOURCE_REPO = sourceDirectory.split('/')[0]
const TEMP_DIRECTORY = path.join(SOURCE_REPO, 'tempCliDocs')
const MARKDOWN_PREFIX = `\n${markdownPrefix}\n\n`
main()
async function main() {
await setupEnvironment()
// convert the rst files to Markdown using pandoc
await rstToMarkdown(sourceDirectory)
const markdownFiles = walk(TEMP_DIRECTORY, {
includeBasePath: true,
globs: ['**/*.md'],
})
const cliMarkdownContents: Record<string, { data: Record<string, unknown>; content: string }> = {}
for (const file of markdownFiles) {
const sourceContent = await readFile(file, 'utf8')
// There is a missing heading in the source content called "Primary Options"
// It should be directory under the "Options" heading.
// It's a quite a bit more complicated to add new nodes in the AST when
// the node isn't a child of the previous heading. It's pretty easy to
// just append a second heading here.
const matchHeading = '## Options\n'
const primaryHeadingSourceContent = sourceContent.replace(
matchHeading,
`${matchHeading}\n### Primary Options\n`,
)
const currentFileName = path.basename(file)
const { data, content } = await convertContentToDocs(
primaryHeadingSourceContent,
{},
currentFileName,
)
await writeFile(file, matter.stringify(content, data))
const targetFilename = path.join(targetDirectory, path.basename(file))
const sourceData = { ...data, ...frontmatterDefaults }
const finalSourceContent = MARKDOWN_PREFIX + content
cliMarkdownContents[targetFilename] = { data: sourceData, content: finalSourceContent }
}
// Begin updating Markdown files in the content directory
await updateContentDirectory({
targetDirectory,
sourceContent: cliMarkdownContents,
frontmatter: frontmatterDefaults,
})
}
// Separates out steps that need to be done before the sync can begin
async function setupEnvironment() {
if (!existsSync(SOURCE_REPO)) {
const errorMessage = `Source directory ${SOURCE_REPO} does not exist. Please clone the repo.`
throw new Error(errorMessage)
}
const isPandoc = execSync('pandoc --version', { encoding: 'utf8' })
if (!isPandoc.startsWith('pandoc')) {
throw new Error(
'You must install pandoc to run this script. See https://pandoc.org/installing.html.',
)
}
// refresh the temp directory
await rimraf(TEMP_DIRECTORY)
await mkdirp(TEMP_DIRECTORY)
}
// copy the raw rst files to the temp directory and convert them
// to Markdownusing pandoc
async function rstToMarkdown(rstSourceDirectory: string) {
const sourceFiles = walk(rstSourceDirectory, {
includeBasePath: true,
globs: ['**/*.rst'],
})
for (const file of sourceFiles) {
const tempFilePath = `${TEMP_DIRECTORY}/${path.basename(file)}`
if (tempFilePath.includes(' ') || tempFilePath.includes('..')) {
const errorMsg = `The ${tempFilePath} has a space or .. in the path.`
throw new Error(errorMsg)
}
await copyFile(file, tempFilePath)
// Convert the rst files to Markdown
const markdownFilename = path.basename(file).replace('.rst', '.md')
const outputFilepath = `${TEMP_DIRECTORY}/${markdownFilename}`
if (outputFilepath.includes(' ') || outputFilepath.includes('..')) {
const errorMsg = `The ${outputFilepath} has a space or .. in the path.`
throw new Error(errorMsg)
}
execFileSync('pandoc', [tempFilePath, '-f', 'rst', '-t', 'commonmark_x', '-o', outputFilepath])
}
}