MaxKless 5bbaffbda8
feat(core): add metagenerator for convert-to-inferred (#27672)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
The DX to migrate to project crystal isn't great right now - people have
to run `convert-to-inferred` generators manually.

## Expected Behavior
We should provide a smoother way to migrate, so we introduced an
`@nx/workspace:infer-targets` generator that can handle multiple
conversions at once, either for the whole workspace or at a project
level.

---------

Co-authored-by: Jack Hsu <jack.hsu@gmail.com>
2024-09-12 20:06:45 -04:00

142 lines
4.2 KiB
TypeScript

import { createProjectGraphAsync, formatFiles, type Tree } from '@nx/devkit';
import { AggregatedLog } from '@nx/devkit/src/generators/plugin-migrations/aggregate-log-util';
import {
migrateProjectExecutorsToPluginV1,
NoTargetsToMigrateError,
} from '@nx/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator';
import { createNodes } from '../../../plugins/plugin';
import { postTargetTransformer } from './lib/post-target-transformer';
import { processStartOptions } from './lib/process-start-options';
import { createProcessOptions } from './lib/create-process-options';
interface Schema {
project?: string;
skipFormat?: boolean;
}
export async function convertToInferred(tree: Tree, options: Schema) {
const projectGraph = await createProjectGraphAsync();
const migrationLogs = new AggregatedLog();
const migratedProjects = await migrateProjectExecutorsToPluginV1(
tree,
projectGraph,
'@nx/react-native/plugin',
createNodes,
{
buildAndroidTargetName: 'build-android',
buildIosTargetName: 'build-ios',
bundleTargetName: 'bundle',
podInstallTargetName: 'pod-install',
runAndroidTargetName: 'run-android',
runIosTargetName: 'run-ios',
startTargetName: 'start',
syncDepsTargetName: 'sync-deps',
upgradeTargetName: 'upgrade',
},
[
{
executors: ['@nx/react-native:build-android'],
postTargetTransformer: postTargetTransformer(
migrationLogs,
createProcessOptions(
'@nx/react-native:build-android',
['port', 'resetCache'],
[]
)
),
targetPluginOptionMapper: (targetName) => ({
buildAndroidTargetName: targetName,
}),
},
{
executors: ['@nx/react-native:build-ios'],
postTargetTransformer: postTargetTransformer(
migrationLogs,
createProcessOptions(
'@nx/react-native:build-ios',
['port', 'resetCache'],
['buildFolder']
)
),
targetPluginOptionMapper: (targetName) => ({
buildIosTargetName: targetName,
}),
},
{
executors: ['@nx/react-native:run-android'],
postTargetTransformer: postTargetTransformer(
migrationLogs,
createProcessOptions(
'@nx/react-native:run-android',
['port', 'resetCache'],
['appId', 'appIdSuffix', 'deviceId']
)
),
targetPluginOptionMapper: (targetName) => ({
runAndroidTargetName: targetName,
}),
},
{
executors: ['@nx/react-native:pod-install'],
postTargetTransformer: postTargetTransformer(migrationLogs),
targetPluginOptionMapper: (targetName) => ({
podInstallTargetName: targetName,
}),
},
{
executors: ['@nx/react-native:run-ios'],
postTargetTransformer: postTargetTransformer(
migrationLogs,
createProcessOptions(
'@nx/react-native:run-ios',
['port', 'resetCache'],
['buildFolder']
)
),
targetPluginOptionMapper: (targetName) => ({
runIosTargetName: targetName,
}),
},
{
executors: ['@nx/react-native:start'],
postTargetTransformer: postTargetTransformer(
migrationLogs,
processStartOptions
),
targetPluginOptionMapper: (targetName) => ({
startTargetName: targetName,
}),
},
{
executors: ['@nx/react-native:sync-deps'],
postTargetTransformer: postTargetTransformer(migrationLogs),
targetPluginOptionMapper: (targetName) => ({
startTargetName: targetName,
}),
},
{
executors: ['@nx/react-native:upgrade'],
postTargetTransformer: postTargetTransformer(migrationLogs),
targetPluginOptionMapper: (targetName) => ({
upgradeTargetName: targetName,
}),
},
],
options.project
);
if (migratedProjects.size === 0) {
throw new NoTargetsToMigrateError();
}
if (!options.skipFormat) {
await formatFiles(tree);
}
return () => {
migrationLogs.flushLogs();
};
}
export default convertToInferred;