Skip to content

Commit

Permalink
fix(@angular/create): use appropriate package manager to install depe…
Browse files Browse the repository at this point in the history
…ndencies

Previously, NPM was always used to install dependencies even when using `yarn create` or `pnpm create`.

Closes #23617
  • Loading branch information
alan-agius4 committed Jul 22, 2022
1 parent e995bda commit fba90f1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
14 changes: 10 additions & 4 deletions packages/angular/create/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
# `@angular/create`

# Create an Angular CLI workspace
## Create an Angular CLI workspace

Scaffold an Angular CLI workspace without needing to install the Angular CLI globally. All of the [ng new](https://angular.io/cli/new) options and features are supported.

# Usage
## Usage

NPM
### npm

```
npm init @angular [project-name] -- [...options]
```

Yarn
### yarn

```
yarn create @angular [project-name] [...options]
```

### pnpm

```
pnpm create @angular [project-name] [...options]
```
23 changes: 22 additions & 1 deletion packages/angular/create/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,30 @@ import { spawnSync } from 'child_process';
import { join } from 'path';

const binPath = join(require.resolve('@angular/cli/package.json'), '../bin/ng.js');
const [, execPath, ...args] = process.argv;

const hasPackageManagerArg = args.some((a) => a.includes('--package-manager'));
if (!hasPackageManagerArg) {
const isNpm = /[\\/]_npx[/\\]/.test(execPath);
const isPnpm = /[\\/]pnpm[/\\]/.test(execPath);

let packageManager = 'npm';
if (isPnpm) {
packageManager = 'pnpm';
} else if (!isNpm && !isPnpm) {
packageManager = 'yarn';
}

args.push('--package-manager', packageManager);
if (isNpm && args.length) {
// NPM required `--` before flags.
const isFlag = args[0].charAt(0) === '-';
args.splice(isFlag ? 0 : 1, 0, '--');
}
}

// Invoke ng new with any parameters provided.
const { error } = spawnSync(process.execPath, [binPath, 'new', ...process.argv.slice(2)], {
const { error } = spawnSync(process.execPath, [binPath, 'new', ...args], {
stdio: 'inherit',
});

Expand Down
9 changes: 7 additions & 2 deletions tests/legacy-cli/e2e/tests/misc/create-angular.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join, resolve } from 'path';
import { expectFileToExist, rimraf } from '../../utils/fs';
import { expectFileToExist, readFile, rimraf } from '../../utils/fs';
import { getActivePackageManager } from '../../utils/packages';
import { silentNpm, silentYarn } from '../../utils/process';

Expand All @@ -26,7 +26,12 @@ export default async function () {
throw new Error(`This test is not configured to use ${packageManager}.`);
}

await expectFileToExist(join(projectName, 'angular.json'));
// Check that package manager has been configured based on the package manager used to invoke the create command.
const workspace = JSON.parse(await readFile(join(projectName, 'angular.json')));
if (workspace.cli?.packageManager !== packageManager) {
throw new Error(`Expected 'packageManager' option to be configured to ${packageManager}.`);
}

// Verify styles was create with correct extension.
await expectFileToExist(join(projectName, 'src/styles.scss'));
} finally {
Expand Down

0 comments on commit fba90f1

Please sign in to comment.