Skip to content

Commit

Permalink
feat(workspace-plugin): make all react library generators work with b…
Browse files Browse the repository at this point in the history
…oth split project and old project setup (#31142)
  • Loading branch information
Hotell authored May 22, 2024
1 parent 7065058 commit b153d70
Show file tree
Hide file tree
Showing 15 changed files with 940 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,35 @@ import {
generateFiles,
joinPathFragments,
offsetFromRoot,
ProjectConfiguration,
readProjectConfiguration,
Tree,
updateJson,
visitNotIgnoredFiles,
} from '@nx/devkit';

import * as path from 'path';

import { PackageJson } from '../../types';
import { assertStoriesProject, isSplitProject } from '../split-library-in-two/shared';

import { BundleSizeConfigurationGeneratorSchema } from './schema';

export async function bundleSizeConfigurationGenerator(tree: Tree, schema: BundleSizeConfigurationGeneratorSchema) {
const options = normalizeOptions(tree, schema);

const config = readProjectConfiguration(tree, options.name);
const project = readProjectConfiguration(tree, options.name);

assertOptions(tree, { isSplitProject: isSplitProject(tree, project), project });

const configPaths = {
bundleSizeRoot: joinPathFragments(config.root, 'bundle-size'),
bundleSizeConfig: joinPathFragments(config.root, 'monosize.config.mjs'),
bundleSizeRoot: joinPathFragments(project.root, 'bundle-size'),
bundleSizeConfig: joinPathFragments(project.root, 'monosize.config.mjs'),
};

generateFiles(tree, path.join(__dirname, 'files'), config.root, {
generateFiles(tree, path.join(__dirname, 'files'), project.root, {
packageName: options.name,
rootOffset: offsetFromRoot(config.root),
rootOffset: offsetFromRoot(project.root),
});

let hasFixtures = false;
Expand All @@ -41,7 +49,7 @@ export async function bundleSizeConfigurationGenerator(tree: Tree, schema: Bundl
tree.delete(configPaths.bundleSizeConfig);
}

updateJson(tree, joinPathFragments(config.root, 'package.json'), (json: PackageJson) => {
updateJson(tree, joinPathFragments(project.root, 'package.json'), (json: PackageJson) => {
json.scripts = json.scripts ?? {};
json.scripts['bundle-size'] = 'monosize measure';
return json;
Expand All @@ -57,4 +65,8 @@ function normalizeOptions(tree: Tree, schema: BundleSizeConfigurationGeneratorSc
};
}

function assertOptions(tree: Tree, options: { project: ProjectConfiguration; isSplitProject: boolean }) {
assertStoriesProject(tree, options);
}

export default bundleSizeConfigurationGenerator;
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@ describe(`cypress-component-configuration`, () => {
}
`);

expect(readJson(tree, 'packages/one/package.json').scripts).toEqual(
const pkgJson = readJson(tree, 'packages/one/package.json');

expect(pkgJson.scripts).toEqual(
expect.objectContaining({
e2e: 'cypress run --component',
'e2e:local': 'cypress open --component',
}),
);
expect(pkgJson.devDependencies).toEqual(
expect.objectContaining({
'@fluentui/scripts-cypress': '*',
}),
);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { Tree, formatFiles, names } from '@nx/devkit';
import { Tree, formatFiles, names, joinPathFragments } from '@nx/devkit';

import { getProjectConfig, printUserLogs, UserLog } from '../../utils';

import type { CypressComponentConfigurationGeneratorSchema } from './schema';

import { assertStoriesProject } from '../split-library-in-two/shared';

import { addFiles } from './lib/add-files';

// eslint-disable-next-line @typescript-eslint/naming-convention
interface _NormalizedSchema extends ReturnType<typeof normalizeOptions> {}

export default async function (tree: Tree, schema: CypressComponentConfigurationGeneratorSchema) {
const userLog: UserLog = [];
const normalizedOptions = normalizeOptions(tree, schema);

if (normalizedOptions.projectConfig.projectType === 'application') {
userLog.push({ type: 'warn', message: 'we dont support cypress component tests for applications' });
printUserLogs(userLog);
const isSplitProject = tree.exists(
joinPathFragments(normalizedOptions.projectConfig.root, '../stories/project.json'),
);

if (!assertOptions(tree, { isSplitProject, ...normalizedOptions })) {
return;
}

Expand All @@ -33,3 +36,17 @@ function normalizeOptions(tree: Tree, options: CypressComponentConfigurationGene
...names(options.project),
};
}

function assertOptions(tree: Tree, options: ReturnType<typeof normalizeOptions> & { isSplitProject: boolean }) {
if (options.projectConfig.projectType === 'application') {
const userLog: UserLog = [];
userLog.push({ type: 'warn', message: `We don't support cypress component tests for applications` });
printUserLogs(userLog);

return false;
}

assertStoriesProject(tree, { isSplitProject: options.isSplitProject, project: options.projectConfig });

return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export function addFiles(tree: Tree, options: Options) {
json.scripts.e2e = 'cypress run --component';
json.scripts['e2e:local'] = 'cypress open --component';

json.devDependencies = json.devDependencies ?? {};
json.devDependencies['@fluentui/scripts-cypress'] = '*';

return json;
});

Expand Down
Loading

0 comments on commit b153d70

Please sign in to comment.