Skip to content

Commit

Permalink
fix(angular): respect skipPackageJson correctly across generators
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Apr 11, 2024
1 parent c8f179d commit 99ad67b
Show file tree
Hide file tree
Showing 24 changed files with 367 additions and 211 deletions.
6 changes: 6 additions & 0 deletions docs/generated/packages/angular/generators/setup-ssr.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
"type": "boolean",
"description": "Skip formatting the workspace after the generator completes.",
"x-priority": "internal"
},
"skipPackageJson": {
"type": "boolean",
"default": false,
"description": "Do not add dependencies to `package.json`.",
"x-priority": "internal"
}
},
"required": ["project"],
Expand Down
23 changes: 23 additions & 0 deletions packages/angular/src/generators/add-linting/add-linting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Tree,
addProjectConfiguration,
readJson,
updateJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import * as linter from '@nx/eslint';
Expand Down Expand Up @@ -64,4 +65,26 @@ describe('addLinting generator', () => {
const eslintConfig = readJson(tree, `${appProjectRoot}/.eslintrc.json`);
expect(eslintConfig).toMatchSnapshot();
});

it('should not touch the package.json when run with `--skipPackageJson`', async () => {
let initialPackageJson;
updateJson(tree, 'package.json', (json) => {
json.dependencies = {};
json.devDependencies = {};
initialPackageJson = json;

return json;
});

await addLintingGenerator(tree, {
prefix: 'myOrg',
projectName: appProjectName,
projectRoot: appProjectRoot,
skipFormat: true,
skipPackageJson: true,
});

const packageJson = readJson(tree, 'package.json');
expect(packageJson).toEqual(initialPackageJson);
});
});
1 change: 1 addition & 0 deletions packages/angular/src/generators/add-linting/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function addLintingGenerator(
rootProject: rootProject,
addPlugin: false,
addExplicitTargets: true,
skipPackageJson: options.skipPackageJson,
});
tasks.push(lintTask);

Expand Down
65 changes: 21 additions & 44 deletions packages/angular/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ describe('app', () => {
expect(tsConfig).toMatchSnapshot();
});

it('should not touch the package.json when run with `--skipPackageJson`', async () => {
let initialPackageJson;
updateJson(appTree, 'package.json', (json) => {
json.dependencies = {};
json.devDependencies = {};
initialPackageJson = json;

return json;
});

await generateApp(appTree, 'my-app', { skipPackageJson: true });

const packageJson = readJson(appTree, 'package.json');
expect(packageJson).toEqual(initialPackageJson);
});

describe('not nested', () => {
it('should create project configs', async () => {
// ACT
Expand Down Expand Up @@ -1182,58 +1198,19 @@ describe('app', () => {
}));
});

it('should add angular dependencies', async () => {
// ACT
it('should add angular peer dependencies when not installed', async () => {
await generateApp(appTree, 'my-app');

// ASSERT
const { dependencies, devDependencies } = readJson(
appTree,
'package.json'
);

expect(dependencies['@angular/animations']).toEqual(
backwardCompatibleVersions.angularV15.angularVersion
);
expect(dependencies['@angular/common']).toEqual(
backwardCompatibleVersions.angularV15.angularVersion
);
expect(dependencies['@angular/compiler']).toEqual(
backwardCompatibleVersions.angularV15.angularVersion
);
expect(dependencies['@angular/core']).toEqual(
backwardCompatibleVersions.angularV15.angularVersion
);
expect(dependencies['@angular/platform-browser']).toEqual(
backwardCompatibleVersions.angularV15.angularVersion
);
expect(dependencies['@angular/platform-browser-dynamic']).toEqual(
backwardCompatibleVersions.angularV15.angularVersion
);
expect(dependencies['@angular/router']).toEqual(
backwardCompatibleVersions.angularV15.angularVersion
);
expect(dependencies['rxjs']).toEqual(
backwardCompatibleVersions.angularV15.rxjsVersion
);
expect(dependencies['zone.js']).toEqual(
backwardCompatibleVersions.angularV15.zoneJsVersion
);
expect(devDependencies['@angular/cli']).toEqual(
const { devDependencies } = readJson(appTree, 'package.json');
expect(devDependencies['@angular-devkit/build-angular']).toEqual(
backwardCompatibleVersions.angularV15.angularDevkitVersion
);
expect(devDependencies['@angular/compiler-cli']).toEqual(
expect(devDependencies['@angular-devkit/schematics']).toEqual(
backwardCompatibleVersions.angularV15.angularDevkitVersion
);
expect(devDependencies['@angular/language-service']).toEqual(
backwardCompatibleVersions.angularV15.angularVersion
);
expect(devDependencies['@angular-devkit/build-angular']).toEqual(
expect(devDependencies['@schematics/angular']).toEqual(
backwardCompatibleVersions.angularV15.angularDevkitVersion
);

// codelyzer should no longer be there by default
expect(devDependencies['codelyzer']).toBeUndefined();
});

it('should import "ApplicationConfig" from "@angular/platform-browser"', async () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/angular/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export async function applicationGeneratorInternal(
...options,
skipFormat: true,
});
ensureAngularDependencies(tree);

if (!options.skipPackageJson) {
ensureAngularDependencies(tree);
}

createProject(tree, options);

Expand Down Expand Up @@ -95,6 +98,7 @@ export async function applicationGeneratorInternal(
await setupSsr(tree, {
project: options.name,
standalone: options.standalone,
skipPackageJson: options.skipPackageJson,
});
}

Expand Down
4 changes: 3 additions & 1 deletion packages/angular/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ function addFileServerTarget(
options: NormalizedSchema,
targetName: string
) {
addDependenciesToPackageJson(tree, {}, { '@nx/web': nxVersion });
if (!options.skipPackageJson) {
addDependenciesToPackageJson(tree, {}, { '@nx/web': nxVersion });
}

const { major: angularMajorVersion } = getInstalledAngularVersionInfo(tree);
const isUsingApplicationBuilder =
Expand Down
24 changes: 23 additions & 1 deletion packages/angular/src/generators/host/host.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateJson } from '@nx/devkit';
import { readJson, updateJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import {
getProjects,
Expand Down Expand Up @@ -608,4 +608,26 @@ describe('Host App Generator', () => {
).toContain(`'remote1','foo-remote2','foo-remote3'`);
});
});

it('should not touch the package.json when run with `--skipPackageJson`', async () => {
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
let initialPackageJson;
updateJson(tree, 'package.json', (json) => {
json.dependencies = {};
json.devDependencies = {};
initialPackageJson = json;

return json;
});

await generateTestHostApplication(tree, {
name: 'test',
ssr: true,
skipFormat: true,
skipPackageJson: true,
});

const packageJson = readJson(tree, 'package.json');
expect(packageJson).toEqual(initialPackageJson);
});
});
24 changes: 13 additions & 11 deletions packages/angular/src/generators/host/lib/update-ssr-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,18 @@ export async function updateSsrSetup(

updateProjectConfiguration(tree, appName, project);

const installTask = addDependenciesToPackageJson(
tree,
{
cors: corsVersion,
'@module-federation/node': moduleFederationNodeVersion,
},
{
'@types/cors': typesCorsVersion,
}
);
if (!options.skipPackageJson) {
return addDependenciesToPackageJson(
tree,
{
cors: corsVersion,
'@module-federation/node': moduleFederationNodeVersion,
},
{
'@types/cors': typesCorsVersion,
}
);
}

return installTask;
return () => {};
}
16 changes: 16 additions & 0 deletions packages/angular/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ describe('lib', () => {
expect(devDependencies['codelyzer']).toBeUndefined();
});

it('should not touch the package.json when run with `--skipPackageJson`', async () => {
let initialPackageJson;
updateJson(tree, 'package.json', (json) => {
json.dependencies = {};
json.devDependencies = {};
initialPackageJson = json;

return json;
});

await runLibraryGeneratorWithOpts({ skipPackageJson: true });

const packageJson = readJson(tree, 'package.json');
expect(packageJson).toEqual(initialPackageJson);
});

describe('not nested', () => {
it('should update ng-package.json', async () => {
// ACT
Expand Down
22 changes: 14 additions & 8 deletions packages/angular/src/generators/library/library.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
addDependenciesToPackageJson,
formatFiles,
GeneratorCallback,
installPackagesTask,
Expand All @@ -10,10 +11,7 @@ import { addTsConfigPath, initGenerator as jsInitGenerator } from '@nx/js';
import init from '../../generators/init/init';
import addLintingGenerator from '../add-linting/add-linting';
import setupTailwindGenerator from '../setup-tailwind/setup-tailwind';
import {
addDependenciesToPackageJsonIfDontExist,
versions,
} from '../utils/version-utils';
import { versions } from '../utils/version-utils';
import { addBuildableLibrariesPostCssDependencies } from '../utils/dependencies';
import { addModule } from './lib/add-module';
import { addStandaloneComponent } from './lib/add-standalone-component';
Expand Down Expand Up @@ -77,7 +75,10 @@ export async function libraryGeneratorInternal(
skipFormat: true,
});
await init(tree, { ...libraryOptions, skipFormat: true });
ensureAngularDependencies(tree);

if (!libraryOptions.skipPackageJson) {
ensureAngularDependencies(tree);
}

const project = addProject(tree, libraryOptions);

Expand All @@ -104,13 +105,18 @@ export async function libraryGeneratorInternal(
});
}

if (libraryOptions.buildable || libraryOptions.publishable) {
addDependenciesToPackageJsonIfDontExist(
if (
(libraryOptions.buildable || libraryOptions.publishable) &&
!libraryOptions.skipPackageJson
) {
addDependenciesToPackageJson(
tree,
{},
{
'ng-packagr': pkgVersions.ngPackagrVersion,
}
},
undefined,
true
);
addBuildableLibrariesPostCssDependencies(tree);
}
Expand Down
26 changes: 15 additions & 11 deletions packages/angular/src/generators/remote/lib/update-ssr-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export async function updateSsrSetup(
port,
standalone,
typescriptConfiguration,
skipPackageJson,
}: {
appName: string;
port: number;
standalone: boolean;
typescriptConfiguration: boolean;
skipPackageJson?: boolean;
}
) {
let project = readProjectConfiguration(tree, appName);
Expand Down Expand Up @@ -116,16 +118,18 @@ export async function updateSsrSetup(

updateProjectConfiguration(tree, appName, project);

const installTask = addDependenciesToPackageJson(
tree,
{
cors: corsVersion,
'@module-federation/node': moduleFederationNodeVersion,
},
{
'@types/cors': typesCorsVersion,
}
);
if (!skipPackageJson) {
return addDependenciesToPackageJson(
tree,
{
cors: corsVersion,
'@module-federation/node': moduleFederationNodeVersion,
},
{
'@types/cors': typesCorsVersion,
}
);
}

return installTask;
return () => {};
}
23 changes: 23 additions & 0 deletions packages/angular/src/generators/remote/remote.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,27 @@ describe('MF Remote App Generator', () => {
);
});
});

it('should not touch the package.json when run with `--skipPackageJson`', async () => {
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
let initialPackageJson;
updateJson(tree, 'package.json', (json) => {
json.dependencies = {};
json.devDependencies = {};
initialPackageJson = json;

return json;
});

await generateTestRemoteApplication(tree, {
name: 'test',
port: 4201,
ssr: true,
skipFormat: true,
skipPackageJson: true,
});

const packageJson = readJson(tree, 'package.json');
expect(packageJson).toEqual(initialPackageJson);
});
});
Loading

0 comments on commit 99ad67b

Please sign in to comment.