Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 9bf84c1

Browse files
authored
build(docs): use caching for build:docs:docgen (#485)
* build(docs): use caching for `build:docs:docgen` * make name more strict
1 parent f240d7a commit 9bf84c1

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

build/gulp/plugins/gulp-react-docgen.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from 'path'
33
import * as through2 from 'through2'
44
import * as Vinyl from 'vinyl'
55

6-
import { getComponentInfo } from './util'
6+
import { getComponentInfo, getBufferChecksum, getInfoChecksum } from './util'
77

88
const pluginName = 'gulp-react-docgen'
99

@@ -19,11 +19,18 @@ export default () =>
1919
return
2020
}
2121

22-
try {
23-
const contents = getComponentInfo(file.path)
22+
const infoFilename = file.basename.replace(/\.tsx$/, '.info.json')
23+
const nextChecksum = getBufferChecksum(file.contents)
2424

25+
if (getInfoChecksum(infoFilename) === nextChecksum) {
26+
cb(null)
27+
return
28+
}
29+
30+
try {
31+
const contents = getComponentInfo(file.path, nextChecksum)
2532
const infoFile = new Vinyl({
26-
path: `./${file.basename.replace(/tsx$/, 'info.json')}`,
33+
path: `./${infoFilename}`,
2734
contents: Buffer.from(JSON.stringify(contents, null, 2)),
2835
})
2936

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as fs from 'fs'
2+
import * as crypto from 'crypto'
3+
4+
import config from '../../../../config'
5+
6+
export const getInfoChecksum = (filename: string): string | null => {
7+
try {
8+
const buffer = fs.readFileSync(config.paths.docsSrc('componentInfo', filename))
9+
const componentInfo = JSON.parse(buffer.toString())
10+
11+
return componentInfo.checksum
12+
} catch (e) {}
13+
14+
return null
15+
}
16+
17+
export const getBufferChecksum = (buffer: Buffer): string =>
18+
crypto
19+
.createHash('md5')
20+
.update(buffer)
21+
.digest('hex')

build/gulp/plugins/util/getComponentInfo.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import parseDocblock from './parseDocblock'
66
import parseType from './parseType'
77
import * as reactDocgenTypescript from 'react-docgen-typescript'
88

9-
const getComponentInfo = filepath => {
9+
const getComponentInfo = (filepath: string, checksum?: string) => {
1010
const absPath = path.resolve(process.cwd(), filepath)
1111

1212
const dir = path.dirname(absPath)
@@ -37,6 +37,9 @@ const getComponentInfo = filepath => {
3737
// remove keys we don't use
3838
delete info.methods
3939

40+
// add checksum
41+
info.checksum = checksum
42+
4043
// add exported Component info
4144
const Component = require(absPath).default
4245
info.constructorName = _.get(Component, 'prototype.constructor.name', null)

build/gulp/plugins/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './checksumUtils'
12
export { default as getComponentInfo } from './getComponentInfo'
23
export { default as parseDefaultValue } from './parseDefaultValue'
34
export { default as parseDocblock } from './parseDocblock'

build/gulp/tasks/docs.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ const handleWatchUnlink = (group, path) => {
3030
// Clean
3131
// ----------------------------------------
3232

33-
task('clean:docs:component-info', cb => {
34-
rimraf(paths.docsSrc('componentInfo'), cb)
35-
})
36-
3733
task('clean:docs:component-menu', cb => {
3834
rimraf(paths.docsSrc('componentMenu.json'), cb)
3935
})
@@ -53,7 +49,6 @@ task('clean:docs:example-menus', cb => {
5349
task(
5450
'clean:docs',
5551
parallel(
56-
'clean:docs:component-info',
5752
'clean:docs:component-menu',
5853
'clean:docs:component-menu-behaviors',
5954
'clean:docs:dist',

0 commit comments

Comments
 (0)