Skip to content

Commit

Permalink
feat(js): ensure that new workspaces entries are added as globs to re…
Browse files Browse the repository at this point in the history
…move too many entries
  • Loading branch information
jaysoo committed Dec 18, 2024
1 parent e6c4bcc commit 2f3a288
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/expo/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export async function expoLibraryGeneratorInternal(
);

if (options.isUsingTsSolutionConfig) {
addProjectToTsSolutionWorkspace(host, `${options.projectRoot}/*`);
addProjectToTsSolutionWorkspace(host, options.projectRoot);
}

if (!options.skipFormat) {
Expand Down
37 changes: 19 additions & 18 deletions packages/js/src/utils/typescript/ts-solution-setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
detectPackageManager,
joinPathFragments,
offsetFromRoot,
output,
Expand All @@ -11,7 +10,7 @@ import {
} from '@nx/devkit';
import { FsTree } from 'nx/src/generators/tree';
import { isUsingPackageManagerWorkspaces } from '../package-manager-workspaces';
import { basename, join, relative } from 'node:path/posix';
import { basename, dirname, join, relative } from 'node:path/posix';

export function isUsingTypeScriptPlugin(tree: Tree): boolean {
const nxJson = readNxJson(tree);
Expand Down Expand Up @@ -210,23 +209,25 @@ export function addProjectToTsSolutionWorkspace(
tree: Tree,
projectDir: string
) {
if (detectPackageManager() === 'pnpm') {
// If dir is "libs/foo" then use "libs/**" so we don't need so many entries in the workspace file.
// If the dir is just "foo" then we have to add it as is.
const baseDir = dirname(projectDir);
const pattern = baseDir === '.' ? projectDir : `${baseDir}/**`;
if (tree.exists('pnpm-workspace.yaml')) {
const { load, dump } = require('@zkochan/js-yaml');
if (tree.exists('pnpm-workspace.yaml')) {
const workspaceFile = tree.read('pnpm-workspace.yaml', 'utf-8');
const yamlData = load(workspaceFile);
const workspaceFile = tree.read('pnpm-workspace.yaml', 'utf-8');
const yamlData = load(workspaceFile);

if (!yamlData?.packages) {
yamlData.packages = [];
}
if (!yamlData?.packages) {
yamlData.packages = [];
}

if (!yamlData.packages.includes(projectDir)) {
yamlData.packages.push(projectDir);
tree.write(
'pnpm-workspace.yaml',
dump(yamlData, { indent: 2, quotingType: '"', forceQuotes: true })
);
}
if (!yamlData.packages.includes(pattern)) {
yamlData.packages.push(pattern);
tree.write(
'pnpm-workspace.yaml',
dump(yamlData, { indent: 2, quotingType: '"', forceQuotes: true })
);
}
} else {
// Update package.json
Expand All @@ -235,8 +236,8 @@ export function addProjectToTsSolutionWorkspace(
packageJson.workspaces = [];
}

if (!packageJson.workspaces.includes(projectDir)) {
packageJson.workspaces.push(projectDir);
if (!packageJson.workspaces.includes(pattern)) {
packageJson.workspaces.push(pattern);
tree.write('package.json', JSON.stringify(packageJson, null, 2));
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export async function libraryGeneratorInternal(host: Tree, rawOptions: Schema) {
);

if (options.isUsingTsSolutionConfig) {
addProjectToTsSolutionWorkspace(host, `${options.projectRoot}/*`);
addProjectToTsSolutionWorkspace(host, options.projectRoot);
}

if (!options.skipFormat) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export async function reactNativeLibraryGeneratorInternal(
);

if (options.isUsingTsSolutionConfig) {
addProjectToTsSolutionWorkspace(host, `${options.projectRoot}/*`);
addProjectToTsSolutionWorkspace(host, options.projectRoot);
}

if (!options.skipFormat) {
Expand Down
66 changes: 62 additions & 4 deletions packages/react/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Linter } from '@nx/eslint';
import { applicationGenerator } from './application';
import { Schema } from './schema';

const { load } = require('@zkochan/js-yaml');
// need to mock cypress otherwise it'll use the nx installed version from package.json
// which is v9 while we are testing for the new v10 version
Expand Down Expand Up @@ -1277,9 +1278,8 @@ describe('app', () => {
describe('TS solution setup', () => {
beforeEach(() => {
appTree = createTreeWithEmptyWorkspace();
appTree.write('pnpm-workspace.yaml', `packages:`);
updateJson(appTree, 'package.json', (json) => {
json.workspaces = ['packages/*', 'apps/*'];
json.workspaces = ['packages/**', 'apps/**'];
return json;
});
writeJson(appTree, 'tsconfig.base.json', {
Expand Down Expand Up @@ -1456,7 +1456,7 @@ describe('app', () => {
`);
});

it('should add project to workspaces when using TS solution', async () => {
it('should add project to workspaces when using TS solution (npm, yarn, bun)', async () => {
await applicationGenerator(appTree, {
directory: 'myapp',
addPlugin: true,
Expand All @@ -1466,11 +1466,69 @@ describe('app', () => {
unitTestRunner: 'none',
e2eTestRunner: 'none',
});
await applicationGenerator(appTree, {
directory: 'libs/nested1',
addPlugin: true,
linter: Linter.EsLint,
style: 'none',
bundler: 'vite',
unitTestRunner: 'none',
e2eTestRunner: 'none',
});
await applicationGenerator(appTree, {
directory: 'libs/nested2',
addPlugin: true,
linter: Linter.EsLint,
style: 'none',
bundler: 'vite',
unitTestRunner: 'none',
e2eTestRunner: 'none',
});

const packageJson = readJson(appTree, 'package.json');
expect(packageJson.workspaces).toEqual([
'packages/**',
'apps/**',
'myapp',
'libs/**',
]);
});

it('should add project to workspaces when using TS solution (pnpm)', async () => {
appTree.write('pnpm-workspace.yaml', `packages:`);

await applicationGenerator(appTree, {
directory: 'myapp',
addPlugin: true,
linter: Linter.EsLint,
style: 'none',
bundler: 'vite',
unitTestRunner: 'none',
e2eTestRunner: 'none',
});
await applicationGenerator(appTree, {
directory: 'apps/nested1',
addPlugin: true,
linter: Linter.EsLint,
style: 'none',
bundler: 'vite',
unitTestRunner: 'none',
e2eTestRunner: 'none',
});
await applicationGenerator(appTree, {
directory: 'apps/nested2',
addPlugin: true,
linter: Linter.EsLint,
style: 'none',
bundler: 'vite',
unitTestRunner: 'none',
e2eTestRunner: 'none',
});

const pnpmContent = appTree.read('pnpm-workspace.yaml', 'utf-8');
const pnpmWorkspaceFile = load(pnpmContent);

expect(pnpmWorkspaceFile.packages).toEqual(['myapp']);
expect(pnpmWorkspaceFile.packages).toEqual(['myapp', 'apps/**']);
});
});

Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,6 @@ module.exports = withNx(
describe('TS solution setup', () => {
beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
tree.write('pnpm-workspace.yaml', `packages:`);
updateJson(tree, 'package.json', (json) => {
json.workspaces = ['packages/*', 'apps/*'];
return json;
Expand Down Expand Up @@ -1252,6 +1251,8 @@ module.exports = withNx(
});

it('should add project to workspaces when using TS solution', async () => {
tree.write('pnpm-workspace.yaml', `packages:`);

await libraryGenerator(tree, {
...defaultSchema,
bundler: 'rollup',
Expand All @@ -1262,7 +1263,7 @@ module.exports = withNx(
const pnpmContent = tree.read('pnpm-workspace.yaml', 'utf-8');
const pnpmWorkspaceFile = load(pnpmContent);

expect(pnpmWorkspaceFile.packages).toEqual(['mylib/*']);
expect(pnpmWorkspaceFile.packages).toEqual(['mylib']);
});
});
});
2 changes: 1 addition & 1 deletion packages/react/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export async function libraryGeneratorInternal(host: Tree, schema: Schema) {
);

if (options.isUsingTsSolutionConfig) {
addProjectToTsSolutionWorkspace(host, `${options.projectRoot}/*`);
addProjectToTsSolutionWorkspace(host, options.projectRoot);
}
if (!options.skipFormat) {
await formatFiles(host);
Expand Down
2 changes: 1 addition & 1 deletion packages/remix/src/generators/library/library.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export async function remixLibraryGeneratorInternal(
);

if (options.isUsingTsSolutionConfig) {
addProjectToTsSolutionWorkspace(tree, `${options.projectRoot}/*`);
addProjectToTsSolutionWorkspace(tree, options.projectRoot);
}

if (!options.skipFormat) {
Expand Down

0 comments on commit 2f3a288

Please sign in to comment.