-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
0 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 |
---|---|---|
|
@@ -7,6 +7,7 @@ dist | |
*.DS_Store | ||
.cache | ||
junit.xml | ||
/repros | ||
|
||
# Yarn stuff | ||
/**/.yarn/* | ||
|
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,126 @@ | ||
/* eslint-disable no-console */ | ||
import path, { join, relative } from 'path'; | ||
import program from 'commander'; | ||
import { command } from 'execa'; | ||
import type { Options as ExecaOptions } from 'execa'; | ||
import yaml from 'js-yaml'; | ||
import pLimit from 'p-limit'; | ||
import prettyTime from 'pretty-hrtime'; | ||
import { copy, emptyDir, ensureDir, readFile, remove, rename } from 'fs-extra'; | ||
|
||
const maxConcurrentTasks = 3; | ||
|
||
type GeneratorConfig = { | ||
name: string; | ||
script: string; | ||
// expected?: { | ||
// framework?: string; | ||
// renderer?: string; | ||
// builder?: string; | ||
// }; | ||
}; | ||
|
||
type DataEntry = { | ||
script: string; | ||
}; | ||
|
||
const OUTPUT_DIRECTORY = join(__dirname, '..', '..', 'repros'); | ||
|
||
const addStorybook = async (baseDir: string) => { | ||
const beforeDir = join(baseDir, 'before'); | ||
const afterDir = join(baseDir, 'after'); | ||
const tmpDir = join(baseDir, '.tmp'); | ||
|
||
await ensureDir(tmpDir); | ||
await emptyDir(tmpDir); | ||
|
||
await copy(beforeDir, tmpDir); | ||
|
||
const sbCliBinaryPath = join(__dirname, `../../code/lib/cli/bin/index.js`); | ||
await runCommand(`${sbCliBinaryPath} init`, { | ||
cwd: tmpDir, | ||
}); | ||
|
||
await rename(tmpDir, afterDir); | ||
}; | ||
|
||
const runCommand = async (commandString: string, options: ExecaOptions) => { | ||
// TODO: remove true | ||
const shouldDebug = true || !!process.env.DEBUG; | ||
|
||
const out = await command('which yarn', { | ||
...options, | ||
}); | ||
commandString = commandString.replace('yarn', out.stdout.trim()); | ||
|
||
if (shouldDebug) { | ||
console.log(`Running command: ${commandString}`); | ||
} | ||
|
||
return command(commandString, { stdout: shouldDebug ? 'inherit' : 'ignore', ...options }); | ||
}; | ||
|
||
const runGenerators = async (generators: GeneratorConfig[]) => { | ||
console.log(`🤹♂️ Generating repros with a concurrency of ${maxConcurrentTasks}`); | ||
|
||
const limit = pLimit(maxConcurrentTasks); | ||
|
||
return Promise.all( | ||
generators.map(({ name, script }) => | ||
limit(async () => { | ||
const time = process.hrtime(); | ||
console.log(`🧬 generating ${name}`); | ||
|
||
const baseDir = join(OUTPUT_DIRECTORY, name); | ||
const beforeDir = join(baseDir, 'before'); | ||
const afterDir = join(baseDir, 'after'); | ||
const tmpDir = join(baseDir, '.tmp'); | ||
|
||
await Promise.all([ | ||
ensureDir(beforeDir).then(() => emptyDir(beforeDir)), | ||
remove(afterDir), | ||
remove(tmpDir), | ||
]); | ||
|
||
await runCommand(script, { cwd: beforeDir }); | ||
|
||
await addStorybook(baseDir); | ||
|
||
console.log( | ||
`✅ Created ${name} in ./${relative(process.cwd(), baseDir)} successfully in ${prettyTime( | ||
process.hrtime(time) | ||
)}` | ||
); | ||
}) | ||
) | ||
); | ||
}; | ||
|
||
const generate = async ({ config }: { config: string }) => { | ||
const configContents = await readFile(config, 'utf8'); | ||
const data: Record<string, DataEntry> = yaml.load(configContents); | ||
|
||
runGenerators( | ||
Object.entries(data).map(([name, configuration]) => ({ | ||
name, | ||
script: configuration.script, | ||
})) | ||
); | ||
}; | ||
|
||
program | ||
.description('Create a reproduction from a set of possible templates') | ||
.option( | ||
'-c --config <config>', | ||
'Choose a custom configuration file (.yml format)', | ||
path.join(__dirname, 'repro-config.yml') | ||
); | ||
|
||
program.parse(process.argv); | ||
|
||
const options = program.opts() as { config: string }; | ||
|
||
generate(options).catch((e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}); |
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 @@ | ||
# group-name/instance-name. Should always be two levels deep. | ||
cra/default-js: | ||
script: "yarn -v" | ||
expected: | ||
framework: "@storybook/cra" | ||
renderer: "@storybook/react" | ||
builder: "@storybook/builder-webpack5" | ||
|
||
# cra/default-ts: | ||
# script: "yarn create react-app . --template typescript" | ||
# expected: | ||
# framework: "@storybook/cra" | ||
# renderer: "@storybook/react" | ||
# builder: "@storybook/builder-webpack5" |
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 +1,2 @@ | ||
declare module 'verdaccio'; | ||
declare module 'pretty-hrtime'; |
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