Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.

Commit c2b1c23

Browse files
committed
feat(core): add restore to prepare script during init
Add restore to prepare script so new installs fully set up the workspace Fixes #44
1 parent 5908947 commit c2b1c23

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

packages/core/src/generators/init/generator.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readJson, Tree } from '@nrwl/devkit';
1+
import { readJson, Tree, writeJson } from '@nrwl/devkit';
22
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
33
import { DotNetClient, mockDotnetFactory } from '@nx-dotnet/dotnet';
44

@@ -13,6 +13,9 @@ describe('init generator', () => {
1313
beforeEach(() => {
1414
appTree = createTreeWithEmptyWorkspace();
1515
dotnetClient = new DotNetClient(mockDotnetFactory());
16+
17+
const packageJson = { scripts: {} };
18+
writeJson(appTree, 'package.json', packageJson);
1619
});
1720

1821
it('should create config', async () => {
@@ -46,4 +49,20 @@ describe('init generator', () => {
4649
await generator(appTree, dotnetClient);
4750
expect(spy).not.toHaveBeenCalled();
4851
});
52+
53+
it('should add restore to prepare script', async () => {
54+
await generator(appTree, dotnetClient);
55+
const updated = readJson(appTree, 'package.json');
56+
expect(updated.scripts.prepare).toBe('nx g @nx-dotnet/core:restore');
57+
});
58+
59+
it('should not add restore if it already exists', async () => {
60+
const packageJson = {
61+
scripts: { prepare: 'nx g @nx-dotnet/core:restore' },
62+
};
63+
writeJson(appTree, 'package.json', packageJson);
64+
await generator(appTree, dotnetClient);
65+
const updated = readJson(appTree, 'package.json');
66+
expect(updated.scripts.prepare).toBe('nx g @nx-dotnet/core:restore');
67+
});
4968
});

packages/core/src/generators/init/generator.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
readJson,
44
readWorkspaceConfiguration,
55
Tree,
6+
updateWorkspaceConfiguration,
67
WorkspaceConfiguration,
78
writeJson,
89
} from '@nrwl/devkit';
@@ -32,6 +33,7 @@ export default async function (
3233
}
3334

3435
initToolManifest(host, dotnetClient);
36+
addPrepareScript(host);
3537
}
3638

3739
function updateGitIgnore(
@@ -73,3 +75,17 @@ function initToolManifest(host: Tree, dotnetClient: DotNetClient) {
7375
dotnetClient.new('tool-manifest');
7476
}
7577
}
78+
79+
function addPrepareScript(host: Tree) {
80+
const packageJson = readJson(host, 'package.json');
81+
const prepareSteps: string[] =
82+
packageJson.scripts.prepare?.split('&&').map((x: string) => x.trim()) ?? [];
83+
84+
const restoreScript = 'nx g @nx-dotnet/core:restore';
85+
if (!prepareSteps.includes(restoreScript)) {
86+
prepareSteps.push(restoreScript);
87+
}
88+
89+
packageJson.scripts.prepare = prepareSteps.join(' && ');
90+
writeJson(host, 'package.json', packageJson);
91+
}

packages/core/src/generators/utils/generate-project.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readProjectConfiguration, Tree } from '@nrwl/devkit';
1+
import { readProjectConfiguration, Tree, writeJson } from '@nrwl/devkit';
22
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
33

44
import { readFileSync } from 'fs';
@@ -30,6 +30,9 @@ describe('nx-dotnet project generator', () => {
3030
beforeEach(() => {
3131
appTree = createTreeWithEmptyWorkspace();
3232
dotnetClient = new DotNetClient(mockDotnetFactory());
33+
34+
const packageJson = { scripts: {} };
35+
writeJson(appTree, 'package.json', packageJson);
3336
});
3437

3538
afterEach(async () => {

0 commit comments

Comments
 (0)