vue-styleguidist / vue-styleguidist
1 1
import * as path from 'path'
2
import { FSWatcher } from 'chokidar'
3 1
import { writeDownMdFile } from './utils'
4
import { DocgenCLIConfigWithComponents } from './docgen'
5 1
import compileTemplates from './compileTemplates'
6

7
export interface DocgenCLIConfigWithOutFile extends DocgenCLIConfigWithComponents {
8
	outFile: string
9
}
10

11
/**
12
 * Build one md file combining all documentations for components in files
13
 * if `config.watch` is true will keep on watch file changes
14
 * and update the current file if needed
15
 * @param files
16
 * @param watcher
17
 * @param config
18
 * @param _compile
19
 */
20 1
export default async function (
21
	files: string[],
22
	watcher: FSWatcher,
23
	config: DocgenCLIConfigWithOutFile,
24
	docMap: { [filepath: string]: string },
25 1
	_compile = compile
26
) {
27
	// This fileCache contains will, because it is
28
	// bound, the same along usage of this function.
29
	// it will contain
30
	// `key`: filePath of source component
31
	// `content`: markdown compiled for it
32 1
	const fileCache = {}
33 1
	const compileSingleDocWithConfig = _compile.bind(null, config, files, fileCache, docMap, watcher)
34

35 1
	await compileSingleDocWithConfig()
36

37 1
	if (config.watch) {
38 1
		watcher.on('add', compileSingleDocWithConfig).on('change', compileSingleDocWithConfig)
39
	}
40
}
41

42
/**
43
 * Compile all components in `files` into one single
44
 * markdown file and save it to the `config.outFile`
45
 * @param files the component files relative paths
46
 * @param config config passed to the current chunk
47
 * @param cachedContent in order to avoid reparsing unchanged components pass an object wher to store for future reference
48
 * @param docMap a map of each documentation file to the component they refer to
49
 * @param changedFilePath When in wtch mode, provide the relative path of the file that changes to only re-parse this file
50
 */
51 1
export async function compile(
52
	config: DocgenCLIConfigWithOutFile,
53
	files: string[],
54
	cachedContent: { [filepath: string]: string },
55
	docMap: { [filepath: string]: string },
56
	watcher: FSWatcher,
57 1
	changedFilePath?: string
58
) {
59
	// this local function will enrich the cachedContent with the
60
	// current components documentation
61 1
	const cacheMarkDownContent = async (filePath: string) => {
62 1
		const { content, dependencies } = await compileTemplates(
63
			path.join(config.componentsRoot, filePath),
64
			config,
65
			filePath
66
		)
67 1
		dependencies.forEach(d => {
68 0
			watcher.add(d)
69 0
			docMap[d] = filePath
70
		})
71 1
		cachedContent[filePath] = content
72 1
		return true
73
	}
74

75 1
	if (changedFilePath) {
76
		// resolve the real component file path before updating if needed
77 1
		changedFilePath = docMap[changedFilePath] || changedFilePath
78
		try {
79
			// if in chokidar mode (watch), the path of the file that was just changed
80
			// is passed as an argument. We only affect the changed file and avoid re-parsing the rest
81 0
			await cacheMarkDownContent(changedFilePath)
82
		} catch (e) {
83 0
			throw new Error(
84
				`Error compiling file ${config.outFile} when file ${changedFilePath} has changed: ${e.message}`
85
			)
86
		}
87
	} else {
88
		try {
89
			// if we are initializing the current file, parse all components
90 1
			await Promise.all(files.map(cacheMarkDownContent))
91
		} catch (e) {
92 0
			throw new Error(`Error compiling file ${config.outFile}: ${e.message}`)
93
		}
94
	}
95
	// and finally save all concatenated values to the markdown file
96 1
	writeDownMdFile(Object.values(cachedContent), config.outFile)
97
}

Read our documentation on viewing source code .

Loading