-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add preview images to storybook (#13)
Requires shot-scraper to be installed Adds --update-images and --no-update-images to storybook and build-storybook subcommands kevinschaul/jump-start-template#16
- Loading branch information
1 parent
97ec67d
commit c4c0880
Showing
9 changed files
with
118 additions
and
52 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
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,27 +1,66 @@ | ||
import { join } from "node:path"; | ||
import { spawn } from "node:child_process"; | ||
import { copyJumpStartTools, spawnWithIO, symlinkStarters } from "./util"; | ||
import updateStories from "../src/util/updateStories"; | ||
import updateScreenshots from "../src/util/updateScreenshots"; | ||
const root = join(import.meta.dirname, "../"); | ||
|
||
type BuildStorybookOpts = { | ||
startersDir: string; | ||
updateImages: string; | ||
}; | ||
|
||
async function startStorybookUpdateScreenshots( | ||
toolsRoot: string, | ||
startersDir: string, | ||
storiesDir: string, | ||
) { | ||
return new Promise((resolve, reject) => { | ||
// Start the storybook server | ||
console.log("Starting dev server to update images"); | ||
const child = spawn("storybook", ["dev", "--ci", "-p", "6006"], { | ||
cwd: toolsRoot, | ||
}); | ||
|
||
// When the dev server logs that storybook has started, call | ||
// updateScreenshots | ||
child.stdout?.on("data", (data) => { | ||
if (/Storybook .* started/.test(data.toString())) { | ||
console.log("Updating images"); | ||
updateScreenshots(startersDir, storiesDir); | ||
child.kill(); | ||
console.log("Images updated"); | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
const buildStorybook = async (opts: BuildStorybookOpts) => { | ||
console.log(`Using startersDir: ${opts.startersDir}`); | ||
|
||
const toolsRoot = copyJumpStartTools(root, opts.startersDir); | ||
symlinkStarters(toolsRoot, opts.startersDir); | ||
|
||
// Rewrite stories now | ||
console.log('Updating stories') | ||
console.log("Updating stories"); | ||
const storiesDir = join(toolsRoot, "./src/stories"); | ||
updateStories(opts.startersDir, storiesDir); | ||
console.log('Update stories complete.') | ||
console.log("Update stories complete."); | ||
|
||
if (opts.updateImages) { | ||
await startStorybookUpdateScreenshots( | ||
toolsRoot, | ||
opts.startersDir, | ||
storiesDir, | ||
); | ||
} | ||
|
||
// Build the site | ||
const outDir = join(opts.startersDir, "dist") | ||
const outDir = join(opts.startersDir, "dist"); | ||
console.log(`Building site to ${outDir}`); | ||
spawnWithIO("storybook", ["build", "--output-dir", outDir], { cwd: toolsRoot }); | ||
spawnWithIO("storybook", ["build", "--output-dir", outDir], { | ||
cwd: toolsRoot, | ||
}); | ||
}; | ||
export default buildStorybook; |
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
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