Skip to content

Commit 1fc0c73

Browse files
committed
feat(@schematics/angular): enable npm engine-strict in new workspaces
npm only warns when a dependency's `engines` doesn't match the installed Node.js version, while Yarn and pnpm fail. Generate an `.npmrc` with `engine-strict=true` for new workspaces that use npm, so installs with an unsupported Node.js version fail instead of printing an easy-to-miss warning. Closes #31271
1 parent dc65268 commit 1fc0c73

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true

‎packages/schematics/angular/workspace/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export default function (options: WorkspaceOptions): Rule {
4848
return mergeWith(
4949
apply(url('./files'), [
5050
options.minimal ? filter((path) => !path.endsWith('editorconfig.template')) : noop(),
51+
// Unlike other package managers, npm only warns about unsupported `engines` by default.
52+
packageManager !== 'npm' ? filter((path) => !path.endsWith('npmrc.template')) : noop(),
5153
applyTemplates({
5254
utils: strings,
5355
...options,

‎packages/schematics/angular/workspace/index_spec.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ describe('Workspace Schematic', () => {
4040
);
4141
});
4242

43+
it('should enable npm `engine-strict` in .npmrc when using npm', async () => {
44+
const tree = await schematicRunner.runSchematic('workspace', {
45+
...defaultOptions,
46+
packageManager: 'npm',
47+
});
48+
expect(tree.readContent('/.npmrc')).toBe('engine-strict=true\n');
49+
});
50+
51+
for (const packageManager of ['yarn', 'pnpm', 'bun', undefined] as const) {
52+
it(`should not create an .npmrc when the package manager is ${packageManager}`, async () => {
53+
const tree = await schematicRunner.runSchematic('workspace', {
54+
...defaultOptions,
55+
packageManager,
56+
});
57+
expect(tree.exists('/.npmrc')).toBeFalse();
58+
});
59+
}
60+
4361
it('should set the name in package.json', async () => {
4462
const tree = await schematicRunner.runSchematic('workspace', defaultOptions);
4563
const pkg = JSON.parse(tree.readContent('/package.json'));

0 commit comments

Comments
 (0)