diff --git a/packages/core/src/generators/init/generator.spec.ts b/packages/core/src/generators/init/generator.spec.ts index 4e677546..c8c24d1d 100644 --- a/packages/core/src/generators/init/generator.spec.ts +++ b/packages/core/src/generators/init/generator.spec.ts @@ -1,4 +1,4 @@ -import { readJson, Tree } from '@nrwl/devkit'; +import { readJson, Tree, writeJson } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { DotNetClient, mockDotnetFactory } from '@nx-dotnet/dotnet'; @@ -13,6 +13,9 @@ describe('init generator', () => { beforeEach(() => { appTree = createTreeWithEmptyWorkspace(); dotnetClient = new DotNetClient(mockDotnetFactory()); + + const packageJson = { scripts: {} }; + writeJson(appTree, 'package.json', packageJson); }); it('should create config', async () => { @@ -46,4 +49,20 @@ describe('init generator', () => { await generator(appTree, dotnetClient); expect(spy).not.toHaveBeenCalled(); }); + + it('should add restore to prepare script', async () => { + await generator(appTree, dotnetClient); + const updated = readJson(appTree, 'package.json'); + expect(updated.scripts.prepare).toBe('nx g @nx-dotnet/core:restore'); + }); + + it('should not add restore if it already exists', async () => { + const packageJson = { + scripts: { prepare: 'nx g @nx-dotnet/core:restore' }, + }; + writeJson(appTree, 'package.json', packageJson); + await generator(appTree, dotnetClient); + const updated = readJson(appTree, 'package.json'); + expect(updated.scripts.prepare).toBe('nx g @nx-dotnet/core:restore'); + }); }); diff --git a/packages/core/src/generators/init/generator.ts b/packages/core/src/generators/init/generator.ts index 9681d654..921ad382 100644 --- a/packages/core/src/generators/init/generator.ts +++ b/packages/core/src/generators/init/generator.ts @@ -3,6 +3,7 @@ import { readJson, readWorkspaceConfiguration, Tree, + updateWorkspaceConfiguration, WorkspaceConfiguration, writeJson, } from '@nrwl/devkit'; @@ -32,6 +33,7 @@ export default async function ( } initToolManifest(host, dotnetClient); + addPrepareScript(host); } function updateGitIgnore( @@ -73,3 +75,17 @@ function initToolManifest(host: Tree, dotnetClient: DotNetClient) { dotnetClient.new('tool-manifest'); } } + +function addPrepareScript(host: Tree) { + const packageJson = readJson(host, 'package.json'); + const prepareSteps: string[] = + packageJson.scripts.prepare?.split('&&').map((x: string) => x.trim()) ?? []; + + const restoreScript = 'nx g @nx-dotnet/core:restore'; + if (!prepareSteps.includes(restoreScript)) { + prepareSteps.push(restoreScript); + } + + packageJson.scripts.prepare = prepareSteps.join(' && '); + writeJson(host, 'package.json', packageJson); +} diff --git a/packages/core/src/generators/utils/generate-project.spec.ts b/packages/core/src/generators/utils/generate-project.spec.ts index e28566d0..b358d795 100644 --- a/packages/core/src/generators/utils/generate-project.spec.ts +++ b/packages/core/src/generators/utils/generate-project.spec.ts @@ -1,4 +1,4 @@ -import { readProjectConfiguration, Tree } from '@nrwl/devkit'; +import { readProjectConfiguration, Tree, writeJson } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { readFileSync } from 'fs'; @@ -30,6 +30,9 @@ describe('nx-dotnet project generator', () => { beforeEach(() => { appTree = createTreeWithEmptyWorkspace(); dotnetClient = new DotNetClient(mockDotnetFactory()); + + const packageJson = { scripts: {} }; + writeJson(appTree, 'package.json', packageJson); }); afterEach(async () => {