Skip to content

Commit

Permalink
fix(vite): aggregate migration log
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Jun 14, 2024
1 parent 31d24b1 commit 0e59ae3
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createProjectGraphAsync, formatFiles, type Tree } from '@nx/devkit';
import { migrateExecutorToPluginV1 } from '@nx/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator';
import { flushLogs } from '@nx/devkit/src/generators/plugin-migrations/aggregate-log-util';
import { createNodes, VitePluginOptions } from '../../plugins/plugin';
import { migrateExecutorToPlugin } from '@nx/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator';
import { createNodesV2, VitePluginOptions } from '../../plugins/plugin';
import { buildPostTargetTransformer } from './lib/build-post-target-transformer';
import { servePostTargetTransformer } from './lib/serve-post-target-transformer';
import { previewPostTargetTransformer } from './lib/preview-post-target-transformer';
import { testPostTargetTransformer } from './lib/test-post-target-transformer';
import { AggregatedLog } from '@nx/devkit/src/generators/plugin-migrations/aggregate-log-util';

interface Schema {
project?: string;
Expand All @@ -14,8 +14,9 @@ interface Schema {

export async function convertToInferred(tree: Tree, options: Schema) {
const projectGraph = await createProjectGraphAsync();
const migrationLogs = new AggregatedLog();
const migratedBuildProjects =
await migrateExecutorToPluginV1<VitePluginOptions>(
await migrateExecutorToPlugin<VitePluginOptions>(
tree,
projectGraph,
'@nx/vite:build',
Expand All @@ -28,11 +29,11 @@ export async function convertToInferred(tree: Tree, options: Schema) {
serveStaticTargetName: 'serve-static',
}),
buildPostTargetTransformer,
createNodes,
createNodesV2,
options.project
);
const migratedServeProjects =
await migrateExecutorToPluginV1<VitePluginOptions>(
await migrateExecutorToPlugin<VitePluginOptions>(
tree,
projectGraph,
'@nx/vite:dev-server',
Expand All @@ -44,12 +45,12 @@ export async function convertToInferred(tree: Tree, options: Schema) {
testTargetName: 'test',
serveStaticTargetName: 'serve-static',
}),
servePostTargetTransformer,
createNodes,
servePostTargetTransformer(migrationLogs),
createNodesV2,
options.project
);
const migratedPreviewProjects =
await migrateExecutorToPluginV1<VitePluginOptions>(
await migrateExecutorToPlugin<VitePluginOptions>(
tree,
projectGraph,
'@nx/vite:preview-server',
Expand All @@ -61,27 +62,26 @@ export async function convertToInferred(tree: Tree, options: Schema) {
testTargetName: 'test',
serveStaticTargetName: 'serve-static',
}),
previewPostTargetTransformer,
createNodes,
options.project
);
const migratedTestProjects =
await migrateExecutorToPluginV1<VitePluginOptions>(
tree,
projectGraph,
'@nx/vite:test',
'@nx/vite/plugin',
(targetName) => ({
buildTargetName: 'build',
serveTargetName: 'serve',
previewTargetName: 'preview',
testTargetName: targetName,
serveStaticTargetName: 'serve-static',
}),
testPostTargetTransformer,
createNodes,
previewPostTargetTransformer(migrationLogs),
createNodesV2,
options.project
);
const migratedTestProjects = await migrateExecutorToPlugin<VitePluginOptions>(
tree,
projectGraph,
'@nx/vite:test',
'@nx/vite/plugin',
(targetName) => ({
buildTargetName: 'build',
serveTargetName: 'serve',
previewTargetName: 'preview',
testTargetName: targetName,
serveStaticTargetName: 'serve-static',
}),
testPostTargetTransformer,
createNodesV2,
options.project
);

const migratedProjects =
migratedBuildProjects.size +
Expand All @@ -98,7 +98,7 @@ export async function convertToInferred(tree: Tree, options: Schema) {
}

return () => {
flushLogs();
migrationLogs.flushLogs();
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,61 @@
import { type TargetConfiguration, type Tree } from '@nx/devkit';
import { getViteConfigPath } from './utils';
import { aggregateLog } from '@nx/devkit/src/generators/plugin-migrations/aggregate-log-util';
import { AggregatedLog } from '@nx/devkit/src/generators/plugin-migrations/aggregate-log-util';

export function previewPostTargetTransformer(
target: TargetConfiguration,
tree: Tree,
projectDetails: { projectName: string; root: string },
inferredTargetConfiguration: TargetConfiguration
) {
const viteConfigPath = getViteConfigPath(tree, projectDetails.root);

if (target.options) {
removePropertiesFromTargetOptions(
target.options,
projectDetails.projectName
);
}
export function previewPostTargetTransformer(migrationLogs: AggregatedLog) {
return (
target: TargetConfiguration,
tree: Tree,
projectDetails: { projectName: string; root: string },
inferredTargetConfiguration: TargetConfiguration
) => {
const viteConfigPath = getViteConfigPath(tree, projectDetails.root);

if (target.configurations) {
for (const configurationName in target.configurations) {
const configuration = target.configurations[configurationName];
if (target.options) {
removePropertiesFromTargetOptions(
configuration,
projectDetails.projectName
target.options,
projectDetails.projectName,
migrationLogs
);
}

if (target.configurations) {
for (const configurationName in target.configurations) {
const configuration = target.configurations[configurationName];
removePropertiesFromTargetOptions(
configuration,
projectDetails.projectName,
migrationLogs
);

if (Object.keys(configuration).length === 0) {
delete target.configurations[configurationName];
if (Object.keys(configuration).length === 0) {
delete target.configurations[configurationName];
}
}
}

if (Object.keys(target.configurations).length === 0) {
if ('defaultConfiguration' in target) {
delete target.defaultConfiguration;
if (Object.keys(target.configurations).length === 0) {
if ('defaultConfiguration' in target) {
delete target.defaultConfiguration;
}
delete target.configurations;
}
delete target.configurations;
}

if (
'defaultConfiguration' in target &&
!target.configurations[target.defaultConfiguration]
) {
delete target.defaultConfiguration;
if (
'defaultConfiguration' in target &&
!target.configurations[target.defaultConfiguration]
) {
delete target.defaultConfiguration;
}
}
}

return target;
return target;
};
}

function removePropertiesFromTargetOptions(
targetOptions: any,
projectName: string
projectName: string,
migrationLogs: AggregatedLog
) {
if ('buildTarget' in targetOptions) {
delete targetOptions.buildTarget;
Expand All @@ -61,7 +66,7 @@ function removePropertiesFromTargetOptions(
}

if ('proxyConfig' in targetOptions) {
aggregateLog({
migrationLogs.addLog({
executorName: '@nx/vite:preview-server',
project: projectName,
log: `Encountered 'proxyConfig' in project.json. You will need to copy the contents of this file to the 'server.proxy' property in your Vite config file.`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,63 @@
import { type TargetConfiguration, type Tree } from '@nx/devkit';
import { moveBuildLibsFromSourceToViteConfig } from './build-post-target-transformer';
import { getViteConfigPath } from './utils';
import { aggregateLog } from '@nx/devkit/src/generators/plugin-migrations/aggregate-log-util';
import { AggregatedLog } from '@nx/devkit/src/generators/plugin-migrations/aggregate-log-util';

export function servePostTargetTransformer(
target: TargetConfiguration,
tree: Tree,
projectDetails: { projectName: string; root: string },
inferredTargetConfiguration: TargetConfiguration
) {
const viteConfigPath = getViteConfigPath(tree, projectDetails.root);

if (target.options) {
removePropertiesFromTargetOptions(
tree,
target.options,
viteConfigPath,
projectDetails.root,
projectDetails.projectName,
true
);
}
export function servePostTargetTransformer(migrationLogs: AggregatedLog) {
return (
target: TargetConfiguration,
tree: Tree,
projectDetails: { projectName: string; root: string },
inferredTargetConfiguration: TargetConfiguration
) => {
const viteConfigPath = getViteConfigPath(tree, projectDetails.root);

if (target.configurations) {
for (const configurationName in target.configurations) {
const configuration = target.configurations[configurationName];
if (target.options) {
removePropertiesFromTargetOptions(
tree,
configuration,
target.options,
viteConfigPath,
projectDetails.root,
projectDetails.projectName
projectDetails.projectName,
migrationLogs,
true
);
}

if (target.configurations) {
for (const configurationName in target.configurations) {
const configuration = target.configurations[configurationName];
removePropertiesFromTargetOptions(
tree,
configuration,
viteConfigPath,
projectDetails.root,
projectDetails.projectName,
migrationLogs
);

if (Object.keys(configuration).length === 0) {
delete target.configurations[configurationName];
if (Object.keys(configuration).length === 0) {
delete target.configurations[configurationName];
}
}
}

if (Object.keys(target.configurations).length === 0) {
if ('defaultConfiguration' in target) {
delete target.defaultConfiguration;
if (Object.keys(target.configurations).length === 0) {
if ('defaultConfiguration' in target) {
delete target.defaultConfiguration;
}
delete target.configurations;
}
delete target.configurations;
}

if (
'defaultConfiguration' in target &&
!target.configurations[target.defaultConfiguration]
) {
delete target.defaultConfiguration;
if (
'defaultConfiguration' in target &&
!target.configurations[target.defaultConfiguration]
) {
delete target.defaultConfiguration;
}
}
}

return target;
return target;
};
}

function removePropertiesFromTargetOptions(
Expand All @@ -62,6 +66,7 @@ function removePropertiesFromTargetOptions(
viteConfigPath: string,
projectRoot: string,
projectName: string,
migrationLogs: AggregatedLog,
defaultOptions = false
) {
if ('buildTarget' in targetOptions) {
Expand All @@ -84,7 +89,7 @@ function removePropertiesFromTargetOptions(
}

if ('proxyConfig' in targetOptions) {
aggregateLog({
migrationLogs.addLog({
executorName: '@nx/vite:dev-server',
project: projectName,
log: `Encountered 'proxyConfig' in project.json. You will need to copy the contents of this file to the 'server.proxy' property in your Vite config file.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ function removePropertiesFromTargetOptions(
delete targetOptions.reportsDirectory;
}

// TODO: handle the regex escaping of paths
if ('testFiles' in targetOptions) {
targetOptions.testNamePattern = `/(${targetOptions.testFiles
.map((f) => f.replace('.', '\\.'))
Expand Down

0 comments on commit 0e59ae3

Please sign in to comment.