angular / angular/angular-cli

`externalSchematics` should install packages locally when they are no present in the `node_modules`

オープン
#19,301 コメント 13 件 リアクション 5 件 担当者 0 名 GitHub で見る
area: @angular-devkit/schematics feature feature: insufficient votes
主要言語
TypeScript
スター
27k
フォーク
11.8k
平均マージ
14時間 23分
マージ済み PR(30日)
162

説明

# 🚀 Feature request

### Command (mark with an `x`)

- [ ] new
- [ ] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] extract-i18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc
- [x] other

### Description

when creating own schematics, they can make use of other schematics by calling the `externalSchematic` function provided by the `@angular-devkit/schematics` collection.

To call an external schematic, the function can be used like this:

```ts
options = {};
externalSchematic('my-schematic-package', 'my-schematic-name', options);
```

But to execute an external schematic package it has to be part of the `node_modules`. Internally the CLI uses the function for example [for calling the PWA schematic](https://github.com/angular/angular-cli/blob/b2174947924a781f7db41377be989750e042a968/packages/angular/pwa/pwa/index.ts#L159). This won't be problematic as is already installed as part of the `@angular/schematics` package and in fact it's part of the `node_modules`.

Once you'll try to install an external schematic which isn't already available in the `node_modules` the schematic fails.

A workaround is to manually integrate an installation procedure first and then call the function like this:

```ts
export const installNpmPackage = (
context: SchematicContext,
packageName: string,
): Promise => {
return new Promise((resolve) => {
context.logger.info(
`📦 Installing package '${packageName}' for external schematic setup...`,
);
const spawnOptions: SpawnOptions = { stdio: 'inherit' };
spawn('npm', ['install', packageName], spawnOptions).on(
'close',
(code: number) => {
if (code === 0) {
context.logger.info(
`✅ '${packageName}' package installed successfully`,
);
resolve();
} else {
const errorMessage = `❌ installation of '${packageName}' package failed`;
context.logger.error(errorMessage);
throw new Error();
}
},
);
});
};

const packageName = '@briebug/cypress-schematic';
await installNpmPackage(context, packageName);
return externalSchematic(packageName, 'ng-add', {
removeProtractor: true,
addCypressTestScripts: true,
});
```

### Describe the solution you'd like

It would be great to integrate the installation process for a package that's not present in the `node_modules` in the `externalSchematic` function.

### Describe alternatives you've considered

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

@angular-devkit/schematics の externalSchematic 関数と、参照されている packages/angular/pwa/pwa/index.ts の呼び出しから始めます。外部パッケージが node_modules に存在しない場合にどのように解決されるかを追跡し、その後、インストールをそのフローにどのように統合すべきかを判断します。利用できない外部 schematic パッケージがインストールされ、schematic が正常に実行されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
angular, typescript
領域
cli
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。