1 1
import * as path from 'path'
2 1
import * as fs from 'fs'
3 1
import {
4
	component,
5
	events,
6
	methods,
7
	props,
8
	slots,
9
	defaultExample,
10
	functionalTag
11
} from './compileTemplates'
12
import { SafeDocgenCLIConfig, DocgenCLIConfig } from './config'
13 1
import { findFileCaseInsensitive } from './utils'
14

15 1
export default (
16
	cwd: string,
17 1
	watch = false,
18
	configFileFromCmd?: string,
19 1
	pathArray: string[] = []
20
): SafeDocgenCLIConfig => {
21 1
	const configFilePath = configFileFromCmd
22 1
		? path.resolve(cwd, configFileFromCmd)
23 1
		: path.join(cwd, 'docgen.config.js')
24 1
	const [componentsFromCmd, outDirFromCmd] = pathArray
25

26 1
	const config: Partial<DocgenCLIConfig> = {
27
		cwd,
28
		watch,
29
		componentsRoot: path.dirname(configFilePath),
30 1
		components: componentsFromCmd || 'src/components/**/[a-zA-Z]*.{vue,js,jsx,ts,tsx}',
31
		outDir: outDirFromCmd,
32 0
		getDocFileName: (componentPath: string): string | false => {
33 0
			const files = [
34
				path.join(path.dirname(componentPath), 'Readme.md'),
35
				// ComponentName.md
36
				componentPath.replace(path.extname(componentPath), '.md'),
37
				// FolderName.md when component definition file is index.js
38
				path.join(path.dirname(componentPath), path.basename(path.dirname(componentPath)) + '.md')
39
			]
40 0
			for (const file of files) {
41 0
				const existingFile = findFileCaseInsensitive(file)
42 1
				if (existingFile) {
43 0
					return existingFile
44
				}
45
			}
46 0
			return false
47
		},
48 0
		getDestFile: (file: string, conf: SafeDocgenCLIConfig): string =>
49 0
			path.resolve(conf.outDir, file).replace(/\.\w+$/, '.md'),
50
		editLinkLabel: 'edit on github',
51 1
		...(fs.existsSync(configFilePath) ? require(configFilePath) : undefined)
52
	}
53

54 1
	if (!config.getRepoEditUrl && config.docsRepo) {
55 1
		const branch = config.docsBranch || 'master'
56 1
		const dir = config.docsFolder || ''
57 0
		config.getRepoEditUrl = p => `https://github.com/${config.docsRepo}/edit/${branch}/${dir}/${p}`
58
	}
59

60
	// only default outDir if `outFile` is null to avoid confusion
61 1
	config.outDir = config.outDir || (config.outFile ? '.' : 'docs')
62

63 1
	config.templates = {
64
		component,
65
		events,
66
		methods,
67
		props,
68
		slots,
69
		defaultExample,
70
		functionalTag,
71
		...config.templates
72
	}
73

74 1
	return config as SafeDocgenCLIConfig
75
}

Read our documentation on viewing source code .

Loading