Skip to content

Commit

Permalink
feat: caching of props-info parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 11, 2020
1 parent 051341d commit be24d25
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 48 deletions.
4 changes: 3 additions & 1 deletion core/instrument/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@storybook/csf": "^0.0.1",
"crypto": "^1.0.1",
"deepmerge": "^4.2.2",
"find-cache-dir": "^3.3.1",
"hosted-git-info": "^3.0.4",
"resolve": "^1.15.1",
"prettier": "^1.19.1",
Expand All @@ -48,8 +49,9 @@
"devDependencies": {
"@babel/types": "^7.8.3",
"@rollup/plugin-node-resolve": "^7.1.1",
"@types/jest": "^25.1.2",
"@types/find-cache-dir": "^3.2.0",
"@types/hosted-git-info": "^3.0.0",
"@types/jest": "^25.1.2",
"cross-env": "^5.2.1",
"docz-rollup": "^2.1.0",
"eslint": "^6.5.1",
Expand Down
48 changes: 4 additions & 44 deletions core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,12 @@ import {
StoryAttributes,
StoryComponent,
StoriesKind,
ComponentInfo,
} from '@component-controls/specification';
import { followImports } from './follow-imports';
import { packageInfo } from '../project/packageInfo';
import {
InstrumentOptions,
PropsLoaderConfig,
PropsInfoExtractorFunction,
} from '../types';

const extractComponentProps = async (
options: PropsLoaderConfig[],
filePath: string,
componentName?: string,
source?: string,
): Promise<ComponentInfo | undefined> => {
const loaders = options.filter(loader => {
const include = Array.isArray(loader.use)
? loader.use
: loader.use
? [loader.use]
: undefined;
const exclude = Array.isArray(loader.exclude)
? loader.exclude
: loader.exclude
? [loader.exclude]
: undefined;
return (
include &&
include.some(mask => filePath.match(mask)) &&
(!exclude || !exclude.some(mask => filePath.match(mask)))
);
});
import { packageInfo } from '../misc/packageInfo';
import { propsInfo } from '../misc/propsInfo';
import { InstrumentOptions } from '../types';

if (loaders.length > 1) {
console.error(`Multiple propsloaders found for file ${filePath}`);
}
const propsLoaderName = loaders.length === 1 ? loaders[0] : undefined;
if (propsLoaderName) {
const propsLoader: PropsInfoExtractorFunction = require(propsLoaderName.name)(
propsLoaderName.options,
);
return await propsLoader(filePath, componentName, source);
}
return undefined;
};
const componentFromParams = (
attributes?: StoryAttributes,
): string | undefined => {
Expand Down Expand Up @@ -116,7 +76,7 @@ export const extractComponent = async (
};
const { propsLoaders } = options || {};
if (follow && follow.filePath && Array.isArray(propsLoaders)) {
const info = await extractComponentProps(
const info = await propsInfo(
propsLoaders,
follow.filePath,
follow.importedName,
Expand Down
2 changes: 1 addition & 1 deletion core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { extractCSFStories } from './babel/csf-stories';
import { extractMDXStories } from './babel/mdx-stories';
import { removeMDXAttributes } from './babel/remove-mdx-attributes';
import { extractStoreComponent } from './babel/extract-component';
import { packageInfo } from './project/packageInfo';
import { packageInfo } from './misc/packageInfo';
import {
InstrumentOptions,
InstrumentOptionsMDX,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import path from 'path';
import * as fs from 'fs';
import * as path from 'path';
//@ts-ignore
import readJson from 'read-package-json';
import hostedGitInfo from 'hosted-git-info';
Expand Down
68 changes: 68 additions & 0 deletions core/instrument/src/misc/propsInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { createHash } from 'crypto';
import findCacheDir from 'find-cache-dir';
import { ComponentInfo } from '@component-controls/specification';
import { PropsLoaderConfig, PropsInfoExtractorFunction } from '../types';

export const propsInfo = async (
options: PropsLoaderConfig[],
filePath: string,
componentName?: string,
source?: string,
): Promise<ComponentInfo | undefined> => {
const cacheFolder =
findCacheDir({ name: 'component-controls-props-info' }) || os.tmpdir();

//create cache folder if it doesnt exist
if (!fs.existsSync(cacheFolder)) {
fs.mkdirSync(cacheFolder, { recursive: true });
}
const cachedFileName = path.join(
cacheFolder,
createHash('md5')
.update(filePath)
.digest('hex'),
);

if (fs.existsSync(cachedFileName)) {
const cacheStats = fs.statSync(cachedFileName);
const fileStats = fs.statSync(filePath);
if (cacheStats.mtime.getTime() >= fileStats.mtime.getTime()) {
const fileData = fs.readFileSync(cachedFileName, 'utf8');
return JSON.parse(fileData);
}
}
let result: ComponentInfo | undefined = undefined;
const loaders = options.filter(loader => {
const include = Array.isArray(loader.use)
? loader.use
: loader.use
? [loader.use]
: undefined;
const exclude = Array.isArray(loader.exclude)
? loader.exclude
: loader.exclude
? [loader.exclude]
: undefined;
return (
include &&
include.some(mask => filePath.match(mask)) &&
(!exclude || !exclude.some(mask => filePath.match(mask)))
);
});

if (loaders.length > 1) {
console.error(`Multiple propsloaders found for file ${filePath}`);
}
const propsLoaderName = loaders.length === 1 ? loaders[0] : undefined;
if (propsLoaderName) {
const propsLoader: PropsInfoExtractorFunction = require(propsLoaderName.name)(
propsLoaderName.options,
);
result = await propsLoader(filePath, componentName, source);
}
fs.writeFileSync(cachedFileName, JSON.stringify(result || {}));
return result;
};
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3302,6 +3302,11 @@
resolved "https://registry.yarnpkg.com/@types/faker/-/faker-4.1.10.tgz#ec31466931086122b05be719d084989ffe3d6eb6"
integrity sha512-Z1UXXNyxUcuu7CSeRmVizMgH7zVYiwfiTgXMnSTvsYDUnVt3dbMSpPdfG/H41IBiclgFGQJgLVdDFeinhhmWTg==

"@types/find-cache-dir@^3.2.0":
version "3.2.0"
resolved "https://registry.yarnpkg.com/@types/find-cache-dir/-/find-cache-dir-3.2.0.tgz#eaaf331699dccf52c47926e4d4f8f3ed8db33f3c"
integrity sha512-+JeT9qb2Jwzw72WdjU+TSvD5O1QRPWCeRpDJV+guiIq+2hwR0DFGw+nZNbTFjMIVe6Bf4GgAKeB/6Ytx6+MbeQ==

"@types/fs-extra@^8.0.0":
version "8.1.0"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-8.1.0.tgz#1114834b53c3914806cd03b3304b37b3bd221a4d"
Expand Down Expand Up @@ -7766,6 +7771,15 @@ find-cache-dir@^3.0.0, find-cache-dir@^3.2.0:
make-dir "^3.0.2"
pkg-dir "^4.1.0"

find-cache-dir@^3.3.1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-3.3.1.tgz#89b33fad4a4670daa94f855f7fbe31d6d84fe880"
integrity sha512-t2GDMt3oGC/v+BMwzmllWDuJF/xcDtE5j/fCGbqDD7OLuJkj0cfh1YSA5VKPvwMeLFLNDBkwOKZ2X85jGLVftQ==
dependencies:
commondir "^1.0.1"
make-dir "^3.0.2"
pkg-dir "^4.1.0"

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/find-node-modules/-/find-node-modules-2.0.0.tgz#5db1fb9e668a3d451db3d618cd167cdd59e41b69"
Expand Down

0 comments on commit be24d25

Please sign in to comment.