Skip to content

Commit

Permalink
fix(core): new generator not skipping package installation (nrwl#17927)
Browse files Browse the repository at this point in the history
  • Loading branch information
skrtheboss authored Jan 30, 2024
1 parent deb0f1a commit abafedf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,34 @@ exports[`new should generate an empty nx.json 1`] = `
},
}
`;

exports[`new should skip install 1`] = `
{
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"namedInputs": {
"default": [
"{projectRoot}/**/*",
"sharedGlobals",
],
"production": [
"default",
],
"sharedGlobals": [],
},
"targetDefaults": {
"build": {
"cache": true,
"dependsOn": [
"^build",
],
"inputs": [
"production",
"^production",
],
},
"lint": {
"cache": true,
},
},
}
`;
61 changes: 49 additions & 12 deletions packages/workspace/src/generators/new/new.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { readJson, Tree, writeJson } from '@nx/devkit';
import * as devkit from '@nx/devkit';
import { createTree } from '@nx/devkit/testing';
import { Linter } from '../../utils/lint';
import {
Expand All @@ -8,14 +9,10 @@ import {
} from '../../utils/versions';
import { Preset } from '../utils/presets';
import { newGenerator, NormalizedSchema } from './new';
import * as getNpmPackageVersion from './../utils/get-npm-package-version';
import * as generatePreset from './generate-preset';

const DEFAULT_PACKAGE_VERSION = '1.0.0';
jest.mock('./../utils/get-npm-package-version', () => ({
...jest.requireActual<any>('./../utils/get-npm-package-version'),
getNpmPackageVersion: jest
.fn()
.mockImplementation((name, version) => version ?? DEFAULT_PACKAGE_VERSION),
}));

const defaultOptions: Omit<
NormalizedSchema,
Expand All @@ -29,21 +26,61 @@ const defaultOptions: Omit<

describe('new', () => {
let tree: Tree;
let installPackagesTaskSpy: jest.SpyInstance;
let generatePresetSpy: jest.SpyInstance;
let getNpmPackageVersionSpy: jest.SpyInstance;

beforeEach(() => {
tree = createTree();
// we need an actual path for the package manager version check
tree.root = process.cwd();

installPackagesTaskSpy = jest
.spyOn(devkit, 'installPackagesTask')
.mockImplementation(() => undefined);

generatePresetSpy = jest
.spyOn(generatePreset, 'generatePreset')
.mockImplementation(async () => undefined);

getNpmPackageVersionSpy = jest
.spyOn(getNpmPackageVersion, 'getNpmPackageVersion')
.mockImplementation(
(name, version) => version ?? DEFAULT_PACKAGE_VERSION
);
});

afterEach(() => {
jest.resetAllMocks();
});

it('should generate an empty nx.json', async () => {
await newGenerator(tree, {
...defaultOptions,
name: 'my-workspace',
directory: 'my-workspace',
appName: 'app',
});
await (
await newGenerator(tree, {
...defaultOptions,
name: 'my-workspace',
directory: 'my-workspace',
appName: 'app',
})
)();
expect(readJson(tree, 'my-workspace/nx.json')).toMatchSnapshot();
expect(installPackagesTaskSpy).toHaveBeenCalled();
expect(generatePresetSpy).toHaveBeenCalled();
});

it('should skip install', async () => {
await (
await newGenerator(tree, {
...defaultOptions,
name: 'my-workspace',
directory: 'my-workspace',
appName: 'app',
skipInstall: true,
})
)();
expect(readJson(tree, 'my-workspace/nx.json')).toMatchSnapshot();
expect(installPackagesTaskSpy).not.toHaveBeenCalled();
expect(generatePresetSpy).toHaveBeenCalled();
});

describe('--preset', () => {
Expand Down
22 changes: 15 additions & 7 deletions packages/workspace/src/generators/new/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,22 @@ export async function newGenerator(tree: Tree, opts: Schema) {
addPresetDependencies(tree, options);

return async () => {
const pmc = getPackageManagerCommand(options.packageManager);
if (pmc.preInstall) {
execSync(pmc.preInstall, {
cwd: joinPathFragments(tree.root, options.directory),
stdio: process.env.NX_GENERATE_QUIET === 'true' ? 'ignore' : 'inherit',
});
if (!options.skipInstall) {
const pmc = getPackageManagerCommand(options.packageManager);
if (pmc.preInstall) {
execSync(pmc.preInstall, {
cwd: joinPathFragments(tree.root, options.directory),
stdio:
process.env.NX_GENERATE_QUIET === 'true' ? 'ignore' : 'inherit',
});
}
installPackagesTask(
tree,
false,
options.directory,
options.packageManager
);
}
installPackagesTask(tree, false, options.directory, options.packageManager);
// TODO: move all of these into create-nx-workspace
if (options.preset !== Preset.NPM && !options.isCustomPreset) {
await generatePreset(tree, options);
Expand Down

0 comments on commit abafedf

Please sign in to comment.