Skip to content

Commit 165363b

Browse files
committed
fix: use nx run-many vs nx affected in postinstall script
1 parent 3e733d9 commit 165363b

File tree

7 files changed

+122
-27
lines changed

7 files changed

+122
-27
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"schematics": {
3+
"update-restore-postinstall": {
4+
"version": "1.1.1",
5+
"description": "Use 'nx run-many' instead of 'nx affected' to restore .NET core projects when using Angular CLI",
6+
"factory": "./src/migrations/update-1-1-1/update-restore-postinstall"
7+
}
8+
}
9+
}

packages/nx-dotnet-core/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"typings": "./src/index.d.ts",
1010
"generators": "./generators.json",
1111
"executors": "./executors.json",
12+
"ng-update": {
13+
"requirements": {},
14+
"migrations": "./migrations.json"
15+
},
1216
"homepage": "https://github.com/bbaia/nx-dotnet-core",
1317
"bugs": {
1418
"url": "https://github.com/bbaia/nx-dotnet-core/issues"

packages/nx-dotnet-core/src/generators/new/generator.spec.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
readJson,
55
readProjectConfiguration,
66
Tree,
7+
updateJson,
78
writeJson,
89
} from '@nrwl/devkit';
910

@@ -145,19 +146,6 @@ describe('new generator', () => {
145146
'my-app': { tags: [] },
146147
});
147148
});
148-
149-
it('should update package.json', async () => {
150-
const options: NewGeneratorSchema = {
151-
type: 'app',
152-
template: 'webapi',
153-
name: 'my-app',
154-
};
155-
156-
await generator(tree, options);
157-
158-
const packageJson = readJson(tree, '/package.json');
159-
expect(packageJson.scripts.postinstall).toEqual('nx restore --all');
160-
});
161149
});
162150

163151
describe('library', () => {
@@ -269,19 +257,6 @@ describe('new generator', () => {
269257
'my-lib': { tags: [] },
270258
});
271259
});
272-
273-
it('should update package.json', async () => {
274-
const options: NewGeneratorSchema = {
275-
type: 'lib',
276-
template: 'classlib',
277-
name: 'my-lib',
278-
};
279-
280-
await generator(tree, options);
281-
282-
const packageJson = readJson(tree, '/package.json');
283-
expect(packageJson.scripts.postinstall).toEqual('nx restore --all');
284-
});
285260
});
286261

287262
describe('--unitTestTemplate', () => {
@@ -382,6 +357,37 @@ describe('new generator', () => {
382357
});
383358
});
384359

360+
describe('should update postinstall script in package.json', () => {
361+
const options: NewGeneratorSchema = {
362+
type: 'app',
363+
template: 'webapi',
364+
name: 'my-app',
365+
};
366+
367+
it('Nx workspace', async () => {
368+
await generator(tree, options);
369+
370+
const packageJson = readJson(tree, '/package.json');
371+
expect(packageJson.scripts.postinstall).toEqual('nx restore --all');
372+
});
373+
374+
it('Angular CLI powered workspace', async () => {
375+
// fake Angular CLI powered workspace
376+
updateJson(tree, '/package.json', json => {
377+
console.log(json);
378+
json.scripts = { ng: 'nx' };
379+
return json;
380+
});
381+
382+
await generator(tree, options);
383+
384+
const packageJson = readJson(tree, '/package.json');
385+
expect(packageJson.scripts.postinstall).toEqual(
386+
'nx run-many --target=restore --all',
387+
);
388+
});
389+
});
390+
385391
describe('Visual Studio Code extension', () => {
386392
const options: NewGeneratorSchema = {
387393
type: 'app',

packages/nx-dotnet-core/src/generators/new/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function addPostInstall(host: Tree) {
186186
updateJson(host, 'package.json', json => {
187187
json.scripts = json.scripts || {};
188188
const command = json.scripts.ng
189-
? 'nx affected --target=restore --all'
189+
? 'nx run-many --target=restore --all'
190190
: 'nx restore --all';
191191
if (!json.scripts.postinstall) {
192192
json.scripts.postinstall = command;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { join } from 'path';
2+
import { Tree } from '@angular-devkit/schematics';
3+
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
4+
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
5+
6+
describe('Should replace "nx affected" with "nx run-many" in postinstall script', () => {
7+
const migrationTestRunner = new SchematicTestRunner(
8+
'@bbaia/nx-dotnet-core',
9+
join(__dirname, '../../../migrations.json'),
10+
);
11+
12+
[
13+
{
14+
test: 'nx affected --target=restore --all',
15+
expected: 'nx run-many --target=restore --all',
16+
},
17+
{
18+
test: 'echo "hi" && nx affected --target=restore --all',
19+
expected: 'echo "hi" && nx run-many --target=restore --all',
20+
},
21+
{
22+
test: 'nx restore --all',
23+
expected: 'nx restore --all',
24+
},
25+
].forEach(testEntry => {
26+
it(`for: "${testEntry.test}"`, async () => {
27+
const tree = createEmptyWorkspace(Tree.empty());
28+
tree.overwrite(
29+
'/package.json',
30+
JSON.stringify({
31+
scripts: {
32+
postinstall: testEntry.test,
33+
},
34+
}),
35+
);
36+
37+
const result = await migrationTestRunner
38+
.runSchematicAsync('update-restore-postinstall', {}, tree)
39+
.toPromise();
40+
41+
const packageJson = JSON.parse(result.read('/package.json').toString());
42+
expect(packageJson.scripts.postinstall).toEqual(testEntry.expected);
43+
});
44+
});
45+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { chain, SchematicContext, Tree } from '@angular-devkit/schematics';
2+
import { updateJsonInTree } from '@nrwl/workspace';
3+
import { formatFiles } from '@nrwl/workspace';
4+
5+
const udpateRestorePostinstallScript = (
6+
host: Tree,
7+
context: SchematicContext,
8+
) => {
9+
updateJsonInTree('package.json', json => {
10+
if (
11+
json.scripts &&
12+
json.scripts.postinstall &&
13+
json.scripts.postinstall.includes('nx affected --target=restore --all')
14+
) {
15+
json.scripts.postinstall = json.scripts.postinstall.replace(
16+
/(.*)(nx affected --target=restore --all)(.*)/,
17+
'$1nx run-many --target=restore --all$3',
18+
);
19+
}
20+
return json;
21+
})(host, context);
22+
};
23+
24+
export default function () {
25+
return chain([udpateRestorePostinstallScript, formatFiles()]);
26+
}

workspace.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
"input": "./packages/nx-dotnet-core",
4545
"glob": "executors.json",
4646
"output": "."
47+
},
48+
{
49+
"input": "./packages/nx-dotnet-core",
50+
"glob": "migrations.json",
51+
"output": "."
4752
}
4853
]
4954
}

0 commit comments

Comments
 (0)