-
Notifications
You must be signed in to change notification settings - Fork 832
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
189 additions
and
0 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
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,66 @@ | ||
#!/usr/bin/env node | ||
'use strict' | ||
/** | ||
* This script scaffolds a bare bones docs template inside of a package. | ||
* | ||
* For the following command: | ||
* | ||
* `yarn run create-docs-template package-name` | ||
* | ||
* The following file tree will be generated: | ||
* | ||
* /src/pacakage-name/docs | ||
* ├── index.js | ||
* └── /examples/ | ||
* └── {ComponentName}-basic.example | ||
* | ||
*/ | ||
|
||
const path = require('path') | ||
const fs = require('fs-extra') | ||
const task = require('./task') | ||
const docsIndexTemplate = require('./docs-index-template') | ||
|
||
const packageName = process.argv[2] | ||
|
||
module.exports = task('create-docs-template', async () => { | ||
const componentNames = [...process.argv] | ||
componentNames.splice(0, 3) | ||
|
||
console.log('component names:', componentNames) | ||
|
||
if (!packageName) { | ||
throw new Error( | ||
'Missing package name argument, use: `yarn run create-docs-template [package-name]`' | ||
) | ||
} | ||
|
||
const packageDir = path.join('src', packageName) | ||
const docsDir = path.join(packageDir, 'docs') | ||
const examplesDir = path.join(docsDir, 'examples') | ||
|
||
// Check if directory already exist | ||
const docsDirExistsAlready = await fs.pathExists(docsDir) | ||
|
||
if (docsDirExistsAlready) { | ||
throw new Error(`Directory already exists: ${docsDir}`) | ||
} | ||
|
||
// Create directory | ||
await fs.ensureDir(docsDir) | ||
await fs.ensureDir(examplesDir) | ||
|
||
await fs.writeFile( | ||
path.join(docsDir, 'index.js'), | ||
docsIndexTemplate({ packageName, componentNames }) | ||
) | ||
|
||
await Promise.all( | ||
componentNames.map(async name => { | ||
return fs.writeFile( | ||
path.join(examplesDir, `${name}-basic.example`), | ||
`<${name} />` | ||
) | ||
}) | ||
) | ||
}) |
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,96 @@ | ||
'use strict' | ||
|
||
function getRawImports(componentNames) { | ||
return componentNames | ||
.map(name => { | ||
return `import source${name} from '!raw-loader!../src/${name}'` | ||
}) | ||
.join('\n') | ||
} | ||
|
||
function getImports(componentNames) { | ||
return componentNames | ||
.map(name => { | ||
return `import ${name} from '../src/${name}'` | ||
}) | ||
.join('\n') | ||
} | ||
|
||
function getCodeExamples(componentNames) { | ||
return componentNames | ||
.map(name => { | ||
return `import example${name}Basic from './examples/${name}-basic.example'` | ||
}) | ||
.join('\n') | ||
} | ||
|
||
function getComponents(componentNames) { | ||
return componentNames.map( | ||
name => ` | ||
{ | ||
name: '${name}', | ||
source: source${name}, | ||
description: ( | ||
<p> | ||
The <code>${name}</code> component. | ||
</p> | ||
), | ||
examples: [ | ||
{ | ||
title: 'Basic ${name} Example', | ||
codeText: example${name}Basic, | ||
scope, | ||
}, | ||
], | ||
}, | ||
` | ||
) | ||
} | ||
|
||
module.exports = ({ packageName, componentNames }) => { | ||
return ` | ||
import React from 'react' | ||
import Box from 'ui-box' | ||
${getImports(componentNames)} | ||
/* eslint-disable import/no-unresolved, import/no-webpack-loader-syntax */ | ||
${getRawImports(componentNames)} | ||
/* eslint-enable import/no-unresolved, import/no-webpack-loader-syntax */ | ||
/** | ||
* Code examples | ||
*/ | ||
${getCodeExamples(componentNames)} | ||
const title = '${packageName}' | ||
const subTitle = 'A component.' | ||
const designGuidelines = ( | ||
<div> | ||
<p> | ||
The <code>${packageName}</code> component. | ||
</p> | ||
</div> | ||
) | ||
const appearanceOptions = null | ||
const scope = { | ||
Box, | ||
${componentNames.join(',\n ')} | ||
} | ||
const components = [ | ||
${getComponents(componentNames)} | ||
] | ||
export default { | ||
title, | ||
subTitle, | ||
designGuidelines, | ||
appearanceOptions, | ||
components, | ||
} | ||
`.trim() | ||
} |