vue-styleguidist / vue-styleguidist
1 1
import * as path from 'path'
2 1
import * as fs from 'fs'
3 1
import { promisify } from 'util'
4 1
import mkdirpNative from 'mkdirp'
5 1
import prettier from 'prettier'
6

7 1
const mkdirp = promisify(mkdirpNative)
8

9
/**
10
 * Prettify then save a markdown content
11
 * If the needed directory does not exist, create it
12
 * @param content dirty looking markdown content to be saved
13
 * @param destFilePath destination absolute file path
14
 */
15 1
export async function writeDownMdFile(content: string | string[], destFilePath: string) {
16 1
	const conf = await prettier.resolveConfig(destFilePath)
17 1
	const prettyMd = (cont: string) =>
18 1
		prettier.format(cont, Object.assign(conf || {}, { parser: 'markdown' }))
19 1
	const destFolder = path.dirname(destFilePath)
20 1
	await mkdirp(destFolder)
21 1
	const writeStream = fs.createWriteStream(destFilePath)
22 1
	if (Array.isArray(content)) {
23 0
		content.forEach(cont => {
24 0
			writeStream.write(prettyMd(cont))
25
		})
26
	} else {
27 1
		writeStream.write(prettyMd(content))
28
	}
29

30
	// close the stream
31 1
	writeStream.close()
32
}
33

34
/**
35
 * retrun an object matching document relative file path
36
 * with their corresponding components, it's inteded to be use
37
 * with watchers to update the right documentation on update of
38
 * Readme.md files
39
 * @param files file paths of the matched comeponents
40
 * @param getDocFileName way to transform a comopnent path into it's Readme.md
41
 * @param root componentRoot to de-absolutize the DocFileName path
42
 */
43 1
export function getDocMap(
44
	files: string[],
45
	getDocFileName: (file: string) => string | false,
46 1
	root: string
47
): { [filepath: string]: string } {
48 1
	const docMap: { [filepath: string]: string } = {}
49 1
	files.forEach(f => {
50 1
		const docFilePath = getDocFileName(path.join(root, f))
51 1
		if (docFilePath) {
52 1
			docMap[path.relative(root, docFilePath)] = f
53
		}
54
	})
55 1
	return docMap
56
}
57

58
/**
59
 * Find a file in a directory, case-insensitive
60
 *
61
 * @param {string} filepath
62
 * @return {string|undefined} File path with correct case
63
 */
64 1
export function findFileCaseInsensitive(filepath: string): string | undefined {
65 0
	const dir = path.dirname(filepath)
66 0
	const fileNameLower = path.basename(filepath).toLowerCase()
67 0
	const files = fs.readdirSync(dir)
68 0
	const found = files.find(file => file.toLowerCase() === fileNameLower)
69 1
	return found && path.join(dir, found)
70
}

Read our documentation on viewing source code .

Loading