This repository has been archived by the owner on Jan 24, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(babel-plugin-function-filemeta): add first version of plugin * feat(react-docgen-actual-name-handler): add new package * chore(babel-plugin-export-metadata): change package * feat(docz-core): add react docgen on data server * chore: a lot of improvements
- Loading branch information
1 parent
96937a9
commit 85499a8
Showing
37 changed files
with
1,739 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["lodash"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { state as entries } from './entries' | ||
export { state as config } from './config' | ||
export { state as props } from './props' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import chokidar from 'chokidar' | ||
import equal from 'fast-deep-equal' | ||
import fastglob from 'fast-glob' | ||
import { State, Params } from '../DataServer' | ||
import { get, omit } from 'lodash/fp' | ||
|
||
import * as paths from '../config/paths' | ||
import { Config } from '../commands/args' | ||
import { docgen } from '../utils/docgen' | ||
|
||
const getPattern = (ts: boolean) => { | ||
const tsPattern = '**/*.{ts,tsx}' | ||
const jsPattern = '**/*.{js,jsx,mjs}' | ||
return [ts ? tsPattern : jsPattern, '!**/node_modules'] | ||
} | ||
|
||
const initial = (config: Config) => async (p: Params) => { | ||
const pattern = getPattern(config.typescript) | ||
const files = await fastglob<string>(pattern, { cwd: paths.root }) | ||
const metadata = await docgen(files, config) | ||
p.setState('props', metadata) | ||
} | ||
|
||
const add = (config: Config) => (p: Params) => async (filepath: string) => { | ||
const old = get('state.props', p) | ||
const metadata = await docgen([filepath], config) | ||
|
||
if (metadata && !equal(old, metadata)) { | ||
p.setState('props', { | ||
...old, | ||
...metadata, | ||
}) | ||
} | ||
} | ||
|
||
const remove = (config: Config) => (p: Params) => async (filepath: string) => | ||
p.setState('props', omit(filepath, get('state.props', p))) | ||
|
||
export const state = (config: Config): State => { | ||
const pattern = getPattern(config.typescript) | ||
const watcher = chokidar.watch(pattern, { | ||
cwd: paths.root, | ||
ignored: /(((^|[\/\\])\..+)|(node_modules))/, | ||
persistent: true, | ||
}) | ||
|
||
watcher.setMaxListeners(Infinity) | ||
|
||
return { | ||
id: 'props', | ||
init: initial(config), | ||
close: () => watcher.close(), | ||
update: async params => { | ||
const addFilepath = add(config) | ||
const removeFilepath = remove(config) | ||
|
||
watcher.on('add', addFilepath(params)) | ||
watcher.on('change', addFilepath(params)) | ||
watcher.on('unlink', removeFilepath(params)) | ||
|
||
return () => watcher.close() | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import * as path from 'path' | ||
import * as fs from 'fs-extra' | ||
import { isFunction } from 'lodash/fp' | ||
import logger from 'signale' | ||
import findUp from 'find-up' | ||
import externalProptypesHandler from 'react-docgen-external-proptypes-handler' | ||
import importedProptypesHandler from 'react-docgen-imported-proptype-handler' | ||
import actualNameHandler from 'react-docgen-actual-name-handler' | ||
import reactDocgenTs from 'react-docgen-typescript' | ||
import reactDocgen from 'react-docgen' | ||
|
||
import * as paths from '../config/paths' | ||
import { Config } from '../commands/args' | ||
|
||
const fileFullPath = (filepath: string) => path.join(paths.root, filepath) | ||
|
||
const tsParser = async (files: string[], config: Config) => { | ||
const tsConfigPath = await findUp('tsconfig.json', { cwd: paths.root }) | ||
if (!tsConfigPath) return {} | ||
|
||
try { | ||
const { parse } = reactDocgenTs.withCustomConfig(tsConfigPath, { | ||
propFilter(prop: any, component: any): any { | ||
if (prop.parent == null) return true | ||
const propFilter = config.docgenConfig.propFilter | ||
const val = propFilter && isFunction(propFilter) && propFilter(prop) | ||
return !prop.parent.fileName.includes('node_modules') || Boolean(val) | ||
}, | ||
}) | ||
|
||
return files | ||
.map(filepath => ({ [fileFullPath(filepath)]: parse(filepath) })) | ||
.reduce((obj, val) => ({ ...obj, ...val }), {}) | ||
} catch (err) { | ||
logger.fatal('Error parsing static types.', err) | ||
return {} | ||
} | ||
} | ||
|
||
const jsParser = (files: string[], config: Config) => { | ||
const resolver = | ||
config.docgenConfig.resolver || | ||
reactDocgen.resolver.findAllExportedComponentDefinitions | ||
|
||
return files.reduce((memo: any, filepath) => { | ||
const handlers = reactDocgen.defaultHandlers.concat([ | ||
externalProptypesHandler(filepath), | ||
importedProptypesHandler(filepath), | ||
actualNameHandler, | ||
]) | ||
|
||
const code = fs.readFileSync(filepath, 'utf-8') | ||
|
||
try { | ||
const data = reactDocgen.parse(code, resolver, handlers) | ||
memo[fileFullPath(filepath)] = data | ||
} catch (err) { | ||
if (err.message !== 'No suitable component definition found.') { | ||
logger.fatal('Error:', filepath, err) | ||
} | ||
} | ||
|
||
return memo | ||
}, {}) | ||
} | ||
|
||
export const docgen = async (files: string[], config: Config) => | ||
config.typescript ? tsParser(files, config) : jsParser(files, config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.