angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Rename / remove / move components

Open zpydee opened this issue 10 years ago • 102 comments

Please provide us with the following information:

  1. OS? Mac OSX (El Capitan)
  1. Versions. Please run ng --version. If there's nothing outputted, please angular-cli: 1.0.0-beta.1 node: 5.7.0 os: darwin x64

It would be really usefull to add cli commands for:

  • rm = removing a component
  • mv = moving / renaming a component

Unless I am missing it, I don't see that this exists and it would be really useful during refactoring to avoid all the manual updates that come with this process.

Thoughts?

zpydee avatar May 24 '16 07:05 zpydee

+1! I couldn't actually believe this isn't available yet! It would be a really nifty feature... renaming 4 files + a folder plus some references is pretty annoying! Considering writing a script for it myself.

spinspume avatar Jun 05 '19 15:06 spinspume

Would definitely be a very nice feature to have, make it possible!

Lagily avatar Jun 09 '19 11:06 Lagily

I'm waiting for it as well! :)

sovushka-utrom avatar Jun 11 '19 07:06 sovushka-utrom

Here is a bash script I wrote to remove components. It is run like ./removeComponent myComponentName

This has only been tested with Angular 6

#!/bin/bash
if [ "$#" -ne 1 ]; then
    echo "Input a component to delete"
    exit 1
fi

# finds folder with component name and deletes
find . -type d -name $1 | xargs rm -rf

# removes lines referencing the component from app.module.ts
grep -v $1 app.module.ts > temp
mv temp app.module.ts

componentName=$1
componentName+="Component"

grep -v -i $componentName app.module.ts > temp
mv temp app.module.ts

thegrims avatar Jun 12 '19 00:06 thegrims

ng add not only installs but also edit project files. For example, if you add @angular/pwa, it would deposits icons in src/assets/ directory and edits the root module.ts as well as put a

A remove should not only remode the package from node_modules but also under all these changes on project files.

You get a fair idea how difficult that is.

charlie39 avatar Jul 09 '19 23:07 charlie39

Hi guys! ✌️

We just wrote a tool to rename angular components. Check it out https://www.npmjs.com/package/ng-rn

Hope you like it 🙃

(node:7652) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded at NgRename.findNgProjectDir (C:\Users\Dell P1\AppData\Roaming\npm\node_modules\ng-rn\index.js:74:19) at NgRename.findNgProjectDir (C:\Users\Dell P1\AppData\Roaming\npm\node_modules\ng-rn\index.js:81:19) at NgRename.findNgProjectDir (C:\Users\Dell P1\AppData\Roaming\npm\node_modules\ng-rn\index.js:81:19) at NgRename.findNgProjectDir (C:\Users\Dell P1\AppData\Roaming\npm\node_modules\ng-rn\index.js:81:19) at NgRename.findNgProjectDir (C:\Users\Dell P1\AppData\Roaming\npm\node_modules\ng-rn\index.js:81:19) at NgRename.findNgProjectDir (C:\Users\Dell P1\AppData\Roaming\npm\node_modules\ng-rn\index.js:81:19) at NgRename.findNgProjectDir (C:\Users\Dell P1\AppData\Roaming\npm\node_modules\ng-rn\index.js:81:19) at NgRename.findNgProjectDir (C:\Users\Dell P1\AppData\Roaming\npm\node_modules\ng-rn\index.js:81:19) at NgRename.findNgProjectDir (C:\Users\Dell P1\AppData\Roaming\npm\node_modules\ng-rn\index.js:81:19) at NgRename.findNgProjectDir (C:\Users\Dell P1\AppData\Roaming\npm\node_modules\ng-rn\index.js:81:19) (node:7652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:7652) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

hamed5765 avatar Jul 29 '19 15:07 hamed5765

The issue must be there ; import { ModifierComponent } from './modifier.component'; //not found

describe('ModifierComponent', () => { let component: ModifierComponent; let fixture: ComponentFixture;

beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ ModifierComponent ] }) .compileComponents(); }));

beforeEach(() => { fixture = TestBed.createComponent(ModifierComponent); component = fixture.componentInstance; fixture.detectChanges(); });

it('should create', () => { expect(component).toBeTruthy(); }); });

hamed5765 avatar Jul 29 '19 15:07 hamed5765

+1

itsems avatar Sep 20 '19 07:09 itsems

+1

getsali avatar Sep 28 '19 10:09 getsali

+1 I just started using angular, coming from ember.js and that's literally the first thing I was searching for, on how to manipulate components using the cli. That's sad, I hope it will be added

zubeirom avatar Oct 16 '19 00:10 zubeirom

it has been almost 4 years and still no ng rename ? i'm considering writing a script myself ...

MA-Agent avatar Nov 13 '19 09:11 MA-Agent

Forilla my Gorilla? This isn't a command?

AWSchneider-dev avatar Jan 08 '20 01:01 AWSchneider-dev

@MA-Agent I see you mentionned considering to write a script yourself. Do you have any news on that ? I am pretty sure a few people here might be interested :eyes:

HunteRoi avatar Jan 08 '20 15:01 HunteRoi

It's pointless really am just gonna right that script myself since they're not considering this as a must need command...

SaboSuke avatar Jan 16 '20 13:01 SaboSuke

+1

xEt3 avatar May 11 '20 12:05 xEt3

It's pointless really am just gonna right that script myself since they're not considering this as a must need command...

It seems easy but it has a big scope. When you remove a component then you will have to visit all files to check if it's component use anywhere else or not. While we add a component we mention module same we will have to do while removing.

sacgrover avatar Jun 02 '20 15:06 sacgrover

And what will you do if the component is moved in the directory of another module. Maybe you have modules in nested directories, or maybe you use some constants in order to avoid copy/paste between the declarations and exports of the @NgModule(). I think that this is way out of scope.

What will you do with page components? What about dialog that are used at runtime, that used to be in entryComponents, like dialogs. And then, what do you do with CSS classes and related things?

Here's an old "recursive rename" script of mine, it only updates content in the directory that you give it, and that directory needs to contain a single component (and with extra related things like component specific services)

recrename.sh.txt


My feeling is that this story is very much out of the scope for the Angular CLI.

Each project has it's own unique structure and if you really want to. You can implement your own schematic or maybe a 3rd party library could implement some generic schematics.

But I think that it will be to much work for the Angular CLI team to maintain. There will be way to many issues reported regarding this feature. So: :-1:

SchnWalter avatar Jun 02 '20 16:06 SchnWalter

We discussed this issue within the CLI team today and had a few thoughts. This issue seems to cover multiple use cases, so to address them individually:

Delete component

There is not much the CLI can do in order to more intelligently delete an Angular component. We can't intelligently remove all references to a component, so it's up to the user to refactor their app such that a component isn't used. That is 90% of the meaningful effort around deleting a component, so manually deleting multiple files isn't consuming a critical amount of time.

A trivially simple command to do this would be:

rm foo.component.*
# OR
rm foo.service.*

The Angular CLI can't really be much more intelligent than this, so implementing it doesn't provide much value to users.

Move component

(In this context, I'm defining "moving" a component as moving it into a different directory with the same name, as opposed to "renaming" a component which stays in the same directory.)

Moving a component is quite tricky due to the way NgModules work. Many users would expect that moving a component from foo/ to bar/ would also change it to be included in @NgModule class Bar { } instead of @NgModule class Foo { } (depending on file structure). Currently the CLI doesn't have knowledge of NgModule structure like this. Even after moving a component into a different NgModule, it may break other modules which include Foo but do not include Bar. Whether it is reasonable to start depending on Bar requires application context which the CLI does not have.

Alternatively, the CLI could ignore NgModule and just move the component, but again that can be done with a trivially simple script.

mv foo.component.* bar/
# OR
mv foo.service.* bar/

IDEs can be a bit smarter about this and update imports to use the new path, but there's not much the CLI itself can do that would be better than that.

Rename component

(In this context, I'm defining "renaming" a component as staying in the same directory, as opposed to "moving" it into a different directory.)

If we only rename a component and keep it in the same directory, then that dodges a lot of the NgModule questions. There are some improvements which could be made here.

  • Rename all the files associated with a component at once, in a single operation.
  • Rename templateUrl and styleUrls: These are references to local files which would be renamed as part of the operation. It makes sense to rename these values as well. IDEs probably would not know to change this (some heuristics might cover this case though).
  • Rename internal symbols: If you have a foo.component.ts with a export class FooComponent { }, and then renamed the file to bar.component.ts, it may be reasonable to also rename the class to BarComponent (this is probably more debatable, TBD if that is what users expect/want).

All of these are more of an IDE feature than a CLI feature. After some discussion among the CLI team, we feel that these features make more sense in the Angular language service than they do in the CLI itself. I believe there are often hooks to handle file renaming, where these features could be directly implemented. Through the language service, it could also provide these same features when moving across directories (ignoring the NgModule challenge). This would probably be a much cleaner user experience and would be much more usable than going through the CLI.

I filed https://github.com/angular/angular/issues/65524 for the language service to make the changes there. However, we don't think it makes sense to include this kind of feature in the CLI itself. All these use cases are either handled just as well by existing well-known and supported tools, or would be better served in the language service than in the CLI.

dgp1130 avatar Jun 11 '20 19:06 dgp1130

I agree with your opinions, this is really hard to let Angular CLI do this. The IDE should be more intelligent and can help refactor instead of Angular CLI. Thank for answer

hiepxanh avatar Jun 12 '20 00:06 hiepxanh

Hope there will be a language server for Angular left to implement this, seems like they lack manpower? https://github.com/angular/vscode-ng-language-service/issues/335

Sti2nd avatar Jun 12 '20 13:06 Sti2nd