Skip to content

Commit

Permalink
feat(workspace-plugin): make split-lib-in-two shared utils to be used…
Browse files Browse the repository at this point in the history
… in other related generators
  • Loading branch information
Hotell committed Apr 23, 2024
1 parent b71e942 commit 0817545
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ 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 project = readProjectConfiguration(tree, options.name);

const isSplitProject = tree.exists(joinPathFragments(project.root, '../stories/project.json'));

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

const configPaths = {
bundleSizeRoot: joinPathFragments(project.root, 'bundle-size'),
Expand Down Expand Up @@ -62,15 +65,8 @@ function normalizeOptions(tree: Tree, schema: BundleSizeConfigurationGeneratorSc
};
}

function assertOptions(tree: Tree, options: ReturnType<typeof normalizeOptions> & { isSplitProject: boolean }) {
if (options.isSplitProject && options.name.endsWith('-stories')) {
throw new Error(
`This generator can be invoked only against library project. Please run it against "${options.name.replace(
'-stories',
'',
)}" library project.`,
);
}
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 @@ -4,6 +4,8 @@ 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
Expand Down Expand Up @@ -44,14 +46,7 @@ function assertOptions(tree: Tree, options: ReturnType<typeof normalizeOptions>
return false;
}

if (options.isSplitProject && options.name.endsWith('-stories')) {
throw new Error(
`This generator can be invoked only against library project. Please run it against "${options.name.replace(
'-stories',
'',
)}" library project.`,
);
}
assertStoriesProject(tree, { isSplitProject: options.isSplitProject, project: options.projectConfig });

return true;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { execSync } from 'child_process';

import {
Tree,
formatFiles,
Expand All @@ -13,7 +15,6 @@ import {
stripIndents,
workspaceRoot,
} from '@nrwl/devkit';

import * as tsquery from '@phenomnomnominal/tsquery';

import { getProjectConfig, getProjectNameWithoutScope, workspacePaths } from '../../utils';
Expand All @@ -22,15 +23,16 @@ import { PackageJson, TsConfig } from '../../types';

import tsConfigBaseAll from '../tsconfig-base-all';

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

import { ReleasePackageGeneratorSchema } from './schema';
import { execSync } from 'child_process';

interface NormalizedSchema extends ReturnType<typeof normalizeOptions> {}

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

const isSplitProject = tree.exists(joinPathFragments(options.projectConfig.root, '../stories/project.json'));
const isSplitProject = isSplitProjectFn(tree, options.projectConfig);

assertProject(tree, { isSplitProject, ...options });

Expand Down Expand Up @@ -400,14 +402,7 @@ function assertProject(tree: Tree, options: NormalizedSchema & { isSplitProject:
throw new Error(`${options.project} is already released as stable.`);
}

if (options.isSplitProject && options.name.endsWith('-stories')) {
throw new Error(
`This generator can be invoked only against library project. Please run it against "${options.name.replace(
'-stories',
'',
)}" library project.`,
);
}
assertStoriesProject(tree, { isSplitProject: options.isSplitProject, project: options.projectConfig });
}

function createExportsInSuite(content: string, packageName: string) {
Expand Down
17 changes: 6 additions & 11 deletions tools/workspace-plugin/src/generators/react-component/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import path from 'path';
import { execSync } from 'child_process';
import { Tree, formatFiles, names, generateFiles, joinPathFragments, workspaceRoot } from '@nx/devkit';

import { getProjectConfig, isPackageConverged } from '../../utils';
import { assertStoriesProject, isSplitProject } from '../split-library-in-two/shared';

import { ReactComponentGeneratorSchema } from './schema';
import { execSync } from 'child_process';

interface NormalizedSchema extends ReturnType<typeof normalizeOptions> {}

Expand Down Expand Up @@ -43,7 +44,6 @@ export default async function (tree: Tree, schema: ReactComponentGeneratorSchema
function normalizeOptions(tree: Tree, options: ReactComponentGeneratorSchema) {
const project = getProjectConfig(tree, { packageName: options.project });
const nameCasings = names(options.name);
const isSplitProject = tree.exists(joinPathFragments(project.projectConfig.root, '../stories/project.json'));

return {
...options,
Expand All @@ -52,7 +52,7 @@ function normalizeOptions(tree: Tree, options: ReactComponentGeneratorSchema) {
directory: 'components',
componentName: nameCasings.className,
npmPackageName: project.projectConfig.name as string,
isSplitProject,
isSplitProject: isSplitProject(tree, project.projectConfig),
};
}

Expand Down Expand Up @@ -131,13 +131,8 @@ function assertComponent(tree: Tree, options: NormalizedSchema) {
if (tree.exists(componentDirPath)) {
throw new Error(`The component "${options.componentName}" already exists`);
}
if (options.isSplitProject && options.projectConfig.name?.endsWith('-stories')) {
throw new Error(
`This generator can be invoked only against library project. Please run it against "${options.projectConfig.name.replace(
'-stories',
'',
)}" library project.`,
);
}

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

return;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { TsConfig } from '../../types';
import { workspacePaths } from '../../utils';
import { SplitLibraryInTwoGeneratorSchema } from './schema';

export { isSplitProject, assertStoriesProject } from './shared';

interface Options extends SplitLibraryInTwoGeneratorSchema {
projectConfig: ReturnType<typeof readProjectConfiguration>;
projectOffsetFromRoot: { old: string; updated: string };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { joinPathFragments, type ProjectConfiguration, type Tree } from '@nx/devkit';

export const isSplitProject = (tree: Tree, project: ProjectConfiguration) =>
tree.exists(joinPathFragments(project.root, '../stories/project.json'));

export function assertStoriesProject(tree: Tree, options: { isSplitProject: boolean; project: ProjectConfiguration }) {
if (options.isSplitProject && options.project.name?.endsWith('-stories')) {
throw new Error(
`This generator can be invoked only against library project. Please run it against "${options.project.name.replace(
'-stories',
'',
)}" library project.`,
);
}
}

0 comments on commit 0817545

Please sign in to comment.