Skip to content

Commit

Permalink
fix(misc): setup package manager workspaces for core preset (#8621)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Jan 20, 2022
1 parent 5212fc9 commit 5c921d8
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 2 deletions.
22 changes: 22 additions & 0 deletions e2e/workspace-create/src/create-nx-workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
getSelectedPackageManager,
packageManagerLockFile,
readJson,
runCLI,
runCreateWorkspace,
updateJson,
updateFile,
uniq,
} from '@nrwl/e2e/utils';
import { existsSync, mkdirSync } from 'fs-extra';
Expand Down Expand Up @@ -47,6 +50,25 @@ describe('create-nx-workspace', () => {
});

expectNoAngularDevkit();

const parent = uniq('parent');
const child = uniq('child');
runCLI(`generate npm-package ${parent}`);
runCLI(`generate npm-package ${child}`);

updateJson(`packages/${parent}/package.json`, (json) => {
json.dependencies = {
[`@${wsName}/${child}`]: '*',
};
return json;
});
updateFile(
`packages/${parent}/src/index.js`,
`require('@${wsName}/${child}');`
);

runCLI(`test ${parent}`);
runCLI(`test ${child}`);
});

it('should be able to create an empty workspace with ts/js capabilities', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/workspace/src/generators/new/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function generatePreset(host: Tree, opts: NormalizedSchema) {
opts.linter ? `--linter=${opts.linter}` : null,
opts.npmScope ? `--npmScope=${opts.npmScope}` : `--npmScope=${opts.name}`,
opts.preset ? `--preset=${opts.preset}` : null,
opts.packageManager ? `--packageManager=${opts.packageManager}` : null,
`--cli=${cliCommand}`,
parsedArgs.interactive ? '--interactive=true' : '--interactive=false',
].filter((e) => !!e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages:
- 'packages/**'
40 changes: 40 additions & 0 deletions packages/workspace/src/generators/preset/preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,44 @@ describe('preset', () => {
readJson<NxJsonConfiguration>(tree, 'nx.json').cli.defaultCollection
).toBe('@nrwl/react-native');
});

describe('core preset', () => {
describe('package manager workspaces', () => {
it('should be configured in package.json', async () => {
await presetGenerator(tree, {
name: 'proj',
preset: Preset.Core,
linter: 'eslint',
cli: 'nx',
standaloneConfig: false,
packageManager: 'npm',
});

expect(readJson(tree, 'package.json').workspaces)
.toMatchInlineSnapshot(`
Array [
"packages/**",
]
`);
});

it('should be configured in pnpm-workspace.yaml', async () => {
await presetGenerator(tree, {
name: 'proj',
preset: Preset.Core,
linter: 'eslint',
cli: 'nx',
standaloneConfig: false,
packageManager: 'pnpm',
});

expect(tree.read('pnpm-workspace.yaml', 'utf-8'))
.toMatchInlineSnapshot(`
"packages:
- 'packages/**'
"
`);
});
});
});
});
19 changes: 17 additions & 2 deletions packages/workspace/src/generators/preset/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import {
addDependenciesToPackageJson,
convertNxGenerator,
formatFiles,
generateFiles,
installPackagesTask,
names,
PackageManager,
readWorkspaceConfiguration,
Tree,
updateJson,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import { Schema } from './schema';
Expand All @@ -15,6 +18,7 @@ import { libraryGenerator } from '../library/library';
import { insertImport } from '../utils/insert-import';
import { insertStatement } from '../utils/insert-statement';
import { Preset } from '../utils/presets';
import { join } from 'path';

export async function presetGenerator(tree: Tree, options: Schema) {
options = normalizeOptions(options);
Expand All @@ -32,8 +36,6 @@ async function createPreset(tree: Tree, options: Schema) {
if (
options.preset === Preset.Empty ||
options.preset === Preset.Apps ||
options.preset === Preset.NPM ||
options.preset === Preset.Core ||
options.preset === Preset.TS
) {
return;
Expand Down Expand Up @@ -180,11 +182,24 @@ async function createPreset(tree: Tree, options: Schema) {
e2eTestRunner: 'detox',
});
setDefaultCollection(tree, '@nrwl/react-native');
} else if (options.preset === Preset.Core || options.preset === Preset.NPM) {
setupPackageManagerWorkspaces(tree, options);
} else {
throw new Error(`Invalid preset ${options.preset}`);
}
}

function setupPackageManagerWorkspaces(tree: Tree, options: Schema) {
if (options.packageManager === 'pnpm') {
generateFiles(tree, join(__dirname, './files/pnpm-workspace'), '.', {});
} else {
updateJson(tree, 'package.json', (json) => {
json.workspaces = ['packages/**'];
return json;
});
}
}

function connectAngularAndNest(host: Tree, options: Schema) {
const { insertNgModuleImport } = require('@nrwl' +
'/angular/src/generators/utils');
Expand Down
2 changes: 2 additions & 0 deletions packages/workspace/src/generators/preset/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Preset } from '../utils/presets';
import { PackageManager } from '@nrwl/devkit';

export interface Schema {
name: string;
Expand All @@ -8,4 +9,5 @@ export interface Schema {
linter?: string;
preset: Preset;
standaloneConfig?: boolean;
packageManager?: PackageManager;
}
5 changes: 5 additions & 0 deletions packages/workspace/src/generators/preset/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"standaloneConfig": {
"description": "Split the project configurations into <projectRoot>/project.json rather than including it inside workspace.json",
"type": "boolean"
},
"packageManager": {
"description": "The package manager used to install dependencies.",
"type": "string",
"enum": ["npm", "yarn", "pnpm"]
}
}
}

0 comments on commit 5c921d8

Please sign in to comment.