Skip to content

Commit

Permalink
support partial publishing to sandboxes repo
Browse files Browse the repository at this point in the history
  • Loading branch information
JReinhold committed Feb 12, 2024
1 parent c9e9d62 commit 57cbc5e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/generate-sandboxes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
- name: Publish
# publish sandboxes even if the generation fails, as some sandboxes might have been generated successfully
if: ${{ !cancelled() }}
run: yarn publish-sandboxes --remote=https://storybook-bot:${{ secrets.PAT_STORYBOOK_BOT }}@github.com/storybookjs/sandboxes.git --push --branch=next
run: yarn publish-sandboxes --remote=https://storybook-bot:${{ secrets.PAT_STORYBOOK_BOT }}@github.com/storybookjs/sandboxes.git --push --branch=jeppe/experiment-partial-generation

- name: Report failure to Discord
if: failure()
Expand Down
6 changes: 3 additions & 3 deletions scripts/sandbox/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const runGenerators = async (
// where as others are very picky about what directories can be called. So we need to
// handle different modes of operation.
try {
if (Math.random() < 0.2) {
if (Math.random() < 0.6) {
throw new Error('Blip Bloop random error when executing before-script');
}
if (script.includes('{{beforeDir}}')) {
Expand Down Expand Up @@ -227,7 +227,7 @@ const runGenerators = async (
await remove(join(beforeDir, '.git'));

try {
if (Math.random() < 0.2) {
if (Math.random() < 0.6) {
throw new Error('Blip Bloop random error when init storybook');
}
await addStorybook({ baseDir, localRegistry, flags, debug, env });
Expand All @@ -244,7 +244,7 @@ const runGenerators = async (
cause: error,
});
}
if (Math.random() < 0.4) {
if (Math.random() < 0.6) {
throw new Error('Blip Bloop random error anywhere');
}
await addDocumentation(baseDir, { name, dirName });
Expand Down
27 changes: 17 additions & 10 deletions scripts/sandbox/publish.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import program from 'commander';
import { join } from 'path';
import { join, relative } from 'path';
import { existsSync } from 'fs';
import * as tempy from 'tempy';
import { copy, emptyDir, readdir, remove, stat, writeFile } from 'fs-extra';
import { copy, emptyDir, remove, writeFile } from 'fs-extra';
import { execaCommand } from 'execa';

import { getTemplatesData, renderTemplate } from './utils/template';
// eslint-disable-next-line import/no-cycle
import { commitAllToGit } from './utils/git';
import { REPROS_DIRECTORY } from '../utils/constants';
import { glob } from 'glob';

export const logger = console;

Expand All @@ -31,15 +32,21 @@ const publish = async (options: PublishOptions & { tmpFolder: string }) => {

// otherwise old files will stick around and result inconsistent states
logger.log(`🗑 Delete existing template dirs from clone`);
const files = await Promise.all(
(
await readdir(REPROS_DIRECTORY)
).map(async (f) => ({ path: f, stats: await stat(join(REPROS_DIRECTORY, f)) }))
);

// empty all existing directories for sandboxes that have a successful after-storybook directory
await Promise.all(
files
.filter(({ stats, path }) => stats.isDirectory && !path.startsWith('.'))
.map(async ({ path }) => emptyDir(join(tmpFolder, path)))
// find all successfully generated after-storybook directories
// eg. /home/repros/react-vite/default-ts/after-storybook
(await glob(join(REPROS_DIRECTORY, '**', 'after-storybook'))).map((dir) => {
// get their path relative to the source 'repros' directory
// eg. ./react-vite/default-ts/after-storybook
const pathRelativeToSource = relative(REPROS_DIRECTORY, dir);
// get the actual path to the corresponding sandbox directory in the clone
// eg. /home/sandboxes-clone/react-vite/default-ts
const sandboxDirectoryToEmpty = join(tmpFolder, pathRelativeToSource, '..');
console.log({ pathRelativeToSource, sandboxDirectoryToEmpty });
return emptyDir(sandboxDirectoryToEmpty);
})
);

logger.log(`🚚 Moving template files into the repository`);
Expand Down

0 comments on commit 57cbc5e

Please sign in to comment.