forked from blai/react-component-illustrator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,100 @@ | ||
'use strict'; | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
import dox from 'dox'; | ||
import {parse as parseRectDoc} from 'react-docgen'; | ||
|
||
// --- | ||
|
||
export default class Illustrator { | ||
constructor() { | ||
constructor(options) { | ||
this.options = options; | ||
this.store = {}; | ||
} | ||
|
||
// --- | ||
|
||
record(key) { | ||
return (value) => { | ||
return this.store[key] = value; | ||
}; | ||
return value => this.store[key] = value; | ||
} | ||
|
||
processExample(file) { | ||
return Promise.resolve(file) | ||
.then(this.record('examplePath')) | ||
.then(() => this.relativePath(file)) | ||
.then(this.record('exampleRequirePath')) | ||
.then(() => fs.readFileSync(file, {encoding: 'utf-8'})) | ||
.then(this.record('exampleSource')) | ||
.then(this.parseExampleDoc) | ||
.then(this.record('exampleDoc')) | ||
; | ||
} | ||
|
||
parseExampleComments(code) { | ||
processComponent(file) { | ||
return Promise.resolve(file) | ||
.then(this.record('componentPath')) | ||
.then(() => { | ||
let componentPath = path.resolve(this.store.examplePath, file); | ||
if (!/\.js$/.test(componentPath)) { | ||
componentPath += '.js'; | ||
} | ||
return componentPath; | ||
}) | ||
.then(path => fs.readFileSync(path, {encoding: 'utf-8'})) | ||
.then(this.record('componentSource')) | ||
.then(this.parseComponentDoc) | ||
.then(this.record('componentDoc')) | ||
; | ||
} | ||
|
||
parseExampleDoc(code) { | ||
return dox.parseComments(code)[0]; | ||
} | ||
|
||
parseReactDoc(code) { | ||
parseComponentDoc(code) { | ||
return parseRectDoc(code); | ||
} | ||
|
||
relativePath() { | ||
let paths = Array.from(arguments, p => path.resolve(p)); | ||
paths.unshift(path.dirname(this.options.dest || path.resolve('.'))); | ||
|
||
let relative = path.relative.apply(path, paths); | ||
|
||
if (relative[0] !== '.') { | ||
relative = `.${path.sep}${relative}`; | ||
} | ||
|
||
return relative; | ||
} | ||
|
||
run() { | ||
return { | ||
comment: this.store.comment, | ||
componentPath: this.componentPath, | ||
documentation: this.store.documentation, | ||
exampleContent: this.store.exampleContent, | ||
examplePath: this.store.examplePath, | ||
sourceContent: this.store.sourceContent | ||
componentDoc: this.store.componentDoc, | ||
componentPath: this.store.componentPath, | ||
componentSource: this.store.componentSource, | ||
exampleDoc: this.store.exampleDoc, | ||
examplePath: this.store.examplePath, | ||
exampleRequirePath: this.store.exampleRequirePath, | ||
exampleSource: this.store.exampleSource | ||
}; | ||
} | ||
|
||
get component() { | ||
if (this.componentPath) { | ||
return this.componentPath; | ||
if (this.store.componentPath) { | ||
return this.store.componentPath; | ||
} | ||
|
||
if (!this.store.comment) { | ||
if (!this.store.exampleDoc) { | ||
return null; | ||
} | ||
|
||
return this.getCommentTag('component').string; | ||
var component = this.getCommentTag('component').string; | ||
return component ? path.resolve(this.store.examplePath, component) : null; | ||
} | ||
|
||
getCommentTag(name) { | ||
return this.store.comment.tags.find(tag => tag.type === name) || {}; | ||
return this.store.exampleDoc.tags.find(tag => tag.type === 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
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,24 +1,25 @@ | ||
|
||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
export function generateManifest(items) { | ||
items = items.map(digest); | ||
return `module.exports = [${items}];`; | ||
export function generateManifest(options, items) { | ||
if (options.dest) { | ||
items = items.map(digest); | ||
} | ||
|
||
let contents = `module.exports = [${items}];`; | ||
|
||
if (options.dest) { | ||
fs.writeFileSync(options.dest, contents); | ||
} else { | ||
console.log(contents); | ||
} | ||
|
||
return items; | ||
} | ||
|
||
export function digest(item) { | ||
var str = JSON.stringify(item); | ||
str = str.replace(/\}$/, `,renderer: require('${item.examplePath}')}`); | ||
str = str.replace(/\}$/, `,renderer: require('${item.exampleRequirePath}')}`); | ||
return str; | ||
} | ||
|
||
export function relativePath(from, to) { | ||
var destDir = path.dirname(from); | ||
var relative = path.relative(destDir, to); | ||
|
||
if (relative[0] !== '.') { | ||
relative = `.${path.sep}${relative}`; | ||
} | ||
|
||
return relative; | ||
} |