Skip to content

Commit

Permalink
feat: callback for repository locations
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Aug 9, 2020
1 parent bee5777 commit 50a8093
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 24 deletions.
3 changes: 2 additions & 1 deletion core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component, Document, PackageInfo } from '@component-controls/core';
import { hashStoreId } from '../misc/hashStore';
import { followImports } from './follow-imports';
import { packageInfo } from '../misc/package-info';
import { readSourceFile } from '../misc/source-file-read';
import { readSourceFile } from '../misc/source-options';

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

Expand Down Expand Up @@ -64,6 +64,7 @@ export const extractComponent = async (
}
}
componentPackage = await packageInfo(
componentName,
follow.originalFilePath,
options?.components?.package,
);
Expand Down
8 changes: 6 additions & 2 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { extractStoreComponent } from './babel/extract-component';
import { packageInfo } from './misc/package-info';
import { extractStoryExports } from './misc/mdx-exports';
import { prettify } from './misc/prettify';
import { readSourceFile } from './misc/source-file-read';
import { readSourceFile } from './misc/source-options';
import {
LoadingDocStore,
InstrumentOptions,
Expand Down Expand Up @@ -87,7 +87,11 @@ const parseSource = async (
await extractStoreComponent(store, filePath, source, options, ast);
const doc: Document | undefined = store.doc;
if (doc && store.stories) {
const storyPackage = await packageInfo(filePath, options.stories.package);
const storyPackage = await packageInfo(
doc.title,
filePath,
options.stories.package,
);
if (storyPackage) {
store.packages[storyPackage.fileHash] = storyPackage;
doc.package = storyPackage.fileHash;
Expand Down
25 changes: 21 additions & 4 deletions core/instrument/src/misc/package-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import parseRepositoryURL from '@hutson/parse-repository-url';
import { PackageInfo } from '@component-controls/core';
import { hashStoreId } from './hashStore';
import { PackageInfoOptions } from '../types';
import { getPackageInfo } from './source-options';

const traverseFolder = (
filePath: string,
Expand Down Expand Up @@ -65,6 +66,7 @@ export interface PackageInfoReturnType {
}

export const packageInfo = async (
partName: string,
filePath?: string,
opts?: PackageInfoOptions | false,
): Promise<PackageInfo | undefined> => {
Expand Down Expand Up @@ -117,15 +119,30 @@ export const packageInfo = async (
.replace('{committish}', templates.committish || 'master');
};

const { storeBrowseLink, storeDocsLink, storeIssuesLink } = opts || {};
const { browseLink, docsLink, issuesLink } = opts || {};
const storeBrowseLink = getPackageInfo(
browseLink,
partName,
fillTemplate(templates.browsefiletemplate),
);
if (storeBrowseLink) {
result.repository.browse = fillTemplate(templates.browsefiletemplate);
result.repository.browse = storeBrowseLink;
}
const storeDocsLink = getPackageInfo(
docsLink,
partName,
fillTemplate(templates.docstemplate),
);
if (storeDocsLink) {
result.repository.docs = fillTemplate(templates.docstemplate);
result.repository.docs = storeDocsLink;
}
const storeIssuesLink = getPackageInfo(
issuesLink,
partName,
fillTemplate(templates.bugstemplate),
);
if (storeIssuesLink) {
result.repository.issues = fillTemplate(templates.bugstemplate);
result.repository.issues = storeIssuesLink;
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs';
import { SourceFileOption } from '../types';
import { ComponentFileOption } from '../types';

export const readSourceFile = (
option: SourceFileOption | undefined,
option: ComponentFileOption | undefined,
source: string | undefined,
name: string,
fileName: string,
Expand All @@ -19,3 +19,17 @@ export const readSourceFile = (
}
return undefined;
};

export const getPackageInfo = (
option: ComponentFileOption | undefined,
name: string,
fileName: string,
): string | undefined => {
if (typeof option !== 'undefined') {
if (typeof option === 'function') {
return option(name, fileName);
}
return option ? fileName : undefined;
}
return undefined;
};
22 changes: 11 additions & 11 deletions core/instrument/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,19 @@ export interface PropsLoaderConfig {
export const defaultPackageOptions: PackageInfoOptions = {
maxLevels: 10,
packageJsonName: 'package.json',
storeBrowseLink: true,
storeDocsLink: true,
storeIssuesLink: true,
browseLink: true,
docsLink: true,
issuesLink: true,
};

export const defaultComponentOptions: ComponentOptions = {
sourceFiles: true,
package: defaultPackageOptions,
};

export type ComponentFileFn = (name: string, fileName?: string) => string;
export type ComponentFileOption = boolean | ComponentFileFn;

export const defaultStoriesOptions: StoriesOptions = {
sourceFiles: false,
package: defaultPackageOptions,
Expand All @@ -108,23 +111,20 @@ export interface PackageInfoOptions {
/**
* Whether to save the link for browsing the file in the repository field
*/
storeBrowseLink?: boolean;
browseLink?: ComponentFileOption;

/**
* Whether to save the link for project readme file in the repository field
*/
storeDocsLink?: boolean;
docsLink?: ComponentFileOption;

/**
* Whether to save the link for filing issues with the project in the repository field
*/

storeIssuesLink?: boolean;
issuesLink?: ComponentFileOption;
}

export type SourceFileFn = (name: string, fileName?: string) => string;
export type SourceFileOption = boolean | SourceFileFn;

export interface ComponentOptions {
/**
* Callback function to resolve the source file name of a component.
Expand All @@ -135,7 +135,7 @@ export interface ComponentOptions {
/**
* If set to false, will not save the component's source file
*/
sourceFiles?: SourceFileOption;
sourceFiles?: ComponentFileOption;

/**
* options for extracting repository information from the component's package,json file
Expand All @@ -147,7 +147,7 @@ export interface StoriesOptions {
/**
* If set to false, will not save the stories's source file, only the source of each individual story
*/
sourceFiles?: SourceFileOption;
sourceFiles?: ComponentFileOption;

/**
* options for extracting repository information from the component's package,json file
Expand Down
2 changes: 1 addition & 1 deletion examples/starter/.config/buildtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
instrument: {
components: {
package: {
storeBrowseLink: true,
browseLink: true,
},
resolveFile: (componentName, filePath) => {
if (filePath.includes('theme-ui/dist')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,31 @@ module.exports = {
instrument: {
components: {
package: {
storeBrowseLink: false,
browseLink: false,
},
},
}
};
```

or better - we can point to the correct repository source location

```js:title=.config/buildtime.js
module.exports = {
instrument: {
components: {
package: {
browseLink: (componentName, filePath ) => {
if (filePath.includes('system-ui/theme-ui/tree/master/dist/index.js')) {
return `https://github.com/system-ui/theme-ui/tree/master/packages/components/src/${componentName}.js`
}
return filePath;
}
},
},
}
};
```

## Props info

Expand All @@ -69,7 +87,12 @@ module.exports = {
instrument: {
components: {
package: {
storeBrowseLink: false,
browseLink: (componentName, filePath ) => {
if (filePath.includes('system-ui/theme-ui/tree/master/dist/index.js')) {
return `https://github.com/system-ui/theme-ui/tree/master/packages/components/src/${componentName}.js`
}
return filePath;
}
},
resolveFile: (componentName, filePath) => {
if (filePath.includes('theme-ui/dist')) {
Expand Down Expand Up @@ -109,7 +132,12 @@ module.exports = {
instrument: {
components: {
package: {
storeBrowseLink: false,
browseLink: (componentName, filePath ) => {
if (filePath.includes('system-ui/theme-ui/tree/master/dist/index.js')) {
return `https://github.com/system-ui/theme-ui/tree/master/packages/components/src/${componentName}.js`
}
return filePath;
}
},
resolveFile: (componentName, filePath) => {
if (filePath.includes('theme-ui/dist')) {
Expand Down

0 comments on commit 50a8093

Please sign in to comment.