Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tools): enable multiple package migration via --name option #20583

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions tools/generators/migrate-converged-pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ Workspace Generator for migrating converged packages to new DX (stage 1)[https:/

<!-- tocstop -->

## NOTES

## Usage

```sh
Expand Down Expand Up @@ -60,6 +58,18 @@ Package/library name (needs to be full name of the package, scope included - e.g

> NOTE: will trigger CLI prompt if you didn't provide this option

**Run migration on subset of packages:**

To run migration on multiple packages you can specify a comma separated list of project names.

```sh
# run migration on:
# - @fluentui/lib-zero
# - @fluentui/lib-one
# - @fluentui/lib-two
yarn nx workspace-generator migrate-converged-pkg --name='@fluentui/lib-zero,@fluentui/lib-one,@fluentui/lib-two'
```

#### `all`

Type: `boolean`
Expand Down
23 changes: 23 additions & 0 deletions tools/generators/migrate-converged-pkg/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,29 @@ describe('migrate-converged-pkg generator', () => {
expect(configs['@proj/react-old'].sourceRoot).not.toBeDefined();
});
});

describe(`--name`, () => {
it(`should accept comma separated string to exec on multiple projects`, async () => {
const projects = [options.name, '@proj/react-one', '@proj/react-two', '@proj/react-old'] as const;

setupDummyPackage(tree, { name: projects[1], version: '9.0.22' });
setupDummyPackage(tree, { name: projects[2], version: '9.0.31' });
setupDummyPackage(tree, { name: projects[3], version: '8.0.1' });

await generator(tree, { name: `${projects[0]},${projects[1]}` });

const configs = projects.reduce((acc, projectName) => {
acc[projectName] = readProjectConfiguration(tree, projectName);

return acc;
}, {} as Record<typeof projects[number], ReadProjectConfiguration>);

expect(configs[projects[0]].sourceRoot).toBeDefined();
expect(configs[projects[1]].sourceRoot).toBeDefined();
expect(configs[projects[2]].sourceRoot).not.toBeDefined();
expect(configs[projects[3]].sourceRoot).not.toBeDefined();
});
});
});

// ==== helpers ====
Expand Down
36 changes: 24 additions & 12 deletions tools/generators/migrate-converged-pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ export default async function (tree: Tree, schema: MigrateConvergedPkgGeneratorS
}

if (hasSchemaFlag(validatedSchema, 'name')) {
runMigrationOnProject(tree, validatedSchema, userLog);
const projectNames = validatedSchema.name.split(',').filter(Boolean);

if (projectNames.length > 1) {
runBatchMigration(tree, userLog, projectNames);
} else {
runMigrationOnProject(tree, validatedSchema, userLog);
}
}

await formatFiles(tree);
Expand All @@ -56,11 +62,13 @@ export default async function (tree: Tree, schema: MigrateConvergedPkgGeneratorS
};
}

function runBatchMigration(tree: Tree, userLog: UserLog) {
const projects = getProjects(tree);
function runBatchMigration(tree: Tree, userLog: UserLog, projectNames?: string[]) {
const projects = Array.from(getProjects(tree)).filter(([projectName]) => {
return projectNames ? projectNames.includes(projectName) : true;
});
Hotell marked this conversation as resolved.
Show resolved Hide resolved

projects.forEach((project, projectName) => {
if (!isPackageConverged(tree, project)) {
projects.forEach(([projectName, projectConfig]) => {
if (!isPackageConverged(tree, projectConfig)) {
userLog.push({ type: 'error', message: `${projectName} is not converged package. Skipping migration...` });
return;
}
Expand Down Expand Up @@ -365,13 +373,17 @@ async function validateSchema(tree: Tree, schema: MigrateConvergedPkgGeneratorSc
}

if (newSchema.name) {
const projectConfig = readProjectConfiguration(tree, newSchema.name);
const projectNames = newSchema.name.split(',').filter(Boolean);

if (!isPackageConverged(tree, projectConfig)) {
throw new Error(
`${newSchema.name} is not converged package. Make sure to run the migration on packages with version 9.x.x`,
);
}
projectNames.forEach(projectName => {
const projectConfig = readProjectConfiguration(tree, projectName);

if (!isPackageConverged(tree, projectConfig)) {
throw new Error(
`${newSchema.name} is not converged package. Make sure to run the migration on packages with version 9.x.x`,
);
}
});
}

return newSchema;
Expand All @@ -382,7 +394,7 @@ async function triggerDynamicPrompts() {

return prompt<PromptResponse>([
{
message: 'Which converged package would you like migrate to new DX? (ex: @fluentui/react-menu)',
message: 'Which converged package/s would you like migrate to new DX? (ex: @fluentui/react-menu)',
Hotell marked this conversation as resolved.
Show resolved Hide resolved
type: 'input',
name: 'name',
},
Expand Down
2 changes: 1 addition & 1 deletion tools/generators/migrate-converged-pkg/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"properties": {
"name": {
"type": "string",
"description": "Package name",
"description": "Library name or comma delimited library names to execute migration on multiple libraries",
Hotell marked this conversation as resolved.
Show resolved Hide resolved
"$default": {
"$source": "argv",
"index": 0
Expand Down
2 changes: 1 addition & 1 deletion tools/generators/migrate-converged-pkg/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface MigrateConvergedPkgGeneratorSchema {
/**
* Library name
* Library name or comma delimited library names to execute migration on multiple libraries
Hotell marked this conversation as resolved.
Show resolved Hide resolved
*/
name?: string;
/**
Expand Down