Skip to content

Commit

Permalink
feat(core): add restore to prepare script during init
Browse files Browse the repository at this point in the history
Add restore to prepare script so new installs fully set up the workspace

Fixes #44
  • Loading branch information
Ben Callaghan committed May 18, 2021
1 parent 5908947 commit c2b1c23
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
21 changes: 20 additions & 1 deletion packages/core/src/generators/init/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 () => {
Expand Down Expand Up @@ -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');
});
});
16 changes: 16 additions & 0 deletions packages/core/src/generators/init/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
readJson,
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
WorkspaceConfiguration,
writeJson,
} from '@nrwl/devkit';
Expand Down Expand Up @@ -32,6 +33,7 @@ export default async function (
}

initToolManifest(host, dotnetClient);
addPrepareScript(host);
}

function updateGitIgnore(
Expand Down Expand Up @@ -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);
}
5 changes: 4 additions & 1 deletion packages/core/src/generators/utils/generate-project.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit c2b1c23

Please sign in to comment.