Skip to content

Commit

Permalink
Documentation script update. (#377)
Browse files Browse the repository at this point in the history
* Update npm run docs to work with linebreaks in story add() function.

* Include import documentation for components in no-ssr folder.
  • Loading branch information
alan-cole authored and tim-yao committed Jun 5, 2019
1 parent 5966a3a commit 5dda34c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 30 deletions.
8 changes: 8 additions & 0 deletions scripts/generate-readme/README_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ npm install {{ packageName }} --save
{{ packageImports }}
```

{% if noSsrPackageImports %}
## Import for client-only components

```js
{{ noSsrPackageImports }}
```

{% endif %}
--------------------------------------------------------------------------------

## Usage and Tests
Expand Down
22 changes: 19 additions & 3 deletions scripts/generate-readme/generate-readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ 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 = {
packageName: '',
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) {
Expand All @@ -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) {
Expand All @@ -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({
Expand All @@ -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) {
Expand Down
63 changes: 36 additions & 27 deletions scripts/generate-readme/get-imports.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
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
const packageName = packageData.name
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
}

0 comments on commit 5dda34c

Please sign in to comment.