Skip to content

Commit

Permalink
feat: add external dependencies block
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 8, 2020
1 parent 6b91c0a commit 86f09a5
Show file tree
Hide file tree
Showing 32 changed files with 2,291 additions and 30 deletions.
2 changes: 2 additions & 0 deletions core/config/test/__snapshots__/stories.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Array [
"/Users/atanasster/component-controls/ui/components/src/Title/Title.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Toggle/Toggle.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Zoom/Zoom.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentDeps/ComponentDeps.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentSource/ComponentSource.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ControlsTable/ControlsTable.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/Description/Description.stories.tsx",
Expand Down Expand Up @@ -87,6 +88,7 @@ Array [
"/Users/atanasster/component-controls/ui/components/src/Title/Title.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Toggle/Toggle.stories.tsx",
"/Users/atanasster/component-controls/ui/components/src/Zoom/Zoom.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentDeps/ComponentDeps.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ComponentSource/ComponentSource.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/ControlsTable/ControlsTable.stories.tsx",
"/Users/atanasster/component-controls/ui/blocks/src/Description/Description.stories.tsx",
Expand Down
2 changes: 1 addition & 1 deletion core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const extractComponent = async (
name: componentName,
from: follow.from,
request: follow.filePath,

imports: follow.imports,
importedName: follow.importedName,
};
if (components?.storeSourceFile) {
Expand Down
55 changes: 41 additions & 14 deletions core/instrument/src/babel/follow-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as parser from '@babel/parser';
import * as path from 'path';
import { File } from '@babel/types';
import traverse from '@babel/traverse';
import { CodeLocation } from '@component-controls/specification';
import { CodeLocation, Imports } from '@component-controls/specification';
import { ImportTypes, traverseImports } from './extract-imports';
import {
traverseExports,
Expand All @@ -23,6 +23,7 @@ export interface FollowImportType {
loc?: CodeLocation;
source?: string;
imported?: string;
imports?: Imports;
}

export const followImports = (
Expand All @@ -45,6 +46,9 @@ export const followImports = (
const ast: any = initialAST || parser.parse(source, parserOptions);
const baseImportedName = importName.split('.')[0];

const imports: ImportTypes = {};
traverse(ast, traverseImports(imports));

const exports: ExportTypes = {
named: {},
};
Expand All @@ -66,6 +70,30 @@ export const followImports = (
result.loc = findExport.loc;
result.source = source ? source : undefined;
}
const externalImports = Object.keys(imports)
.filter(key => !imports[key].from.startsWith('.'))
.reduce(
(
acc: {
[key: string]: {
name: string;
importedName: string;
}[];
},
key,
) => {
const { name, from, importedName } = imports[key];
if (acc[from]) {
return {
...acc,
[from]: [...acc[from], { name, importedName }],
};
}
return { ...acc, [from]: [{ name, importedName }] };
},
{},
);
result.imports = externalImports;
return result;
} else {
const resolvedFilePath = resolve.sync(findExport.from, {
Expand Down Expand Up @@ -106,8 +134,6 @@ export const followImports = (
if (foundInExportAll) {
return foundInExportAll;
}
const imports: ImportTypes = {};
traverse(ast, traverseImports(imports));

const findImport = imports[baseImportedName];
if (findImport) {
Expand All @@ -122,17 +148,18 @@ export const followImports = (
undefined,
options,
);
return imported
? {
...imported,
importedName: findImport.importedName,
from: findImport.from,
}
: {
exportedAs: findImport.importedName,
importedName: findImport.importedName,
from: findImport.from,
};
if (imported) {
return {
...imported,
importedName: findImport.importedName,
from: findImport.from,
};
}
return {
exportedAs: findImport.importedName,
importedName: findImport.importedName,
from: findImport.from,
};
} catch (e) {
//non-existing file
return {
Expand Down
11 changes: 10 additions & 1 deletion core/instrument/src/misc/package-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,21 @@ export const packageInfo = async (
)
.replace('{committish}', templates.committish || 'master');
};
const { name, version } = packageJSON;
const {
name,
version,
dependencies,
devDependencies,
peerDependencies,
} = packageJSON;
const result: PackageInfo = {
fileHash: hashStoreId(fileName),
name,
version,
repository: {},
dependencies,
devDependencies,
peerDependencies,
};
const { storeBrowseLink, storeDocsLink, storeIssuesLink } = opts || {};
if (storeBrowseLink) {
Expand Down
40 changes: 40 additions & 0 deletions core/instrument/test/__snapshots__/csf-components.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ Object {
"ceea0565a65f75c8c7b6b355d6d335ac": Object {
"from": "../../components/button-default-arrow-func",
"importedName": "default",
"imports": Object {
"react": Array [
Object {
"importedName": "default",
"name": "React",
},
],
},
"name": "Button",
"request": "/Users/atanasster/component-controls/core/instrument/test/fixtures/components/button-default-arrow-func.js",
},
Expand Down Expand Up @@ -36,12 +44,28 @@ Object {
"2f4bd34d15be236674abaf6e894afe6e": Object {
"from": "../../components/button-named-arrow-func",
"importedName": "Button",
"imports": Object {
"react": Array [
Object {
"importedName": "default",
"name": "React",
},
],
},
"name": "Button",
"request": "/Users/atanasster/component-controls/core/instrument/test/fixtures/components/button-named-arrow-func.js",
},
"dc262d21cd7820523c7e0bacca461e87": Object {
"from": "../../components/button-default-arrow-func",
"importedName": "default",
"imports": Object {
"react": Array [
Object {
"importedName": "default",
"name": "React",
},
],
},
"name": "ArrowButton",
"request": "/Users/atanasster/component-controls/core/instrument/test/fixtures/components/button-default-arrow-func.js",
},
Expand Down Expand Up @@ -101,12 +125,28 @@ Object {
"2f4bd34d15be236674abaf6e894afe6e": Object {
"from": "../../components/button-named-arrow-func",
"importedName": "Button",
"imports": Object {
"react": Array [
Object {
"importedName": "default",
"name": "React",
},
],
},
"name": "Button",
"request": "/Users/atanasster/component-controls/core/instrument/test/fixtures/components/button-named-arrow-func.js",
},
"dc262d21cd7820523c7e0bacca461e87": Object {
"from": "../../components/button-default-arrow-func",
"importedName": "default",
"imports": Object {
"react": Array [
Object {
"importedName": "default",
"name": "React",
},
],
},
"name": "ArrowButton",
"request": "/Users/atanasster/component-controls/core/instrument/test/fixtures/components/button-default-arrow-func.js",
},
Expand Down
112 changes: 112 additions & 0 deletions core/instrument/test/__snapshots__/csf-exclude-stories.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,36 @@ Object {
},
"packages": Object {
"7b45564d8ad9cf0f6da12c3360f96029": Object {
"dependencies": Object {
"@babel/generator": "^7.9.4",
"@babel/parser": "^7.9.4",
"@babel/traverse": "^7.9.0",
"@component-controls/core": "^1.0.1",
"@component-controls/specification": "^1.0.1",
"@hutson/parse-repository-url": "^5.0.0",
"@mdx-js/loader": "^1.5.5",
"camelcase": "^6.0.0",
"deepmerge": "^4.2.2",
"find-cache-dir": "^3.3.1",
"hosted-git-info": "^3.0.4",
"prettier": "^1.19.1",
"read-package-json": "^2.1.1",
"resolve": "^1.15.1",
"typescript": "^3.8.3",
},
"devDependencies": Object {
"@babel/types": "^7.9.0",
"@rollup/plugin-node-resolve": "^7.1.1",
"@types/find-cache-dir": "^3.2.0",
"@types/hosted-git-info": "^3.0.0",
"@types/jest": "^25.1.2",
"cross-env": "^5.2.1",
"eslint": "^6.5.1",
"jest": "^24.9.0",
},
"fileHash": "7b45564d8ad9cf0f6da12c3360f96029",
"name": "@component-controls/instrument",
"peerDependencies": undefined,
"repository": Object {
"browse": "https://github.com/ccontrols/component-controls/tree/master/core/instrument/test/fixtures/csf/exclude-stories/exclude-stories-array.js",
"docs": "https://github.com/ccontrols/component-controls/tree/master#readme",
Expand Down Expand Up @@ -69,8 +97,36 @@ Object {
},
"packages": Object {
"7b45564d8ad9cf0f6da12c3360f96029": Object {
"dependencies": Object {
"@babel/generator": "^7.9.4",
"@babel/parser": "^7.9.4",
"@babel/traverse": "^7.9.0",
"@component-controls/core": "^1.0.1",
"@component-controls/specification": "^1.0.1",
"@hutson/parse-repository-url": "^5.0.0",
"@mdx-js/loader": "^1.5.5",
"camelcase": "^6.0.0",
"deepmerge": "^4.2.2",
"find-cache-dir": "^3.3.1",
"hosted-git-info": "^3.0.4",
"prettier": "^1.19.1",
"read-package-json": "^2.1.1",
"resolve": "^1.15.1",
"typescript": "^3.8.3",
},
"devDependencies": Object {
"@babel/types": "^7.9.0",
"@rollup/plugin-node-resolve": "^7.1.1",
"@types/find-cache-dir": "^3.2.0",
"@types/hosted-git-info": "^3.0.0",
"@types/jest": "^25.1.2",
"cross-env": "^5.2.1",
"eslint": "^6.5.1",
"jest": "^24.9.0",
},
"fileHash": "7b45564d8ad9cf0f6da12c3360f96029",
"name": "@component-controls/instrument",
"peerDependencies": undefined,
"repository": Object {
"browse": "https://github.com/ccontrols/component-controls/tree/master/core/instrument/test/fixtures/csf/exclude-stories/exclude-stories-regex.js",
"docs": "https://github.com/ccontrols/component-controls/tree/master#readme",
Expand Down Expand Up @@ -125,8 +181,36 @@ Object {
},
"packages": Object {
"7b45564d8ad9cf0f6da12c3360f96029": Object {
"dependencies": Object {
"@babel/generator": "^7.9.4",
"@babel/parser": "^7.9.4",
"@babel/traverse": "^7.9.0",
"@component-controls/core": "^1.0.1",
"@component-controls/specification": "^1.0.1",
"@hutson/parse-repository-url": "^5.0.0",
"@mdx-js/loader": "^1.5.5",
"camelcase": "^6.0.0",
"deepmerge": "^4.2.2",
"find-cache-dir": "^3.3.1",
"hosted-git-info": "^3.0.4",
"prettier": "^1.19.1",
"read-package-json": "^2.1.1",
"resolve": "^1.15.1",
"typescript": "^3.8.3",
},
"devDependencies": Object {
"@babel/types": "^7.9.0",
"@rollup/plugin-node-resolve": "^7.1.1",
"@types/find-cache-dir": "^3.2.0",
"@types/hosted-git-info": "^3.0.0",
"@types/jest": "^25.1.2",
"cross-env": "^5.2.1",
"eslint": "^6.5.1",
"jest": "^24.9.0",
},
"fileHash": "7b45564d8ad9cf0f6da12c3360f96029",
"name": "@component-controls/instrument",
"peerDependencies": undefined,
"repository": Object {
"browse": "https://github.com/ccontrols/component-controls/tree/master/core/instrument/test/fixtures/csf/exclude-stories/include-stories-array.js",
"docs": "https://github.com/ccontrols/component-controls/tree/master#readme",
Expand Down Expand Up @@ -194,8 +278,36 @@ Object {
},
"packages": Object {
"7b45564d8ad9cf0f6da12c3360f96029": Object {
"dependencies": Object {
"@babel/generator": "^7.9.4",
"@babel/parser": "^7.9.4",
"@babel/traverse": "^7.9.0",
"@component-controls/core": "^1.0.1",
"@component-controls/specification": "^1.0.1",
"@hutson/parse-repository-url": "^5.0.0",
"@mdx-js/loader": "^1.5.5",
"camelcase": "^6.0.0",
"deepmerge": "^4.2.2",
"find-cache-dir": "^3.3.1",
"hosted-git-info": "^3.0.4",
"prettier": "^1.19.1",
"read-package-json": "^2.1.1",
"resolve": "^1.15.1",
"typescript": "^3.8.3",
},
"devDependencies": Object {
"@babel/types": "^7.9.0",
"@rollup/plugin-node-resolve": "^7.1.1",
"@types/find-cache-dir": "^3.2.0",
"@types/hosted-git-info": "^3.0.0",
"@types/jest": "^25.1.2",
"cross-env": "^5.2.1",
"eslint": "^6.5.1",
"jest": "^24.9.0",
},
"fileHash": "7b45564d8ad9cf0f6da12c3360f96029",
"name": "@component-controls/instrument",
"peerDependencies": undefined,
"repository": Object {
"browse": "https://github.com/ccontrols/component-controls/tree/master/core/instrument/test/fixtures/csf/exclude-stories/include-stories-regex.js",
"docs": "https://github.com/ccontrols/component-controls/tree/master#readme",
Expand Down
Loading

0 comments on commit 86f09a5

Please sign in to comment.