From 37693c40e3afc4c6dd7c949ea658bdf94146c9d8 Mon Sep 17 00:00:00 2001 From: sidhanshamil Date: Mon, 12 Aug 2024 22:29:51 +0530 Subject: [PATCH] feat(@angular-devkit/schematics-cli): add package manager option to blank schematic --- .../angular_devkit/schematics_cli/bin/schematics.ts | 4 +++- .../angular_devkit/schematics_cli/blank/factory.ts | 7 ++++++- .../angular_devkit/schematics_cli/blank/schema.json | 6 ++++++ .../schematics_cli/schematic/factory.ts | 7 ++++++- .../schematics_cli/schematic/schema.json | 6 ++++++ tests/legacy-cli/e2e/tests/schematics_cli/basic.ts | 11 +++++++---- .../legacy-cli/e2e/tests/schematics_cli/blank-test.ts | 11 ++++++----- .../e2e/tests/schematics_cli/schematic-test.ts | 11 ++++++----- 8 files changed, 46 insertions(+), 17 deletions(-) diff --git a/packages/angular_devkit/schematics_cli/bin/schematics.ts b/packages/angular_devkit/schematics_cli/bin/schematics.ts index 02272044c568..5dab4733c13f 100644 --- a/packages/angular_devkit/schematics_cli/bin/schematics.ts +++ b/packages/angular_devkit/schematics_cli/bin/schematics.ts @@ -9,7 +9,7 @@ // symbol polyfill must go first import 'symbol-observable'; -import type { JsonValue, logging, schema } from '@angular-devkit/core'; +import { JsonValue, logging, schema } from '@angular-devkit/core'; import { ProcessOutput, createConsoleLogger } from '@angular-devkit/core/node'; import { UnsuccessfulWorkflowExecution } from '@angular-devkit/schematics'; import { NodeWorkflow } from '@angular-devkit/schematics/tools'; @@ -338,6 +338,8 @@ export async function main({ } }); + workflow.registry.addPostTransform(schema.transforms.addUndefinedDefaults); + // Show usage of deprecated options workflow.registry.useXDeprecatedProvider((msg) => logger.warn(msg)); diff --git a/packages/angular_devkit/schematics_cli/blank/factory.ts b/packages/angular_devkit/schematics_cli/blank/factory.ts index 812112bcc716..09f2908a55c3 100644 --- a/packages/angular_devkit/schematics_cli/blank/factory.ts +++ b/packages/angular_devkit/schematics_cli/blank/factory.ts @@ -90,7 +90,12 @@ export default function (options: Schema): Rule { move(options.name), ]); - context.addTask(new NodePackageInstallTask(options.name)); + context.addTask( + new NodePackageInstallTask({ + workingDirectory: options.name, + packageManager: options.packageManager, + }), + ); } return chain([ diff --git a/packages/angular_devkit/schematics_cli/blank/schema.json b/packages/angular_devkit/schematics_cli/blank/schema.json index 1fb10610a70d..9b4ba24fd945 100644 --- a/packages/angular_devkit/schematics_cli/blank/schema.json +++ b/packages/angular_devkit/schematics_cli/blank/schema.json @@ -12,6 +12,12 @@ "index": 0 } }, + "packageManager": { + "description": "The package manager used to install dependencies.", + "type": "string", + "enum": ["npm", "yarn", "pnpm", "cnpm", "bun"], + "default": "npm" + }, "author": { "type": "string", "description": "Author for the new schematic." diff --git a/packages/angular_devkit/schematics_cli/schematic/factory.ts b/packages/angular_devkit/schematics_cli/schematic/factory.ts index 39a1ffecb54b..c8cc96e8e225 100644 --- a/packages/angular_devkit/schematics_cli/schematic/factory.ts +++ b/packages/angular_devkit/schematics_cli/schematic/factory.ts @@ -24,7 +24,12 @@ export default function (options: Schema): Rule { const coreVersion = require('@angular-devkit/core/package.json').version; return (_, context) => { - context.addTask(new NodePackageInstallTask(options.name)); + context.addTask( + new NodePackageInstallTask({ + workingDirectory: options.name, + packageManager: options.packageManager, + }), + ); return mergeWith( apply(url('./files'), [ diff --git a/packages/angular_devkit/schematics_cli/schematic/schema.json b/packages/angular_devkit/schematics_cli/schematic/schema.json index 2169f0227799..85aed7f7eba6 100644 --- a/packages/angular_devkit/schematics_cli/schematic/schema.json +++ b/packages/angular_devkit/schematics_cli/schematic/schema.json @@ -11,6 +11,12 @@ "author": { "type": "string", "description": "Author for the new schematic." + }, + "packageManager": { + "description": "The package manager used to install dependencies.", + "type": "string", + "enum": ["npm", "yarn", "pnpm", "cnpm", "bun"], + "default": "npm" } }, "required": ["name"] diff --git a/tests/legacy-cli/e2e/tests/schematics_cli/basic.ts b/tests/legacy-cli/e2e/tests/schematics_cli/basic.ts index da65b17ef02c..62128e1e32db 100644 --- a/tests/legacy-cli/e2e/tests/schematics_cli/basic.ts +++ b/tests/legacy-cli/e2e/tests/schematics_cli/basic.ts @@ -1,4 +1,4 @@ -import * as path from 'path'; +import { join } from 'node:path'; import { getGlobalVariable } from '../../utils/env'; import { exec, execAndWaitForOutputToMatch, silentNpm } from '../../utils/process'; import { rimraf } from '../../utils/fs'; @@ -14,13 +14,13 @@ export default async function () { await exec(process.platform.startsWith('win') ? 'where' : 'which', 'schematics'); const startCwd = process.cwd(); - const schematicPath = path.join(startCwd, 'test-schematic'); + const schematicPath = join(startCwd, 'test-schematic'); try { // create blank schematic await exec('schematics', 'schematic', '--name', 'test-schematic'); - process.chdir(path.join(startCwd, 'test-schematic')); + process.chdir(join(startCwd, 'test-schematic')); await execAndWaitForOutputToMatch( 'schematics', ['.:', '--list-schematics'], @@ -29,6 +29,9 @@ export default async function () { } finally { // restore path process.chdir(startCwd); - await rimraf(schematicPath); + await Promise.all([ + rimraf(schematicPath), + silentNpm('uninstall', '-g', '@angular-devkit/schematics-cli'), + ]); } } diff --git a/tests/legacy-cli/e2e/tests/schematics_cli/blank-test.ts b/tests/legacy-cli/e2e/tests/schematics_cli/blank-test.ts index 76b3cb67395e..4087f7476db9 100644 --- a/tests/legacy-cli/e2e/tests/schematics_cli/blank-test.ts +++ b/tests/legacy-cli/e2e/tests/schematics_cli/blank-test.ts @@ -1,5 +1,4 @@ -import * as fs from 'fs'; -import * as path from 'path'; +import { join } from 'node:path'; import { getGlobalVariable } from '../../utils/env'; import { exec, silentNpm } from '../../utils/process'; import { rimraf } from '../../utils/fs'; @@ -15,7 +14,7 @@ export default async function () { await exec(process.platform.startsWith('win') ? 'where' : 'which', 'schematics'); const startCwd = process.cwd(); - const schematicPath = path.join(startCwd, 'test-schematic'); + const schematicPath = join(startCwd, 'test-schematic'); try { // create schematic @@ -23,11 +22,13 @@ export default async function () { process.chdir(schematicPath); - await silentNpm('install'); await silentNpm('test'); } finally { // restore path process.chdir(startCwd); - await rimraf(schematicPath); + await Promise.all([ + rimraf(schematicPath), + silentNpm('uninstall', '-g', '@angular-devkit/schematics-cli'), + ]); } } diff --git a/tests/legacy-cli/e2e/tests/schematics_cli/schematic-test.ts b/tests/legacy-cli/e2e/tests/schematics_cli/schematic-test.ts index 0033f98cc96f..7d7d26e7304b 100644 --- a/tests/legacy-cli/e2e/tests/schematics_cli/schematic-test.ts +++ b/tests/legacy-cli/e2e/tests/schematics_cli/schematic-test.ts @@ -1,5 +1,4 @@ -import * as fs from 'fs'; -import * as path from 'path'; +import { join } from 'node:path'; import { getGlobalVariable } from '../../utils/env'; import { exec, silentNpm } from '../../utils/process'; import { rimraf } from '../../utils/fs'; @@ -15,7 +14,7 @@ export default async function () { await exec(process.platform.startsWith('win') ? 'where' : 'which', 'schematics'); const startCwd = process.cwd(); - const schematicPath = path.join(startCwd, 'test-schematic'); + const schematicPath = join(startCwd, 'test-schematic'); try { // create schematic @@ -23,11 +22,13 @@ export default async function () { process.chdir(schematicPath); - await silentNpm('install'); await silentNpm('test'); } finally { // restore path process.chdir(startCwd); - await rimraf(schematicPath); + await Promise.all([ + rimraf(schematicPath), + silentNpm('uninstall', '-g', '@angular-devkit/schematics-cli'), + ]); } }