Merge pull request #7463 from storybookjs/7450-7446-cli-migrate-fixes

CLI: sb migrate npm & typescript support
This commit is contained in:
Michael Shilman 2019-07-18 14:06:02 +08:00 committed by GitHub
commit 4f026d27a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 8 deletions

View File

@ -22,7 +22,7 @@ if (process.argv[1].includes('getstorybook')) {
.option('-f --force', 'Forcely add storybook')
.option('-s --skip-install', 'Skip installing deps')
.option('-N --use-npm', 'Use npm to install deps')
.option('-p --parser <babel | babylon | flow>', 'jscodeshift parser')
.option('-p --parser <babel | babylon | flow | ts | tsx>', 'jscodeshift parser')
.option('-t --type <type>', 'Add Storybook for a specific project type')
.option('-y --yes', 'Answer yes to all prompts')
.action(options => initiate(options, pkg));
@ -53,6 +53,7 @@ if (process.argv[1].includes('getstorybook')) {
.description('Run a storybook codemod migration on your source files')
.option('-l --list', 'List available migrations')
.option('-g --glob <glob>', 'Glob for files upon which to apply the migration', '**/*.js')
.option('-p --parser <babel | babylon | flow | ts | tsx>', 'jscodeshift parser')
.option(
'-n --dry-run',
'Dry run: verify the migration exists and show the files to which it will be applied'
@ -61,8 +62,8 @@ if (process.argv[1].includes('getstorybook')) {
'-r --rename <from-to>',
'Rename suffix of matching files after codemod has been applied, e.g. ".js:.ts"'
)
.action((migration, { configDir, glob, dryRun, list, rename }) => {
migrate(migration, { configDir, glob, dryRun, list, rename, logger }).catch(err => {
.action((migration, { configDir, glob, dryRun, list, rename, parser }) => {
migrate(migration, { configDir, glob, dryRun, list, rename, parser, logger }).catch(err => {
logger.error(err);
process.exit(1);
});

View File

@ -25,7 +25,7 @@ async function renameFile(file, from, to, { logger }) {
return fs.rename(file, newFile);
}
export async function runCodemod(codemod, { glob, logger, dryRun, rename, hasYarn }) {
export async function runCodemod(codemod, { glob, logger, dryRun, rename, hasYarn, parser }) {
const codemods = listCodemods();
if (!codemods.includes(codemod)) {
throw new Error(`Unknown codemod ${codemod}. Run --list for options.`);
@ -42,10 +42,15 @@ export async function runCodemod(codemod, { glob, logger, dryRun, rename, hasYar
const files = await globby([glob, '!node_modules', '!dist']);
logger.log(`=> Applying ${codemod}: ${files.length} files`);
if (!dryRun) {
const runner = hasYarn ? 'yarn' : 'npm';
spawnSync(runner, ['run', 'jscodeshift', '-t', `${TRANSFORM_DIR}/${codemod}.js`, ...files], {
stdio: 'inherit',
});
const runner = hasYarn ? 'yarn' : 'npx';
const parserArgs = parser ? ['--parser', parser] : [];
spawnSync(
runner,
['jscodeshift', '-t', `${TRANSFORM_DIR}/${codemod}.js`, ...parserArgs, ...files],
{
stdio: 'inherit',
}
);
}
if (renameParts) {