Skip to content

Commit a947e25

Browse files
committed
fix(core): set workspaces property when using NPM preset
1 parent d8f3c6b commit a947e25

File tree

9 files changed

+171
-123
lines changed

9 files changed

+171
-123
lines changed

e2e/nx-run/src/cache.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('cache', () => {
7272
// --------------------------------------------
7373
updateFile(`nx.json`, (c) => {
7474
const r = JSON.parse(c);
75-
r.affected.defaultBase = 'different';
75+
r.affected = { defaultBase: 'different' };
7676
return JSON.stringify(r);
7777
});
7878
const outputWithNoBuildCached = runCLI(`affected:build ${files}`);

packages/workspace/src/generators/new/__snapshots__/new.spec.ts.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ Object {
7272
exports[`new should generate an empty nx.json 1`] = `
7373
Object {
7474
"$schema": "./node_modules/nx/schemas/nx-schema.json",
75-
"affected": Object {
76-
"defaultBase": "main",
77-
},
7875
"namedInputs": Object {
7976
"default": Array [
8077
"{projectRoot}/**/*",

packages/workspace/src/generators/preset/files/pnpm-workspace/pnpm-workspace.yaml

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/workspace/src/generators/preset/preset.spec.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -149,44 +149,4 @@ describe('preset', () => {
149149

150150
expect(tree.exists('/apps/proj/src/app/App.tsx')).toBe(true);
151151
});
152-
153-
describe('core preset', () => {
154-
describe('package manager workspaces', () => {
155-
it('should be configured in package.json', async () => {
156-
await presetGenerator(tree, {
157-
name: 'proj',
158-
preset: Preset.Core,
159-
linter: 'eslint',
160-
cli: 'nx',
161-
standaloneConfig: false,
162-
packageManager: 'npm',
163-
});
164-
165-
expect(readJson(tree, 'package.json').workspaces)
166-
.toMatchInlineSnapshot(`
167-
Array [
168-
"packages/*",
169-
]
170-
`);
171-
});
172-
173-
it('should be configured in pnpm-workspace.yaml', async () => {
174-
await presetGenerator(tree, {
175-
name: 'proj',
176-
preset: Preset.Core,
177-
linter: 'eslint',
178-
cli: 'nx',
179-
standaloneConfig: false,
180-
packageManager: 'pnpm',
181-
});
182-
183-
expect(tree.read('pnpm-workspace.yaml', 'utf-8'))
184-
.toMatchInlineSnapshot(`
185-
"packages:
186-
- 'packages/*'
187-
"
188-
`);
189-
});
190-
});
191-
});
192152
});

packages/workspace/src/generators/preset/preset.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ async function createPreset(tree: Tree, options: Schema) {
176176
standaloneConfig: options.standaloneConfig,
177177
e2eTestRunner: 'detox',
178178
});
179-
} else if (options.preset === Preset.Core || options.preset === Preset.NPM) {
180-
setupPackageManagerWorkspaces(tree, options);
181179
} else if (options.preset === Preset.TS) {
182180
const c = readWorkspaceConfiguration(tree);
183181
c.workspaceLayout = {
@@ -190,17 +188,6 @@ async function createPreset(tree: Tree, options: Schema) {
190188
}
191189
}
192190

193-
function setupPackageManagerWorkspaces(tree: Tree, options: Schema) {
194-
if (options.packageManager === 'pnpm') {
195-
generateFiles(tree, join(__dirname, './files/pnpm-workspace'), '.', {});
196-
} else {
197-
updateJson(tree, 'package.json', (json) => {
198-
json.workspaces = ['packages/*'];
199-
return json;
200-
});
201-
}
202-
}
203-
204191
function connectAngularAndNest(host: Tree, options: Schema) {
205192
const { insertNgModuleImport } = require('@nrwl' +
206193
'/angular/src/generators/utils');

packages/workspace/src/generators/workspace/files-npm/__dot__gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# See http://help.github.com/ignore-files/ for more about ignoring files.
22

33
# compiled output
4-
/dist
5-
/tmp
4+
dist
5+
tmp
66
/out-tsc
77

88
# dependencies

packages/workspace/src/generators/workspace/files/__dot__gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# See http://help.github.com/ignore-files/ for more about ignoring files.
22

33
# compiled output
4-
/dist
5-
/tmp
4+
dist
5+
tmp
66
/out-tsc
77

88
# dependencies

packages/workspace/src/generators/workspace/workspace.spec.ts

Lines changed: 143 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { workspaceGenerator } from './workspace';
55
import { createTree } from '@nrwl/devkit/testing';
66
import { Preset } from '../utils/presets';
77
import * as nxSchema from '../../../../nx/schemas/nx-schema.json';
8-
import * as workspaceSchema from '../../../../nx/schemas/workspace-schema.json';
98

109
describe('@nrwl/workspace:workspace', () => {
1110
let tree: Tree;
@@ -39,26 +38,32 @@ describe('@nrwl/workspace:workspace', () => {
3938
defaultBase: 'main',
4039
});
4140
const nxJson = readJson<NxJsonConfiguration>(tree, '/proj/nx.json');
42-
expect(nxJson).toEqual({
43-
$schema: './node_modules/nx/schemas/nx-schema.json',
44-
npmScope: 'proj',
45-
affected: {
46-
defaultBase: 'main',
47-
},
48-
tasksRunnerOptions: {
49-
default: {
50-
runner: 'nx/tasks-runners/default',
51-
options: {
52-
cacheableOperations: ['build', 'lint', 'test', 'e2e'],
41+
expect(nxJson).toMatchInlineSnapshot(`
42+
Object {
43+
"$schema": "./node_modules/nx/schemas/nx-schema.json",
44+
"npmScope": "proj",
45+
"targetDefaults": Object {
46+
"build": Object {
47+
"dependsOn": Array [
48+
"^build",
49+
],
5350
},
5451
},
55-
},
56-
targetDefaults: {
57-
build: {
58-
dependsOn: ['^build'],
52+
"tasksRunnerOptions": Object {
53+
"default": Object {
54+
"options": Object {
55+
"cacheableOperations": Array [
56+
"build",
57+
"lint",
58+
"test",
59+
"e2e",
60+
],
61+
},
62+
"runner": "nx/tasks-runners/default",
63+
},
5964
},
60-
},
61-
});
65+
}
66+
`);
6267
const validateNxJson = ajv.compile(nxSchema);
6368
expect(validateNxJson(nxJson)).toEqual(true);
6469
});
@@ -72,32 +77,46 @@ describe('@nrwl/workspace:workspace', () => {
7277
defaultBase: 'main',
7378
});
7479
const nxJson = readJson<NxJsonConfiguration>(tree, '/proj/nx.json');
75-
expect(nxJson).toEqual({
76-
$schema: './node_modules/nx/schemas/nx-schema.json',
77-
npmScope: 'proj',
78-
affected: {
79-
defaultBase: 'main',
80-
},
81-
tasksRunnerOptions: {
82-
default: {
83-
runner: 'nx/tasks-runners/default',
84-
options: {
85-
cacheableOperations: ['build', 'lint', 'test', 'e2e'],
80+
expect(nxJson).toMatchInlineSnapshot(`
81+
Object {
82+
"$schema": "./node_modules/nx/schemas/nx-schema.json",
83+
"namedInputs": Object {
84+
"default": Array [
85+
"{projectRoot}/**/*",
86+
"sharedGlobals",
87+
],
88+
"production": Array [
89+
"default",
90+
],
91+
"sharedGlobals": Array [],
92+
},
93+
"npmScope": "proj",
94+
"targetDefaults": Object {
95+
"build": Object {
96+
"dependsOn": Array [
97+
"^build",
98+
],
99+
"inputs": Array [
100+
"production",
101+
"^production",
102+
],
86103
},
87104
},
88-
},
89-
namedInputs: {
90-
default: ['{projectRoot}/**/*', 'sharedGlobals'],
91-
production: ['default'],
92-
sharedGlobals: [],
93-
},
94-
targetDefaults: {
95-
build: {
96-
dependsOn: ['^build'],
97-
inputs: ['production', '^production'],
105+
"tasksRunnerOptions": Object {
106+
"default": Object {
107+
"options": Object {
108+
"cacheableOperations": Array [
109+
"build",
110+
"lint",
111+
"test",
112+
"e2e",
113+
],
114+
},
115+
"runner": "nx/tasks-runners/default",
116+
},
98117
},
99-
},
100-
});
118+
}
119+
`);
101120
});
102121

103122
it('should create a prettierrc file', async () => {
@@ -155,14 +174,14 @@ describe('@nrwl/workspace:workspace', () => {
155174

156175
const { scripts } = readJson(tree, '/proj/package.json');
157176
expect(scripts).toMatchInlineSnapshot(`
158-
Object {
159-
"build": "nx build",
160-
"ng": "nx",
161-
"postinstall": "node ./decorate-angular-cli.js",
162-
"start": "nx serve",
163-
"test": "nx test",
164-
}
165-
`);
177+
Object {
178+
"build": "nx build",
179+
"ng": "nx",
180+
"postinstall": "node ./decorate-angular-cli.js",
181+
"start": "nx serve",
182+
"test": "nx test",
183+
}
184+
`);
166185
});
167186

168187
it('should not add decorate-angular-cli when used with nx cli', async () => {
@@ -177,29 +196,94 @@ Object {
177196

178197
const { scripts } = readJson(tree, '/proj/package.json');
179198
expect(scripts).toMatchInlineSnapshot(`
180-
Object {
181-
"build": "nx build",
182-
"start": "nx serve",
183-
"test": "nx test",
184-
}
185-
`);
199+
Object {
200+
"build": "nx build",
201+
"start": "nx serve",
202+
"test": "nx test",
203+
}
204+
`);
186205
});
187206

188-
it('should create a workspace using package layout', async () => {
207+
it('should create a workspace using NPM preset (npm package manager)', async () => {
208+
tree.write('/proj/package.json', JSON.stringify({}));
189209
await workspaceGenerator(tree, {
190210
name: 'proj',
191211
directory: 'proj',
192212
cli: 'nx',
193213
preset: Preset.NPM,
194214
defaultBase: 'main',
215+
packageManager: 'npm',
195216
});
196217
expect(tree.exists('/proj/packages/.gitkeep')).toBe(true);
197218
expect(tree.exists('/proj/apps/.gitkeep')).toBe(false);
198219
expect(tree.exists('/proj/libs/.gitkeep')).toBe(false);
199220
const nx = readJson(tree, '/proj/nx.json');
200-
expect(nx.extends).toEqual('nx/presets/npm.json');
221+
expect(nx).toMatchInlineSnapshot(`
222+
Object {
223+
"$schema": "./node_modules/nx/schemas/nx-schema.json",
224+
"extends": "nx/presets/npm.json",
225+
"tasksRunnerOptions": Object {
226+
"default": Object {
227+
"options": Object {
228+
"cacheableOperations": Array [
229+
"build",
230+
"lint",
231+
"test",
232+
"e2e",
233+
],
234+
},
235+
"runner": "nx/tasks-runners/default",
236+
},
237+
},
238+
}
239+
`);
201240

202-
const { scripts } = readJson(tree, '/proj/package.json');
203-
expect(scripts).toMatchInlineSnapshot(`Object {}`);
241+
const packageJson = readJson(tree, '/proj/package.json');
242+
expect(packageJson).toMatchInlineSnapshot(`
243+
Object {
244+
"dependencies": Object {},
245+
"devDependencies": Object {
246+
"nx": "0.0.1",
247+
"prettier": "^2.6.2",
248+
},
249+
"license": "MIT",
250+
"name": "proj",
251+
"private": true,
252+
"scripts": Object {},
253+
"version": "0.0.0",
254+
"workspaces": Array [
255+
"packages/*",
256+
],
257+
}
258+
`);
259+
});
260+
261+
it('should create a workspace using NPM preset (pnpm package manager)', async () => {
262+
tree.write('/proj/package.json', JSON.stringify({}));
263+
await workspaceGenerator(tree, {
264+
name: 'proj',
265+
directory: 'proj',
266+
cli: 'nx',
267+
preset: Preset.NPM,
268+
defaultBase: 'main',
269+
packageManager: 'pnpm',
270+
});
271+
const packageJson = readJson(tree, '/proj/package.json');
272+
expect(packageJson).toMatchInlineSnapshot(`
273+
Object {
274+
"dependencies": Object {},
275+
"devDependencies": Object {
276+
"nx": "0.0.1",
277+
"prettier": "^2.6.2",
278+
},
279+
"license": "MIT",
280+
"name": "proj",
281+
"private": true,
282+
"scripts": Object {},
283+
"version": "0.0.0",
284+
}
285+
`);
286+
const pnpm = tree.read('/proj/pnpm-workspace.yaml').toString();
287+
expect(pnpm).toContain('packages/*');
204288
});
205289
});

0 commit comments

Comments
 (0)