From 78da9bf12a9979980f07ed520ba1ebc8851c3a2f Mon Sep 17 00:00:00 2001 From: rianbotha Date: Wed, 24 Apr 2019 18:17:11 +0200 Subject: [PATCH] Added ability to handle multiple exports in one file --- README.md | 2 +- index.js | 50 +++++++++++------ package-lock.json | 138 +++++++++++++++++++++++----------------------- package.json | 6 +- 4 files changed, 106 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index a8105aa..3a7b03a 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,4 @@ props-table path/to/SomeComponent.js ``` ## Credits -Forked from https://github.com/TeamWertarbyte/react-props-md-table +Inspired by https://github.com/TeamWertarbyte/react-props-md-table diff --git a/index.js b/index.js index c14dff1..f6cd024 100755 --- a/index.js +++ b/index.js @@ -3,26 +3,42 @@ const reactDocs = require('react-docgen'); const fs = require('fs'); const path = require('path'); +const formatType = (type) => ( + type.name === 'union' + ? type.value.map(formatType).join(' || ') + : type.name +); + +const nlToBr = (str, addQuotes = false) => ( + str.replace(/\r\n|\r|\n/gi, addQuotes ? '`
`' : '
') +); + const filename = process.argv[process.argv.length - 1]; -const content = fs.readFileSync(path.resolve(process.cwd(), filename), 'utf-8'); -const component = reactDocs.parse(content); -console.log('| Property | PropType | Required | Default | Description |'); -console.log('|----------|----------|----------|---------|-------------|'); +try { + const content = fs.readFileSync(path.resolve(process.cwd(), filename), 'utf-8'); + const components = reactDocs.parse(content, reactDocs.resolver.findAllComponentDefinitions); -for (const name of Object.keys(component.props)) { - const { type, required, description, defaultValue } = component.props[name]; - console.log(`| ${name} | \`${formatType(type)}\` | ${required ? 'yes' : ''} | ${defaultValue != null ? `\`${nlToBr(defaultValue.value, true)}\`` : ''} | ${nlToBr(description)} |`); -} + components.forEach((component) => { -function formatType (type) { - if (type.name === 'union') { - return type.value.map(formatType).join(' || '); - } else { - return type.name; - } -} + const title = components.length > 1 ? `## ${component.displayName || ''} Properties` : '## Properties'; + console.log(title); + console.log(''); + + if (component.props) { + console.log('| Property | PropType | Required | Default | Description |'); + console.log('|----------|----------|----------|---------|-------------|'); + + for (const name of Object.keys(component.props)) { + const { type, required, description, defaultValue } = component.props[name]; + console.log(`| ${name} | \`${formatType(type)}\` | ${required ? 'yes' : ''} | ${defaultValue != null ? `\`${nlToBr(defaultValue.value, true)}\`` : ''} | ${nlToBr(description)} |`); + } + } else { + console.log('This component has no properties'); + } -function nlToBr(str, addQuotes = false) { - return str.replace(/\r\n|\r|\n/gi, addQuotes ? '`
`' : '
'); + console.log(''); + }); +} catch (e) { + console.warn(`Can't extract props data from file ${filename}: ${e.message}`); } diff --git a/package-lock.json b/package-lock.json index c7df0f8..7010616 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@rianbotha/react-props-md-table", - "version": "1.0.2", + "version": "2.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -13,17 +13,17 @@ } }, "@babel/core": { - "version": "7.3.3", - "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.3.3.tgz", - "integrity": "sha512-w445QGI2qd0E0GlSnq6huRZWPMmQGCp5gd5ZWS4hagn0EiwzxD5QMFkpchyusAyVC1n27OKXzQ0/88aVU9n4xQ==", + "version": "7.4.3", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.4.3.tgz", + "integrity": "sha512-oDpASqKFlbspQfzAE7yaeTmdljSH2ADIvBlb0RwbStltTuWa0+7CCI1fYVINNv9saHPa1W7oaKeuNuKj+RQCvA==", "requires": { "@babel/code-frame": "^7.0.0", - "@babel/generator": "^7.3.3", - "@babel/helpers": "^7.2.0", - "@babel/parser": "^7.3.3", - "@babel/template": "^7.2.2", - "@babel/traverse": "^7.2.2", - "@babel/types": "^7.3.3", + "@babel/generator": "^7.4.0", + "@babel/helpers": "^7.4.3", + "@babel/parser": "^7.4.3", + "@babel/template": "^7.4.0", + "@babel/traverse": "^7.4.3", + "@babel/types": "^7.4.0", "convert-source-map": "^1.1.0", "debug": "^4.1.0", "json5": "^2.1.0", @@ -34,11 +34,11 @@ } }, "@babel/generator": { - "version": "7.3.3", - "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.3.3.tgz", - "integrity": "sha512-aEADYwRRZjJyMnKN7llGIlircxTCofm3dtV5pmY6ob18MSIuipHpA2yZWkPlycwu5HJcx/pADS3zssd8eY7/6A==", + "version": "7.4.0", + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.4.0.tgz", + "integrity": "sha512-/v5I+a1jhGSKLgZDcmAUZ4K/VePi43eRkUs3yePW1HB1iANOD5tqJXwGSG4BZhSksP8J9ejSlwGeTiiOFZOrXQ==", "requires": { - "@babel/types": "^7.3.3", + "@babel/types": "^7.4.0", "jsesc": "^2.5.1", "lodash": "^4.17.11", "source-map": "^0.5.0", @@ -64,21 +64,21 @@ } }, "@babel/helper-split-export-declaration": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.0.0.tgz", - "integrity": "sha512-MXkOJqva62dfC0w85mEf/LucPPS/1+04nmmRMPEBUB++hiiThQ2zPtX/mEWQ3mtzCEjIJvPY8nuwxXtQeQwUag==", + "version": "7.4.0", + "resolved": "https://registry.npmjs.org/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.4.0.tgz", + "integrity": "sha512-7Cuc6JZiYShaZnybDmfwhY4UYHzI6rlqhWjaIqbsJGsIqPimEYy5uh3akSRLMg65LSdSEnJ8a8/bWQN6u2oMGw==", "requires": { - "@babel/types": "^7.0.0" + "@babel/types": "^7.4.0" } }, "@babel/helpers": { - "version": "7.3.1", - "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.3.1.tgz", - "integrity": "sha512-Q82R3jKsVpUV99mgX50gOPCWwco9Ec5Iln/8Vyu4osNIOQgSrd9RFrQeUvmvddFNoLwMyOUWU+5ckioEKpDoGA==", + "version": "7.4.3", + "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.4.3.tgz", + "integrity": "sha512-BMh7X0oZqb36CfyhvtbSmcWc3GXocfxv3yNsAEuM0l+fAqSO22rQrUpijr3oE/10jCTrB6/0b9kzmG4VetCj8Q==", "requires": { - "@babel/template": "^7.1.2", - "@babel/traverse": "^7.1.5", - "@babel/types": "^7.3.0" + "@babel/template": "^7.4.0", + "@babel/traverse": "^7.4.3", + "@babel/types": "^7.4.0" } }, "@babel/highlight": { @@ -92,48 +92,48 @@ } }, "@babel/parser": { - "version": "7.3.3", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.3.3.tgz", - "integrity": "sha512-xsH1CJoln2r74hR+y7cg2B5JCPaTh+Hd+EbBRk9nWGSNspuo6krjhX0Om6RnRQuIvFq8wVXCLKH3kwKDYhanSg==" + "version": "7.4.3", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.4.3.tgz", + "integrity": "sha512-gxpEUhTS1sGA63EGQGuA+WESPR/6tz6ng7tSHFCmaTJK/cGK8y37cBTspX+U2xCAue2IQVvF6Z0oigmjwD8YGQ==" }, "@babel/runtime": { - "version": "7.3.1", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.3.1.tgz", - "integrity": "sha512-7jGW8ppV0ant637pIqAcFfQDDH1orEPGJb8aXfUozuCU3QqX7rX4DA8iwrbPrR1hcH0FTTHz47yQnk+bl5xHQA==", + "version": "7.4.3", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.4.3.tgz", + "integrity": "sha512-9lsJwJLxDh/T3Q3SZszfWOTkk3pHbkmH+3KY+zwIDmsNlxsumuhS2TH3NIpktU4kNvfzy+k3eLT7aTJSPTo0OA==", "requires": { - "regenerator-runtime": "^0.12.0" + "regenerator-runtime": "^0.13.2" } }, "@babel/template": { - "version": "7.2.2", - "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.2.2.tgz", - "integrity": "sha512-zRL0IMM02AUDwghf5LMSSDEz7sBCO2YnNmpg3uWTZj/v1rcG2BmQUvaGU8GhU8BvfMh1k2KIAYZ7Ji9KXPUg7g==", + "version": "7.4.0", + "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.4.0.tgz", + "integrity": "sha512-SOWwxxClTTh5NdbbYZ0BmaBVzxzTh2tO/TeLTbF6MO6EzVhHTnff8CdBXx3mEtazFBoysmEM6GU/wF+SuSx4Fw==", "requires": { "@babel/code-frame": "^7.0.0", - "@babel/parser": "^7.2.2", - "@babel/types": "^7.2.2" + "@babel/parser": "^7.4.0", + "@babel/types": "^7.4.0" } }, "@babel/traverse": { - "version": "7.2.3", - "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.2.3.tgz", - "integrity": "sha512-Z31oUD/fJvEWVR0lNZtfgvVt512ForCTNKYcJBGbPb1QZfve4WGH8Wsy7+Mev33/45fhP/hwQtvgusNdcCMgSw==", + "version": "7.4.3", + "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.4.3.tgz", + "integrity": "sha512-HmA01qrtaCwwJWpSKpA948cBvU5BrmviAief/b3AVw936DtcdsTexlbyzNuDnthwhOQ37xshn7hvQaEQk7ISYQ==", "requires": { "@babel/code-frame": "^7.0.0", - "@babel/generator": "^7.2.2", + "@babel/generator": "^7.4.0", "@babel/helper-function-name": "^7.1.0", - "@babel/helper-split-export-declaration": "^7.0.0", - "@babel/parser": "^7.2.3", - "@babel/types": "^7.2.2", + "@babel/helper-split-export-declaration": "^7.4.0", + "@babel/parser": "^7.4.3", + "@babel/types": "^7.4.0", "debug": "^4.1.0", "globals": "^11.1.0", - "lodash": "^4.17.10" + "lodash": "^4.17.11" } }, "@babel/types": { - "version": "7.3.3", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.3.3.tgz", - "integrity": "sha512-2tACZ80Wg09UnPg5uGAOUvvInaqLk3l/IAhQzlxLQOIXacr6bMsra5SH6AWw/hIDRCSbCdHP2KzSOD+cT7TzMQ==", + "version": "7.4.0", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.4.0.tgz", + "integrity": "sha512-aPvkXyU2SPOnztlgo8n9cEiXW755mgyvueUPcpStqdzoSPm0fjO0vQBjLkt3JKJW7ufikfcnMTTPsN1xaTsBPA==", "requires": { "esutils": "^2.0.2", "lodash": "^4.17.11", @@ -149,9 +149,9 @@ } }, "ast-types": { - "version": "0.12.2", - "resolved": "https://registry.npmjs.org/ast-types/-/ast-types-0.12.2.tgz", - "integrity": "sha512-8c83xDLJM/dLDyXNLiR6afRRm4dPKN6KAnKqytRK3DBJul9lA+atxdQkNDkSVPdTqea5HiRq3lnnOIZ0MBpvdg==" + "version": "0.12.3", + "resolved": "https://registry.npmjs.org/ast-types/-/ast-types-0.12.3.tgz", + "integrity": "sha512-wJUcAfrdW+IgDoMGNz5MmcvahKgB7BwIbLupdKVVHxHNYt+HVR2k35swdYNv9aZpF8nvlkjbnkp2rrNwxGckZA==" }, "async": { "version": "2.6.2", @@ -199,9 +199,9 @@ "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, "commander": { - "version": "2.19.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.19.0.tgz", - "integrity": "sha512-6tvAOO+D6OENvRAh524Dh9jcfKTYDQAqvqezbCW82xj5X0pSrcpxtvRKHLG0yBY6SD7PSDrJaj+0AiOcKVd1Xg==" + "version": "2.20.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.0.tgz", + "integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==" }, "concat-map": { "version": "0.0.1", @@ -317,9 +317,9 @@ "integrity": "sha512-VvivMrbvd2nKkiG38qjULzlc+4Vx4wm/whI9pQD35YrARNnhxeiRktSOhSukRLFNlzg6Br/cJPet5J/u19r/mg==" }, "react-docgen": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/react-docgen/-/react-docgen-4.0.0.tgz", - "integrity": "sha512-uA6BlsUD2nigCUj3IPY+/W4h6Y0H72zeVg2GE6IZpxjyfcUzTbjl5AO5ND4knG/8V5MGRPOHfTcaBWJcgLmPYA==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/react-docgen/-/react-docgen-4.1.0.tgz", + "integrity": "sha512-yoGY45tD4yPGmnOlJr4mjU6LTYRO1OEny9ZtK8LjnFMqr2M/uRuH88vPba7743oVDFuVNH5bnN2bh6eQX3qXjA==", "requires": { "@babel/core": "^7.0.0", "@babel/runtime": "^7.0.0", @@ -331,11 +331,11 @@ } }, "recast": { - "version": "0.17.3", - "resolved": "https://registry.npmjs.org/recast/-/recast-0.17.3.tgz", - "integrity": "sha512-NwQguXPwHqaVb6M7tsY11+8RDoAKHGRdymPGDxHJrsxOlNADQh0b08uz/MgYp1R1wmHuSBK4A4I5Oq+cE1J40g==", + "version": "0.17.5", + "resolved": "https://registry.npmjs.org/recast/-/recast-0.17.5.tgz", + "integrity": "sha512-K+DgfAMIyEjNKjaFSWgg9TTu7wFgU/4KTyw4E9vl6M5QPDuUYbyt49Yzb0EIDbZks+6lXk/UZ9eTuE4jlLyf2A==", "requires": { - "ast-types": "0.12.2", + "ast-types": "0.12.3", "esprima": "~4.0.0", "private": "^0.1.8", "source-map": "~0.6.1" @@ -349,14 +349,14 @@ } }, "regenerator-runtime": { - "version": "0.12.1", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.12.1.tgz", - "integrity": "sha512-odxIc1/vDlo4iZcfXqRYFj0vpXFNoGdKMAUieAlFYO6m/nl5e9KR/beGf41z4a1FI+aQgtjhuaSlDxQ0hmkrHg==" + "version": "0.13.2", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.2.tgz", + "integrity": "sha512-S/TQAZJO+D3m9xeN1WTI8dLKBBiRgXBlTJvbWjCThHWZj9EvHK70Ff50/tYj2J/fvBY6JtFVwRuazHN2E7M9BA==" }, "resolve": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.10.0.tgz", - "integrity": "sha512-3sUr9aq5OfSg2S9pNtPA9hL1FVEAjvfOC4leW0SNf/mpnaakz2a9femSd6LqAww2RaFctwyf1lCqnTHuF1rxDg==", + "version": "1.10.1", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.10.1.tgz", + "integrity": "sha512-KuIe4mf++td/eFb6wkaPbMDnP6kObCaEtIDuHOUED6MNUo4K670KZUHuuvYPZDxNF0WVLw49n06M2m2dXphEzA==", "requires": { "path-parse": "^1.0.6" } @@ -367,9 +367,9 @@ "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, "semver": { - "version": "5.6.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz", - "integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==" + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", + "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==" }, "source-map": { "version": "0.5.7", diff --git a/package.json b/package.json index 1243587..b4dc0a0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@rianbotha/react-props-md-table", - "version": "1.0.2", - "description": "A markdown table generator for React component props forked from TeamWerterbyte.", + "version": "2.0.0", + "description": "A markdown table generator for React component props.", "main": "index.js", "bin": { "props-table": "index.js" @@ -21,6 +21,6 @@ "license": "MIT", "homepage": "https://github.com/rianbotha/react-props-md-table#readme", "dependencies": { - "react-docgen": "^4.0.0" + "react-docgen": "^4.1.0" } }