Angular Cli Cusom ng生成行为

时间:2018-01-31 14:12:37

标签: node.js angular angular-cli customization directory

我想在

时更改新组件的文件结构
  • 从命令行输入ng generate component pippo

观察到的行为

create src/app/pippo/pippo.component.html (24 bytes)
create src/app/pippo/pippo.component.spec.ts (621 bytes)
create src/app/pippo/pippo.component.ts (266 bytes)
create src/app/pippo/pippo.component.scss (0 bytes)

期望的行为

create src/app/components/pippo/tpl/pippo.component.html (24 bytes)
create src/app/components/pippo/ts/pippo.component.spec.ts (621 bytes)
create src/app/components/pippo/ts/pippo.component.ts (266 bytes)
create src/app/components/pippo/scss/pippo.component.scss (0 bytes)

解释

  1. 选择要部署组件的自定义文件夹,而不是始终使用应用文件夹
  2. 将模板,scss和ts分开,因为每个类型可能需要更多文件
  3. 可能吗? 我也可以使用npm“ fs

    创建自定义任务

    BAT解决方案

    @echo off
    setlocal enabledelayedexpansion
    ECHO #################################
    ECHO #################################
    ECHO NG COMPONENT CREATOR BY TONY SAMPERI
    ECHO #################################
    ECHO #################################
    
    set folderStatus=N
    set folderName=newComponent
    set sourcePath=%cd%\src\app\components
    cd %sourcePath%
    echo sourcePath is %sourcePath%
    
    call :getFolderName
    IF "!folderStatus!" == "C" call :createStructure
    IF "!folderStatus!" == "N" call :failedWarning
    
    PAUSE
    endlocal
    EXIT /B
    
    :getFolderName
    echo Component name?
    set /P folderName=
    IF exist %folderName% (
        echo Directory %folderName% already exists!
        goto :getFolderName
    ) ELSE (
        mkdir %folderName%
        set folderStatus=C
    )
    EXIT /B
    
    :createStructure
    cd %folderName%
    echo Moved to %sourcePath%\%folderName%
    
    mkdir ts
    cd ts
    type NUL > %folderName%.component.ts
    cd ..
    mkdir scss
    cd scss
    type NUL > %folderName%.component.scss
    cd ..
    mkdir tpl
    cd tpl
    type NUL > %folderName%.component.html
    cd ..
    echo DONE.
    

0 个答案:

没有答案