2022-07-25 22:43:46 +10:00
|
|
|
import path from 'path';
|
2022-07-31 14:27:09 +10:00
|
|
|
import { remove, pathExists, readJSON, writeJSON } from 'fs-extra';
|
2022-07-27 22:09:36 +10:00
|
|
|
import prompts from 'prompts';
|
2022-07-25 22:43:46 +10:00
|
|
|
|
2022-07-25 11:19:26 +10:00
|
|
|
import { getOptionsOrPrompt } from './utils/options';
|
2022-07-25 22:43:46 +10:00
|
|
|
import { executeCLIStep } from './utils/cli-step';
|
2022-07-28 20:30:40 +10:00
|
|
|
import { exec } from '../code/lib/cli/src/repro-generators/scripts';
|
2022-07-31 14:27:09 +10:00
|
|
|
import type { Parameters } from '../code/lib/cli/src/repro-generators/configs';
|
|
|
|
import { getInterpretedFile } from '../code/lib/core-common';
|
|
|
|
import { readConfig, writeConfig } from '../code/lib/csf-tools';
|
|
|
|
import { babelParse } from '../code/lib/csf-tools/src/babelParse';
|
2022-07-25 11:19:26 +10:00
|
|
|
|
2022-07-28 20:15:57 +10:00
|
|
|
const frameworks = ['react', 'angular'];
|
2022-07-25 11:19:26 +10:00
|
|
|
const addons = ['a11y', 'storysource'];
|
2022-08-02 09:15:06 +10:00
|
|
|
const sandboxDir = path.resolve(__dirname, '../sandbox');
|
2022-07-28 21:21:58 +10:00
|
|
|
const codeDir = path.resolve(__dirname, '../code');
|
2022-07-25 11:19:26 +10:00
|
|
|
|
2022-07-25 22:43:46 +10:00
|
|
|
async function getOptions() {
|
2022-08-02 09:15:06 +10:00
|
|
|
return getOptionsOrPrompt('yarn sandbox', {
|
2022-07-25 22:43:46 +10:00
|
|
|
framework: {
|
|
|
|
description: 'Which framework would you like to use?',
|
|
|
|
values: frameworks,
|
2022-07-31 14:27:09 +10:00
|
|
|
required: true as const,
|
2022-07-25 22:43:46 +10:00
|
|
|
},
|
|
|
|
addon: {
|
|
|
|
description: 'Which extra addons (beyond the CLI defaults) would you like installed?',
|
|
|
|
values: addons,
|
2022-07-31 14:27:09 +10:00
|
|
|
multiple: true as const,
|
2022-07-25 22:43:46 +10:00
|
|
|
},
|
|
|
|
includeStories: {
|
2022-07-28 20:15:57 +10:00
|
|
|
description: "Include Storybook's own stories?",
|
|
|
|
promptType: (_, { framework }) => framework === 'react',
|
2022-07-25 22:43:46 +10:00
|
|
|
},
|
|
|
|
create: {
|
2022-08-02 09:15:06 +10:00
|
|
|
description: 'Create the template from scratch (rather than degitting it)?',
|
2022-07-25 22:43:46 +10:00
|
|
|
},
|
2022-07-27 22:09:36 +10:00
|
|
|
forceDelete: {
|
2022-08-02 09:15:06 +10:00
|
|
|
description: 'Always delete an existing sandbox, even if it has the same configuration?',
|
2022-07-28 20:15:57 +10:00
|
|
|
promptType: false,
|
2022-07-27 22:09:36 +10:00
|
|
|
},
|
|
|
|
forceReuse: {
|
2022-08-02 09:15:06 +10:00
|
|
|
description: 'Always reuse an existing sandbox, even if it has a different configuration?',
|
2022-07-28 20:15:57 +10:00
|
|
|
promptType: false,
|
2022-07-27 22:09:36 +10:00
|
|
|
},
|
2022-07-28 19:46:40 +10:00
|
|
|
link: {
|
|
|
|
description: 'Link the storybook to the local code?',
|
|
|
|
inverse: true,
|
2022-07-25 22:43:46 +10:00
|
|
|
},
|
|
|
|
start: {
|
2022-08-02 09:15:06 +10:00
|
|
|
description: 'Start the Storybook?',
|
2022-07-25 22:43:46 +10:00
|
|
|
inverse: true,
|
|
|
|
},
|
|
|
|
build: {
|
2022-08-02 09:15:06 +10:00
|
|
|
description: 'Build the Storybook?',
|
2022-07-25 22:43:46 +10:00
|
|
|
},
|
|
|
|
watch: {
|
2022-08-02 09:15:06 +10:00
|
|
|
description: 'Start building used packages in watch mode as well as the Storybook?',
|
2022-07-25 22:43:46 +10:00
|
|
|
},
|
2022-07-26 16:20:49 +10:00
|
|
|
dryRun: {
|
2022-07-28 19:46:40 +10:00
|
|
|
description: "Don't execute commands, just list them (dry run)?",
|
2022-07-26 16:20:49 +10:00
|
|
|
},
|
2022-07-25 22:43:46 +10:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-27 22:09:36 +10:00
|
|
|
const steps = {
|
2022-07-25 22:43:46 +10:00
|
|
|
repro: {
|
|
|
|
command: 'repro',
|
2022-08-02 09:15:06 +10:00
|
|
|
description: 'Bootstrapping Template',
|
2022-07-25 22:43:46 +10:00
|
|
|
icon: '👷',
|
|
|
|
hasArgument: true,
|
|
|
|
options: {
|
|
|
|
template: { values: frameworks },
|
|
|
|
e2e: {},
|
|
|
|
},
|
2022-07-25 11:19:26 +10:00
|
|
|
},
|
2022-07-25 22:43:46 +10:00
|
|
|
add: {
|
|
|
|
command: 'add',
|
|
|
|
description: 'Adding addon',
|
|
|
|
icon: '+',
|
|
|
|
hasArgument: true,
|
|
|
|
options: {},
|
2022-07-25 11:19:26 +10:00
|
|
|
},
|
2022-07-28 21:21:58 +10:00
|
|
|
link: {
|
|
|
|
command: 'link',
|
|
|
|
description: 'Linking packages',
|
|
|
|
icon: '🔗',
|
|
|
|
hasArgument: true,
|
2022-07-31 14:27:09 +10:00
|
|
|
options: { local: {}, start: { inverse: true } },
|
2022-07-28 21:21:58 +10:00
|
|
|
},
|
2022-07-25 11:19:26 +10:00
|
|
|
build: {
|
2022-07-25 22:43:46 +10:00
|
|
|
command: 'build',
|
2022-08-02 09:15:06 +10:00
|
|
|
description: 'Building Storybook',
|
2022-07-25 22:43:46 +10:00
|
|
|
icon: '🔨',
|
|
|
|
options: {},
|
2022-07-25 11:19:26 +10:00
|
|
|
},
|
2022-07-25 22:43:46 +10:00
|
|
|
dev: {
|
|
|
|
command: 'dev',
|
2022-08-02 09:15:06 +10:00
|
|
|
description: 'Starting Storybook',
|
2022-07-26 16:20:49 +10:00
|
|
|
icon: '🖥 ',
|
2022-07-25 22:43:46 +10:00
|
|
|
options: {},
|
2022-07-25 11:19:26 +10:00
|
|
|
},
|
2022-07-25 22:43:46 +10:00
|
|
|
};
|
|
|
|
|
2022-07-31 14:27:09 +10:00
|
|
|
const logger = console;
|
|
|
|
|
|
|
|
export const overrideMainConfig = async ({
|
|
|
|
cwd,
|
|
|
|
mainOverrides,
|
|
|
|
}: {
|
|
|
|
cwd: string;
|
|
|
|
mainOverrides: Parameters['mainOverrides'];
|
|
|
|
}) => {
|
|
|
|
logger.info(`📝 Overwriting main.js with the following configuration:`);
|
|
|
|
const configDir = path.join(cwd, '.storybook');
|
|
|
|
const mainConfigPath = getInterpretedFile(path.resolve(configDir, 'main'));
|
|
|
|
logger.debug(mainOverrides);
|
|
|
|
const mainConfig = await readConfig(mainConfigPath);
|
|
|
|
|
|
|
|
Object.keys(mainOverrides).forEach((field) => {
|
|
|
|
// NOTE: using setFieldNode and passing the output of babelParse()
|
|
|
|
mainConfig.setFieldNode([field], mainOverrides[field]);
|
|
|
|
});
|
|
|
|
|
|
|
|
await writeConfig(mainConfig);
|
|
|
|
};
|
|
|
|
|
|
|
|
const addPackageScripts = async ({
|
|
|
|
cwd,
|
|
|
|
scripts,
|
|
|
|
}: {
|
|
|
|
cwd: string;
|
|
|
|
scripts: Record<string, string>;
|
|
|
|
}) => {
|
|
|
|
logger.info(`🔢 Adding package resolutions:`);
|
|
|
|
const packageJsonPath = path.join(cwd, 'package.json');
|
|
|
|
const packageJson = await readJSON(packageJsonPath);
|
|
|
|
packageJson.scripts = {
|
|
|
|
...packageJson.scripts,
|
|
|
|
...scripts,
|
|
|
|
};
|
|
|
|
await writeJSON(packageJsonPath, packageJson, { spaces: 2 });
|
|
|
|
};
|
|
|
|
|
2022-07-25 22:43:46 +10:00
|
|
|
async function main() {
|
|
|
|
const optionValues = await getOptions();
|
|
|
|
|
2022-07-28 21:21:58 +10:00
|
|
|
const { framework, forceDelete, forceReuse, link, dryRun } = optionValues;
|
2022-08-02 09:15:06 +10:00
|
|
|
const cwd = path.join(sandboxDir, framework as string);
|
2022-07-25 22:43:46 +10:00
|
|
|
|
2022-07-27 22:09:36 +10:00
|
|
|
const exists = await pathExists(cwd);
|
2022-07-28 19:46:40 +10:00
|
|
|
let shouldDelete = exists && !forceReuse;
|
2022-07-27 22:09:36 +10:00
|
|
|
if (exists && !forceDelete && !forceReuse) {
|
2022-07-28 19:46:40 +10:00
|
|
|
const relativePath = path.relative(process.cwd(), cwd);
|
|
|
|
({ shouldDelete } = await prompts({
|
2022-07-27 22:09:36 +10:00
|
|
|
type: 'toggle',
|
2022-07-28 19:46:40 +10:00
|
|
|
message: `${relativePath} already exists, should delete it and create a new one?`,
|
|
|
|
name: 'shouldDelete',
|
|
|
|
initial: false,
|
2022-07-27 22:09:36 +10:00
|
|
|
active: 'yes',
|
|
|
|
inactive: 'no',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-07-28 19:46:40 +10:00
|
|
|
if (exists && shouldDelete && !dryRun) await remove(cwd);
|
2022-07-27 22:09:36 +10:00
|
|
|
|
2022-07-28 19:46:40 +10:00
|
|
|
if (!exists || shouldDelete) {
|
2022-07-25 22:43:46 +10:00
|
|
|
await executeCLIStep(steps.repro, {
|
|
|
|
argument: cwd,
|
|
|
|
optionValues: { template: framework },
|
2022-08-02 09:15:06 +10:00
|
|
|
cwd: sandboxDir,
|
2022-07-26 16:20:49 +10:00
|
|
|
dryRun,
|
2022-07-25 22:43:46 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
// TODO -- sb add <addon> doesn't actually work properly:
|
|
|
|
// - installs in `deps` not `devDeps`
|
|
|
|
// - does a `workspace:^` install (what does that mean?)
|
|
|
|
// - doesn't add to `main.js`
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
for (const addon of optionValues.addon as string[]) {
|
|
|
|
const addonName = `@storybook/addon-${addon}`;
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2022-07-26 16:20:49 +10:00
|
|
|
await executeCLIStep(steps.add, { argument: addonName, cwd, dryRun });
|
2022-07-25 22:43:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO copy stories
|
2022-07-28 21:21:58 +10:00
|
|
|
|
|
|
|
if (link) {
|
|
|
|
await executeCLIStep(steps.link, {
|
|
|
|
argument: cwd,
|
|
|
|
cwd: codeDir,
|
|
|
|
dryRun,
|
2022-07-31 14:27:09 +10:00
|
|
|
optionValues: { local: true, start: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO -- work out exactly where this should happen
|
|
|
|
const code = '(c) => ({ ...c, resolve: { ...c.resolve, symlinks: false } })';
|
|
|
|
const mainOverrides = {
|
|
|
|
// @ts-ignore (not sure why TS complains here, it does exist)
|
|
|
|
webpackFinal: babelParse(code).program.body[0].expression,
|
|
|
|
};
|
|
|
|
await overrideMainConfig({ cwd, mainOverrides } as any);
|
|
|
|
|
|
|
|
await addPackageScripts({
|
|
|
|
cwd,
|
|
|
|
scripts: {
|
|
|
|
storybook:
|
|
|
|
'NODE_OPTIONS="--preserve-symlinks --preserve-symlinks-main" storybook dev -p 6006',
|
|
|
|
'build-storybook':
|
|
|
|
'NODE_OPTIONS="--preserve-symlinks --preserve-symlinks-main" storybook build',
|
|
|
|
},
|
2022-07-28 21:21:58 +10:00
|
|
|
});
|
|
|
|
}
|
2022-07-25 22:43:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
const { start } = optionValues;
|
|
|
|
if (start) {
|
2022-07-28 20:30:40 +10:00
|
|
|
await exec(
|
|
|
|
'yarn storybook',
|
|
|
|
{ cwd },
|
|
|
|
{
|
|
|
|
dryRun,
|
|
|
|
startMessage: `⬆️ Starting Storybook`,
|
|
|
|
errorMessage: `🚨 Starting Storybook failed`,
|
|
|
|
}
|
|
|
|
);
|
2022-07-25 22:43:46 +10:00
|
|
|
} else {
|
2022-07-26 16:20:49 +10:00
|
|
|
await executeCLIStep(steps.build, { cwd, dryRun });
|
2022-07-25 22:43:46 +10:00
|
|
|
// TODO serve
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO start dev
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((err) => console.error(err));
|