Skip to content

Commit

Permalink
feat: instrument options what to collect
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 8, 2020
1 parent 43df951 commit 4998ed6
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 55 deletions.
96 changes: 89 additions & 7 deletions core/instrument/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,31 @@ Name | Type | Description |

*Defined in [core/instrument/src/types.ts](https://github.com/atanasster/component-controls/blob/ab703a5/core/instrument/src/types.ts)*

### **defaultParserOptions**: ParserOptions
### **defaultParserOptions**: *[ParserOptions](#parseroptions)*

**plugins**: *"typescript" | "jsx"[]* = ['jsx', 'typescript']

**sourceType**: *"module"* = "module"

___

### **defaultResolveOptions**: ResolverOptions
### **defaultResolveOptions**: *[ResolverOptions](#resolveroptions)*

**extensions**: *string[]* = ['.js', '.jsx', '.ts', '.tsx', '.vue', '.mjs', '.es', '.es6']

___

### **defaultComponentOptions**: *[ComponentOptions](#componentoptions)*

**storeSourceFile**: *boolean* = true;

___

### **defaultStoriesOptions**: *[StoriesOptions](#storiesoptions)*

**storeSourceFile**: *boolean* = true;

___

# Interfaces

Expand All @@ -92,21 +103,26 @@ Options that can control the parsing and instrumentation process

### Properties

**component**? : *[ComponentOptions](#componentoptions)*
**components**? : *[ComponentOptions](#componentoptions)*

Options for extracting stories information.

**stories**? : *[StoriesOptions](#storiesoptions)*

Options for extracting component information.


**parser**? : *[ParserOptions](#parseroptions)*

Options to control the babel parser.
Options to control babel parser.

**prettier**? : *[PrettierOptions](#prettieroptions) | false*

prettier options are used to prettify the code at the end of the process
this is useful for "story" code, where the story is extracted from the full file
passing a value of false as prettier option will disabled prettifying

**resolve**? : *[ResolverOptions](#resolveroptions)*
**resolver**? : *[ResolverOptions](#resolveroptions)*

Options to control resolving filenames.

Expand All @@ -122,7 +138,37 @@ Options for extracting component information.

**resolveFile**? : *undefined | `(componentName: string, filePath: string) => string | undefined;`*

Callback function toresolve the source file name of a component.
Callback function to resolve the source file name of a component.
Return false if this file should not be processed.


**storeSourceFile**? : *boolean*

If set to false, will not save the component's source file.


**package**? : *[PackageInfoOptions](#packageinfooptions) | false*

Otions for extracting repository information from the component's package,json file.

___

## StoriesOptions

Options for extracting stories information.

*Defined in [core/instrument/src/types.ts](https://github.com/atanasster/component-controls/blob/ab703a5/core/instrument/src/types.ts)*

### Properties

**storeSourceFile**? : *boolean*

If set to false, will not save the stories's source file, only the source of each individual story.


**package**? : *[PackageInfoOptions](#packageinfooptions) | false*

Options for extracting repository information from the component's package,json file.

___

Expand Down Expand Up @@ -201,6 +247,7 @@ Defaults to true if sourceType === 'module'. Otherwise, false.

Adds all parsed tokens to a tokens property on the File node.

___

# PrettierOptions

Expand Down Expand Up @@ -315,6 +362,7 @@ Whether or not to indent the code inside <script> and <style> tags in Vue files.

Whether or not to indent the code inside <script> and <style> tags in Vue files.

___

# ResolvePrettierConfigOptions

Expand Down Expand Up @@ -342,7 +390,7 @@ the following EditorConfig properties are supported:
**useCache**? : *undefined | false | true*

If set to `false`, all caching will be bypassed.

___

# ResolverOptions

Expand Down Expand Up @@ -396,3 +444,37 @@ algorithm does not preserve symlinks by default.
**readFileSync**? : *undefined | function*

how to read files synchronously (defaults to fs.readFileSync)

___

## PackageInfoOptions

Options for finding and extracting package.json informtation, applies both for components and stories.

*Defined in [core/instrument/src/types.ts](https://github.com/atanasster/component-controls/blob/ab703a5/core/instrument/src/types.ts)*

### Properties

**maxLevels**? : *number*

Max levels of folders to look up to find the `package.json` file.


**packageJsonName**? : *string*

`package.json` alternative name


**storeBrowseLink**? : *boolean*

Whether to save the link for browsing the file in the repository field.


**storeDocsLink**? : *boolean*

Whether to save the link for project readme file in the repository field.


**storeIssuesLink**? : *boolean*

Whether to save the link for filing issues with the project in the repository field.
1 change: 1 addition & 0 deletions core/instrument/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@mdx-js/loader": "^1.5.5",
"@storybook/csf": "^0.0.1",
"crypto": "^1.0.1",
"deepmerge": "^4.2.2",
"hosted-git-info": "^3.0.4",
"resolve": "^1.15.1",
"prettier": "^1.19.1",
Expand Down
18 changes: 11 additions & 7 deletions core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,28 @@ export const extractComponent = async (
componentName: string,
filePath: string,
source?: string,
optiomns?: InstrumentOptions,
options?: InstrumentOptions,
initialAST?: File,
): Promise<StoryComponent | undefined> => {
const follow = followImports(
componentName,
filePath,
source,
optiomns,
options,
initialAST,
);
const { components } = options || {};
return follow
? {
name: componentName,
from: follow.from,
request: follow.filePath,
loc: follow.loc,
source: follow.source,
repository: await packageInfo(follow.originalFilePath),
source: components?.storeSourceFile ? follow.source : undefined,
repository: await packageInfo(
follow.originalFilePath,
options?.components?.package,
),
}
: {
name: componentName,
Expand All @@ -67,7 +71,7 @@ export const extractSotreComponent = async (
store: StoriesStore,
filePath: string,
source: string,
optiomns?: InstrumentOptions,
options?: InstrumentOptions,
initialAST?: File,
) => {
const kinds = Object.keys(store.kinds);
Expand All @@ -80,7 +84,7 @@ export const extractSotreComponent = async (
componentName,
filePath,
source,
optiomns,
options,
initialAST,
);
if (component) {
Expand All @@ -97,7 +101,7 @@ export const extractSotreComponent = async (
componentName,
filePath,
source,
optiomns,
options,
initialAST,
);
if (component) {
Expand Down
6 changes: 3 additions & 3 deletions core/instrument/src/babel/follow-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export const followImports = (
options?: InstrumentOptions,
initialAST?: File,
): FollowImportType | undefined => {
const { parser: parserOptions, resolve: resolveOptions, component } =
const { parser: parserOptions, resolver: resolveOptions, components } =
options || {};
const fileName =
component && component.resolveFile
? component.resolveFile(importName, filePath)
components && components.resolveFile
? components.resolveFile(importName, filePath)
: filePath;
if (!fileName) {
return undefined;
Expand Down
42 changes: 27 additions & 15 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { toId, storyNameFromExport } from '@storybook/csf';
import traverse from '@babel/traverse';
import generate from '@babel/generator';
import prettier from 'prettier';
import deepMerge from 'deepmerge';
import parserBabel from 'prettier/parser-babylon';
import {
StoriesStore,
Expand All @@ -20,8 +21,14 @@ import { extractSotreComponent } from './babel/extract-component';
import { packageInfo } from './project/packageInfo';
import {
InstrumentOptions,
ParserOptions,
defaultParserOptions,
ResolveOptions,
defaultResolveOptions,
ComponentOptions,
defaultComponentOptions,
StoriesOptions,
defaultStoriesOptions,
} from './types';

export * from './types';
Expand All @@ -44,23 +51,22 @@ const parseSource = async (
options?: InstrumentOptions,
): Promise<StoriesStore> => {
const {
parser: parserOptions,
prettier: prettierOptions,
resolve: resolveOptions,
component: componentOptions,
parser: parserOptions = {},
prettier: prettierOptions = {},
resolver: resolveOptions = {},
components: componentOptions = {},
stories: storiesOptions = {},
} = options || {};

const mergedOptions = {
parser: {
...defaultParserOptions,
...parserOptions,
},
resolve: {
...defaultResolveOptions,
...resolveOptions,
},
parser: deepMerge<ParserOptions>(defaultParserOptions, parserOptions),
resolve: deepMerge<ResolveOptions>(defaultResolveOptions, resolveOptions),
prettier: prettierOptions,
component: componentOptions,
components: deepMerge<ComponentOptions>(
defaultComponentOptions,
componentOptions,
),
stories: deepMerge<StoriesOptions>(defaultStoriesOptions, storiesOptions),
};

const prettify = async (c: string): Promise<string> => {
Expand Down Expand Up @@ -96,7 +102,9 @@ const parseSource = async (
traverse(ast, traverseFn(store));
if (Object.keys(store.kinds).length > 0) {
const kind = store.kinds[Object.keys(store.kinds)[0]];
kind.source = originalSource;
if (mergedOptions.stories.storeSourceFile) {
kind.source = originalSource;
}
store.stories = Object.keys(store.stories).reduce(
(acc: { [key: string]: Story }, name) => {
const story: Story = store.stories[name];
Expand All @@ -117,7 +125,11 @@ const parseSource = async (
const kindsNames = Object.keys(store.kinds);
for (let i = 0; i < kindsNames.length; i += 1) {
const kind: StoriesKind = store.kinds[kindsNames[i]];
const repository = await packageInfo(filePath);

const repository = await packageInfo(
filePath,
mergedOptions.stories?.package,
);
if (repository) {
kind.repository = repository;
}
Expand Down
Loading

0 comments on commit 4998ed6

Please sign in to comment.