Skip to content

Commit

Permalink
fix(react): add release option for @nx/react:lib --publishable
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Feb 4, 2025
1 parent 7c2c046 commit 1122711
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
18 changes: 13 additions & 5 deletions e2e/release/src/publishable-libraries-release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import {
describe('publishable libraries release', () => {
beforeAll(async () => {
newProject({
packages: ['@nx/js'],
packages: ['@nx/js', '@nx/react'],
});

// Normalize git committer information so it is deterministic in snapshots
await runCommandAsync(`git config user.email "[email protected]"`);
await runCommandAsync(`git config user.name "Test"`);
Expand All @@ -25,14 +24,23 @@ describe('publishable libraries release', () => {
});
afterAll(() => cleanupProject());

it('should be able to release publishable js library', async () => {
it('should be able release js publishable libraries', async () => {
const jsLib = uniq('js-lib');
runCLI(
`generate @nx/js:lib ${jsLib} --publishable --importPath=@proj/${jsLib}`
);

let versionOutput = runCLI(`release --first-release`);
versionOutput = runCLI(`release patch`);
runCLI(`release --first-release`);
const versionOutput = runCLI(`release patch`);
expect(versionOutput).toContain('Executing pre-version command');
});

it('should be able release react publishable libraries', async () => {
const reactLib = uniq('react-lib');
runCLI(
`generate @nx/react:lib ${reactLib} --publishable --importPath=@proj/${reactLib}`
);
const versionOutput = runCLI(`release patch`);
expect(versionOutput).toContain('Executing pre-version command');
});
});
4 changes: 2 additions & 2 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ async function configureProject(
tree,
options.name,
projectConfiguration,
defaultOutputDirectory,
options.isUsingTsSolutionConfig
options.isUsingTsSolutionConfig,
defaultOutputDirectory
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export async function addReleaseOptionForPublishableTarget(
tree: Tree,
projectName: string,
projectConfiguration: ProjectConfiguration,
defaultOutputDirectory: string = 'dist',
isUsingTsSolutionConfig: boolean = false
isUsingTsSolutionConfig: boolean = false,
defaultOutputDirectory: string = 'dist'
): Promise<ProjectConfiguration> {
if (!isUsingTsSolutionConfig) {
const packageRoot = joinPathFragments(
Expand Down
25 changes: 17 additions & 8 deletions packages/react/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
GeneratorCallback,
installPackagesTask,
joinPathFragments,
readNxJson,
readProjectConfiguration,
runTasksInSerial,
Tree,
updateJson,
updateNxJson,
updateProjectConfiguration,
writeJson,
} from '@nx/devkit';
import { getRelativeCwd } from '@nx/devkit/src/generators/artifact-name-and-directory-utils';
Expand All @@ -37,6 +37,10 @@ import {
} from '@nx/js/src/utils/typescript/ts-solution-setup';
import { determineEntryFields } from './lib/determine-entry-fields';
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';
import {
addReleaseOptionForPublishableTarget,
releaseTasks,
} from '@nx/js/src/generators/library/utils/add-release-config';

export async function libraryGenerator(host: Tree, schema: Schema) {
return await libraryGeneratorInternal(host, {
Expand Down Expand Up @@ -77,7 +81,7 @@ export async function libraryGeneratorInternal(host: Tree, schema: Schema) {

if (options.isUsingTsSolutionConfig) {
writeJson(host, `${options.projectRoot}/package.json`, {
name: options.importPath,
name: options.importPath ?? options.name,
version: '0.0.1',
...determineEntryFields(options),
nx: options.parsedTags?.length
Expand Down Expand Up @@ -236,11 +240,16 @@ export async function libraryGeneratorInternal(host: Tree, schema: Schema) {
tasks.push(componentTask);
}

if (options.publishable || options.buildable) {
updateJson(host, `${options.projectRoot}/package.json`, (json) => {
json.name = options.importPath;
return json;
});
if (options.publishable) {
const projectConfiguration = readProjectConfiguration(host, options.name);
await addReleaseOptionForPublishableTarget(
host,
options.name,
projectConfiguration,
options.isUsingTsSolutionConfig
);
updateProjectConfiguration(host, options.name, projectConfiguration);
tasks.push(await releaseTasks(host));
}

if (!options.skipPackageJson) {
Expand Down

0 comments on commit 1122711

Please sign in to comment.