From 5dda34c12774568e9923d754b39dabb2b665a688 Mon Sep 17 00:00:00 2001 From: Alan Date: Thu, 6 Jun 2019 09:18:22 +1000 Subject: [PATCH] Documentation script update. (#377) * Update npm run docs to work with linebreaks in story add() function. * Include import documentation for components in no-ssr folder. --- scripts/generate-readme/README_template.md | 8 +++ scripts/generate-readme/generate-readme.js | 22 ++++++-- scripts/generate-readme/get-imports.js | 63 ++++++++++++---------- 3 files changed, 63 insertions(+), 30 deletions(-) diff --git a/scripts/generate-readme/README_template.md b/scripts/generate-readme/README_template.md index 18ab0ea6f..74d6f9be3 100644 --- a/scripts/generate-readme/README_template.md +++ b/scripts/generate-readme/README_template.md @@ -27,6 +27,14 @@ npm install {{ packageName }} --save {{ packageImports }} ``` +{% if noSsrPackageImports %} +## Import for client-only components + +```js +{{ noSsrPackageImports }} +``` + +{% endif %} -------------------------------------------------------------------------------- ## Usage and Tests diff --git a/scripts/generate-readme/generate-readme.js b/scripts/generate-readme/generate-readme.js index 33379270a..2873038ea 100644 --- a/scripts/generate-readme/generate-readme.js +++ b/scripts/generate-readme/generate-readme.js @@ -23,7 +23,7 @@ let fs = require('fs') let path = require('path') let glob = require('glob-fs')({ gitignore: true }) let wrap = require('wordwrap')(80) -let getImports = require('./get-imports') +let { getImportStatements, getImportStatementFromJS } = require('./get-imports') function generateTemplate (directory) { let data = { @@ -31,11 +31,12 @@ function generateTemplate (directory) { packageDescription: '', packageDependencies: '', packageImports: '', + noSsrPackageImports: '', storybookParams: [] } // Get imports. - let imports = getImports(directory + 'package.json') + let imports = getImportStatements(directory + 'package.json') imports.forEach((importStatement, idx) => { let print = importStatement if (print.length > 80) { @@ -51,6 +52,19 @@ function generateTemplate (directory) { data.packageName = wrap(jsonFile.name) data.packageDescription = wrap(jsonFile.description) + // Get no-ssr imports. + if (fs.existsSync(directory + 'no-ssr/index.js')) { + let nossrImport = fs.readFileSync(directory + 'no-ssr/index.js', 'utf-8') + const nossrImports = getImportStatementFromJS(nossrImport, data.packageName + '/no-ssr') + nossrImports.forEach((importStatement, idx) => { + let print = importStatement + if (print.length > 80) { + print = print.replace(/,\s/gi, ',\n ').replace(/{ /gi, '{\n ').replace(/ }/gi, '\n}') + } + data.noSsrPackageImports += ((idx > 0) ? '\n' : '') + print + }) + } + // Get dependencies. let deps = [] for (let key in jsonFile.dependencies) { @@ -70,7 +84,7 @@ function generateTemplate (directory) { let storiesOf = /(?:storiesOf\(')(.+)(?:',)/g let storiesOfMatch = storiesOf.exec(story) - let add = /(?:add\(')(.+)(?:',)/gim + let add = /(?:add\([\n\s]*)(?:')(.+)(?:',)/gim let addMatch = add.exec(story) while (addMatch !== null) { data.storybookParams.push({ @@ -80,6 +94,8 @@ function generateTemplate (directory) { addMatch = add.exec(story) } + // Add check for no-ssr folder. + // Render template. Twig.renderFile(path.join(__dirname, '/README_template.md'), data, (err, html) => { if (err) { diff --git a/scripts/generate-readme/get-imports.js b/scripts/generate-readme/get-imports.js index 15e8bd917..46dfa5437 100644 --- a/scripts/generate-readme/get-imports.js +++ b/scripts/generate-readme/get-imports.js @@ -1,7 +1,6 @@ let fs = require('fs') function getImportStatements (pkgdirectory) { - const importStatements = [] const pkg = fs.readFileSync(pkgdirectory, 'utf-8') const packageData = JSON.parse(pkg) const packageMain = packageData.main @@ -9,40 +8,50 @@ function getImportStatements (pkgdirectory) { const importedFile = fs.readFileSync(pkgdirectory.replace('package.json', packageMain), 'utf-8') if (packageMain.indexOf('.js') > -1) { - const reg = new RegExp('export\\s({|default)\\s*([A-Za-z]+)', 'gi') - const batchCurlies = [] - let importDefault = null - - // Match - let lastMatch = reg.exec(importedFile) - while (lastMatch !== null) { - if (lastMatch && lastMatch.length === 3) { - const type = lastMatch[1] - const name = lastMatch[2] - if (type === '{') { - batchCurlies.push(name) - } else { - importDefault = name - } - } - lastMatch = reg.exec(importedFile) - } - // Add to array - if (batchCurlies.length > 0) { - importStatements.push(`import { ${batchCurlies.toString().replace(/,/gi, ', ')} } from '${packageName}'`) - } - if (importDefault !== null && batchCurlies.indexOf(importDefault) < 0) { - importStatements.push(`import ${importDefault} from '${packageName}'`) - } + return getImportStatementFromJS(importedFile, packageName) } else { // Assume a .vue file. + const importStatements = [] const reg = new RegExp('name: \'(.*)\'', 'gi') let lastMatch = reg.exec(importedFile) if (lastMatch !== null) { importStatements.push(`import ${lastMatch[1]} from '${packageName}'`) } + return importStatements + } +} + +function getImportStatementFromJS (importedFile, packageName) { + const importStatements = [] + const reg = new RegExp('export\\s({|default)\\s*([A-Za-z]+)', 'gi') + const batchCurlies = [] + let importDefault = null + + // Match + let lastMatch = reg.exec(importedFile) + while (lastMatch !== null) { + if (lastMatch && lastMatch.length === 3) { + const type = lastMatch[1] + const name = lastMatch[2] + if (type === '{') { + batchCurlies.push(name) + } else { + importDefault = name + } + } + lastMatch = reg.exec(importedFile) + } + // Add to array + if (batchCurlies.length > 0) { + importStatements.push(`import { ${batchCurlies.toString().replace(/,/gi, ', ')} } from '${packageName}'`) + } + if (importDefault !== null && batchCurlies.indexOf(importDefault) < 0) { + importStatements.push(`import ${importDefault} from '${packageName}'`) } return importStatements } -module.exports = getImportStatements +module.exports = { + getImportStatements: getImportStatements, + getImportStatementFromJS: getImportStatementFromJS +}