Skip to content

Commit

Permalink
feat: components history and catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Feb 17, 2021
1 parent d369821 commit 5e76046
Show file tree
Hide file tree
Showing 255 changed files with 14,939 additions and 10,721 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Expand Up @@ -112,7 +112,7 @@
"name": "jest instrument",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"cwd": "${workspaceFolder}/core/instrument",
"args": ["mdx-story-source"],
"args": ["file-info"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
Expand All @@ -126,7 +126,7 @@
"name": "jest react-docgen-typescript",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"cwd": "${workspaceFolder}/props-info/react-docgen-typescript",
"args": ["extends"],
"args": ["full"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
Expand Down
14 changes: 8 additions & 6 deletions core/config/test/__snapshots__/stories.test.ts.snap
Expand Up @@ -21,6 +21,7 @@ Array [
"/Users/atanasster/component-controls/ui/components/src/CopyContainer/CopyContainer.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Description/Description.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/ExternalLink/ExternalLink.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/GithubAvatarList/GithubAvatarList.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Header/Header.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/HoverBox/HoverBox.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Link/Link.stories.tsx",
Expand All @@ -34,19 +35,19 @@ Array [
"/Users/atanasster/component-controls/ui/components/src/SearchInput/SearchInput.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Sidebar/Sidebar.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Source/Source.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Subheading/Subjeading.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Subtitle/Subtitle.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/SyntaxHighlighter/SyntaxHighlighter.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Table/Table.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Tabs/Tabs.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Tag/Tag.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Title/Title.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/TitledImage/TitledImage.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Toggle/Toggle.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Tree/Tree.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Value/Value.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Zoom/Zoom.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentDependencies/ComponentDeps.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentContributors/ComponentContributors.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentDependencies/ExternalDependencies.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentDependencies/LocalDependencies.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentJSX/ComponentJSX.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentSource/ComponentSource.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/Container/Container.stories.tsx",
Expand Down Expand Up @@ -99,6 +100,7 @@ Array [
"/Users/atanasster/component-controls/ui/components/src/CopyContainer/CopyContainer.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Description/Description.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/ExternalLink/ExternalLink.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/GithubAvatarList/GithubAvatarList.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Header/Header.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/HoverBox/HoverBox.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Link/Link.stories.tsx",
Expand All @@ -112,19 +114,19 @@ Array [
"/Users/atanasster/component-controls/ui/components/src/SearchInput/SearchInput.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Sidebar/Sidebar.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Source/Source.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Subheading/Subjeading.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Subtitle/Subtitle.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/SyntaxHighlighter/SyntaxHighlighter.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Table/Table.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Tabs/Tabs.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Tag/Tag.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Title/Title.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/TitledImage/TitledImage.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Toggle/Toggle.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Tree/Tree.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Value/Value.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Zoom/Zoom.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentDependencies/ComponentDeps.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentContributors/ComponentContributors.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentDependencies/ExternalDependencies.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentDependencies/LocalDependencies.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentJSX/ComponentJSX.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentSource/ComponentSource.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/Container/Container.stories.tsx",
Expand Down
5 changes: 5 additions & 0 deletions core/core/src/common.ts
Expand Up @@ -54,4 +54,9 @@ export interface StoryProps<Props = unknown> {
* plugins configuration settings
*/
plugins?: any;

/**
* category string - can be used for cleanly separating stories/components
*/
category?: string;
}
11 changes: 11 additions & 0 deletions core/core/src/components.ts
@@ -1,4 +1,5 @@
import { CodeLocation, ImportType, Imports } from './utility';
import { FileInfo } from './files';

export type TypeValue =
| 'any'
Expand Down Expand Up @@ -126,6 +127,11 @@ export interface Component {
*/
request?: string;

/**
* file name with extension
*/
fileName?: string;

/**
* file containing the component's props info
* sometimes different from the component source file
Expand Down Expand Up @@ -165,6 +171,11 @@ export interface Component {
* jsx component tree
*/
jsx?: JSXTree;

/**
* source file info
*/
fileInfo?: FileInfo;
}
/**
* given a component, return its name
Expand Down
1 change: 1 addition & 0 deletions core/core/src/controls-utils.ts
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import escape from 'escape-html';
import { deepmerge } from './deepMerge';
import {
Expand Down
81 changes: 81 additions & 0 deletions core/core/src/files.ts
@@ -0,0 +1,81 @@
/**
* git commits fields
*/
export interface Commit {
commitHash?: string;
subject?: string;
authorName?: string;
authorData?: string;
authorEmail?: string;
committerName?: string;
committerDate?: string;
committerEmail?: string;
}

export type Commits = Commit[];

export interface FileInfo {
/**
* date file created
*/
dateCreated?: Date;

/**
* date last modified
*/
dateModified?: Date;

/**
* git commits for the file
*/
commits?: Commits;
/**
* number of lines of code
*/
sloc?: {
/**
* lines of block comments
*/
block: number;

/**
* empty lines of block comments
*/

blockEmpty: number;
/**
* total lines of comments
*/
comment: number;

/**
* empty lines
*/
empty: number;

/**
* lines mixed up with source and comments
*/
mixed: number;

/**
* lines with single-line comments
*/
single: number;

/**
* lines of code (source)
*/
source: number;

/**
* lines with TODO's
*/
todo: number;

/**
* physical total lines
*/
total: number;
};
}
1 change: 1 addition & 0 deletions core/core/src/index.ts
Expand Up @@ -8,6 +8,7 @@ export * from './deepMerge';
export * from './propsInfo';
export * from './document';
export * from './document-utils';
export * from './files';
export * from './utility';
export { randomizeData, canRandomizeControl } from './controls-randomize';
export * from './controls-smart';
Expand Down
5 changes: 5 additions & 0 deletions core/core/src/utility.ts
Expand Up @@ -86,6 +86,11 @@ export interface PackageInfo {
*/
peerDependencies?: PackageDependencies;

/**
* if false, the package is not published to npm
*/
privateNpm?: boolean;

/**
* repository information extracted from the "repository" field in package.json.
*/
Expand Down
4 changes: 4 additions & 0 deletions core/instrument/package.json
Expand Up @@ -40,14 +40,17 @@
"camelcase": "^6.0.0",
"deepmerge": "^4.2.2",
"find-cache-dir": "^3.3.1",
"gitlog": "^4.0.4",
"gray-matter": "^4.0.2",
"hosted-git-info": "^3.0.4",
"isomorphic-git": "^1.8.0",
"js-string-escape": "^1.0.1",
"prettier": "^1.19.1",
"read-package-json": "^2.1.1",
"remark-emoji": "^2.1.0",
"remark-images": "^2.0.0",
"resolve": "^1.15.1",
"sloc": "^0.2.1",
"typescript": "^4.0.5"
},
"devDependencies": {
Expand All @@ -57,6 +60,7 @@
"@types/find-cache-dir": "^3.2.0",
"@types/hosted-git-info": "^3.0.0",
"@types/jest": "^26.0.10",
"@types/sloc": "^0.2.0",
"cross-env": "^5.2.1",
"eslint": "^6.5.1",
"jest": "^26.4.2"
Expand Down
21 changes: 12 additions & 9 deletions core/instrument/src/babel/extract-component.ts
Expand Up @@ -13,6 +13,7 @@ import { followImports } from './follow-imports';
import { analyze_components } from './analyze-component';
import { packageInfo } from '../misc/package-info';
import { readSourceFile } from '../misc/source-options';
import { getFileIinfo } from '../misc/file-info';

import { LoadingDocStore, InstrumentOptions } from '../types';

Expand Down Expand Up @@ -108,15 +109,7 @@ export const extractComponent = async (
}

component.request = follow.filePath;
if (components && typeof components.resolvePropsFile === 'function') {
const propsFile = components.resolvePropsFile(
componentName,
follow.filePath,
);
if (propsFile !== component.request) {
component.propsInfoFile = propsFile;
}
}
component.fileName = path.basename(follow.filePath);
const saveSource = readSourceFile(
components?.sourceFiles,
follow.source,
Expand All @@ -127,6 +120,16 @@ export const extractComponent = async (
component.source = saveSource;
component.loc = follow.loc;
}
component.fileInfo = await getFileIinfo(follow.filePath, saveSource);
if (components && typeof components.resolvePropsFile === 'function') {
const propsFile = components.resolvePropsFile(
componentName,
follow.filePath,
);
if (propsFile !== component.request) {
component.propsInfoFile = propsFile;
}
}
}
componentPackage = await packageInfo(
componentName,
Expand Down
8 changes: 4 additions & 4 deletions core/instrument/src/index.ts
@@ -1,4 +1,3 @@
import * as fs from 'fs';
import mdx from '@mdx-js/mdx';
import matter from 'gray-matter';
import { File } from '@babel/types';
Expand All @@ -20,6 +19,7 @@ import { extractStoryExports } from './misc/mdx-exports';
import { prettify } from './misc/prettify';
import { parseFile } from './misc/ast_store';
import { readSourceFile } from './misc/source-options';
import { getFileIinfo } from './misc/file-info';
import {
LoadingDocStore,
InstrumentOptions,
Expand Down Expand Up @@ -96,9 +96,9 @@ const parseSource = async (
store.packages[storyPackage.fileHash] = storyPackage;
doc.package = storyPackage.fileHash;
}
const stats = fs.statSync(filePath);
doc.dateModified = doc.dateModified || stats.mtime;
doc.date = doc.date || stats.birthtime;
const fileInfo = await getFileIinfo(filePath);
doc.dateModified = doc.dateModified || fileInfo.dateModified;
doc.date = doc.date || fileInfo.dateCreated;
}
for (const key of Object.keys(store.stories)) {
const story: Story = store.stories[key];
Expand Down
55 changes: 55 additions & 0 deletions core/instrument/src/misc/chached-file.ts
@@ -0,0 +1,55 @@
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';

export class CachedFileResource<T> {
private folderKey: string;
private fileKey: string;
private filePath: string;

constructor(folderKey: string, fileKey: string, filePath: string) {
this.folderKey = folderKey;
this.fileKey = fileKey;
this.filePath = filePath;
}

private getCacheFolder = () => {
const folderName =
findCacheDir({ name: `component-controls-${this.folderKey}` }) ||
os.tmpdir();
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName, { recursive: true });
}
return folderName;
};

private getCachedFile = () => {
const cachedFolder = this.getCacheFolder();
return path.join(
cachedFolder,
createHash('md5')
.update(this.fileKey)
.digest('hex'),
);
};
get = (): T | undefined => {
const cachedFileName = this.getCachedFile();
if (fs.existsSync(cachedFileName)) {
const cacheStats = fs.statSync(cachedFileName);
const fileStats = fs.statSync(this.filePath);
if (cacheStats.mtime.getTime() >= fileStats.mtime.getTime()) {
const fileData = fs.readFileSync(cachedFileName, 'utf8');
const json = JSON.parse(fileData);
return Object.keys(json).length ? json : undefined;
}
}
return undefined;
};

set = (data: T | undefined): void => {
const cachedFileName = this.getCachedFile();
fs.writeFileSync(cachedFileName, JSON.stringify(data || {}));
};
}

0 comments on commit 5e76046

Please sign in to comment.