generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import CLI configuration into add-on (#1)
* Move provider list to a dedicated file * Load state from an environment variable * Pass CLI state to add-on * Fetch branches only if a provider is configured * Log generic errors during Storybook build * Fix typo in environment variable * Allow CLI to be executed on the project * Move hostname to Storybook add-on parameters * Add script_name option to README
- Loading branch information
Showing
13 changed files
with
163 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
dist/ | ||
node_modules/ | ||
storybook-bundle/ | ||
storybook-static/ | ||
build-storybook.log | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"from": "storybook-static", | ||
"to": "storybook-bundle", | ||
"default_root": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,11 @@ | ||
export const ADDON_ID = "storybook-branch-switcher"; | ||
export const PARAM_KEY = "branches" as const; | ||
export const GLOBAL_KEY = "branch" as const; | ||
export const BRANCH_SWITCHER_ID = `${ADDON_ID}/switcher` as const; | ||
|
||
export interface BranchSwitcherParameters { | ||
list: string[]; | ||
defaultBranch: string; | ||
currentBranch: string; | ||
hostname?: string; | ||
} | ||
|
||
export const DEFAULT_ADDON_PARAMETERS: BranchSwitcherParameters = { | ||
list: [], | ||
defaultBranch: "master", | ||
currentBranch: "master", | ||
hostname: undefined | ||
hostname: undefined, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { addons, types } from "@storybook/manager-api"; | ||
import { BranchSwitcher } from "./branch-switcher"; | ||
import { BranchSwitcher } from "./components/branch-switcher"; | ||
import { ADDON_ID, BRANCH_SWITCHER_ID, PARAM_KEY } from "./constants"; | ||
|
||
addons.register(ADDON_ID, () => { | ||
addons.add(BRANCH_SWITCHER_ID, { | ||
title: 'Branches', | ||
title: "Branches", | ||
type: types.TOOL, | ||
match: ({ viewMode }) => !!(viewMode && viewMode.match(/^(story|docs)$/)), | ||
render: BranchSwitcher, | ||
paramKey: PARAM_KEY, | ||
paramKey: PARAM_KEY | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
import type { ProjectAnnotations, Renderer } from "@storybook/types"; | ||
import { GLOBAL_KEY } from "./constants"; | ||
|
||
export const globals: ProjectAnnotations<Renderer>['globals'] = { | ||
// Required to make sure SB picks this up from URL params | ||
[GLOBAL_KEY]: '', | ||
}; | ||
export const globals: ProjectAnnotations<Renderer>["globals"] = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,34 @@ | ||
import type { ProviderConfig } from "../cli"; | ||
import bitbucketPullRequests from "./providers/bitbucket-pull-requests"; | ||
import providers from "./providers"; | ||
|
||
export interface Branch { | ||
id: string; | ||
commit: string; | ||
} | ||
|
||
export interface BranchProvider { | ||
isApplicable: (config: ProviderConfig) => boolean; | ||
fetcher: (config: ProviderConfig) => Promise<Branch[]>; | ||
} | ||
|
||
const providers: BranchProvider[] = [ | ||
bitbucketPullRequests | ||
]; | ||
|
||
export const fetchBranches = async (config: ProviderConfig): Promise<Branch[]> => { | ||
export const fetchBranches = async ( | ||
config?: ProviderConfig, | ||
): Promise<Branch[]> => { | ||
const branches: Branch[] = []; | ||
|
||
// Add current branch to the list | ||
const currentBranch = await $`git branch --show-current` | ||
const currentCommit = await $`git log --format="%H" -n 1` | ||
branches.push({ id: currentBranch.toString().trim(), commit: currentCommit.toString().trim() }) | ||
const currentBranch = await $`git branch --show-current`; | ||
const currentCommit = await $`git log --format="%H" -n 1`; | ||
branches.push({ | ||
id: currentBranch.toString().trim(), | ||
commit: currentCommit.toString().trim(), | ||
}); | ||
|
||
// Fetch other branches from a provider, based on configuration | ||
await spinner("Fetching branches..", async () => { | ||
const provider = providers.find((p) => p.isApplicable(config)); | ||
if (!provider) { | ||
throw new Error("Cannot find a branch provider"); | ||
} | ||
branches.push(...await provider.fetcher(config)); | ||
}); | ||
if (config) { | ||
await spinner("Fetching branches..", async () => { | ||
const provider = providers.find((p) => p.isApplicable(config)); | ||
if (!provider) { | ||
throw new Error("Cannot find a branch provider"); | ||
} | ||
branches.push(...(await provider.fetcher(config))); | ||
}); | ||
} | ||
|
||
return branches; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { ProviderConfig } from "../../cli"; | ||
import type { Branch } from "../branches"; | ||
import bitbucketPullRequests from "./bitbucket-pull-requests"; | ||
|
||
export interface BranchProvider { | ||
isApplicable: (config: ProviderConfig) => boolean; | ||
fetcher: (config: ProviderConfig) => Promise<Branch[]>; | ||
} | ||
|
||
const providers: BranchProvider[] = [ | ||
bitbucketPullRequests | ||
]; | ||
|
||
export default providers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export interface BranchSwitcherState { | ||
list: string[]; | ||
defaultBranch: string; | ||
currentBranch: string; | ||
} | ||
|
||
const DEFAULT_ADDON_STATE: BranchSwitcherState = { | ||
list: [], | ||
defaultBranch: "master", | ||
currentBranch: "master", | ||
}; | ||
|
||
export const state: BranchSwitcherState = Object.assign( | ||
DEFAULT_ADDON_STATE, | ||
JSON.parse(process.env["STORYBOOK_BRANCH_SWITCHER_STATE"] ?? "{}"), | ||
); |