我有这个windows batch
文件,可以简化前端开发人员的工作:
@echo off
echo.
set NEW_COMPONENT=%1
set NEW_COMPONENT=%NEW_COMPONENT:/=\%
set NEW_COMPONENT_TO_SPLIT=%NEW_COMPONENT:/= %
REM get the component name (last item form '/' spearated array)
for %%a in ("%NEW_COMPONENT_TO_SPLIT%") do set "COMPONENT_NAME=%%~nxa"
set HTML=src\%NEW_COMPONENT%\%COMPONENT_NAME%.html
set SCSS=src\%NEW_COMPONENT%\_%COMPONENT_NAME%.scss
set JS=src\%NEW_COMPONENT%\_%COMPONENT_NAME%.js
set IMG=src\%NEW_COMPONENT%\img
mkdir src\%NEW_COMPONENT%
echo | set /p x=created folder: src\%NEW_COMPONENT%\
echo. 2>%HTML%
echo | set /p x=created file: %HTML%
echo. 2>%SCSS%
echo | set /p x=created file: %SCSS%
echo. 2>%JS%
echo created file: %JS%
mkdir %IMG%
echo | set /p x=created folder: %IMG%\
echo.
现在,他们要求我将import %COMPONENT_NAME% from '%PARENT_COMPONENT';
字符串放入父组件.js
文件的第一行,以自动链接新组件和父组件。
用户可以这样使用:
create-component.bat components/header/nav
在这种情况下,%COMPONENT_NAME%
将是nav
,但如何才能在上一个项目之前捕获该项目?在这种情况下,它将是header
答案 0 :(得分:3)
for
可以使用相对路径。因此components\header\nav\..
与components\header
相同。从该路径中,您需要最后一个元素%%~nxA
:
set "NEW_COMPONENT_TO_SPLIT=components\header\nav"
for %%A in ("%NEW_COMPONENT_TO_SPLIT%\..") do set "COMPONENT_NAME=%%~nxA"
echo %COMPONENT_NAME"
答案 1 :(得分:0)
将您的for
循环部分更改为此(修改后的第一循环和添加的行):
for %%a in ("%NEW_COMPONENT_TO_SPLIT%") do set "COMPONENT_TMP=%%~pa"
for %%i in ("%COMPONENT_TMP:~0,-1%") do set "COMPONENT_NAME=%%~nxi"
因此,您的修订脚本:
@echo off
echo.
set NEW_COMPONENT=%1
set NEW_COMPONENT=%NEW_COMPONENT:/=\%
set NEW_COMPONENT_TO_SPLIT=%NEW_COMPONENT:/= %
REM get the component name (last item form '/' spearated array)
for %%a in ("%NEW_COMPONENT_TO_SPLIT%") do set "COMPONENT_TMP=%%~pa"
for %%i in ("%COMPONENT_TMP:~0,-1%") do set "COMPONENT_NAME=%%~nxi"
set HTML=src\%NEW_COMPONENT%\%COMPONENT_NAME%.html
set SCSS=src\%NEW_COMPONENT%\_%COMPONENT_NAME%.scss
set JS=src\%NEW_COMPONENT%\_%COMPONENT_NAME%.js
set IMG=src\%NEW_COMPONENT%\img
mkdir src\%NEW_COMPONENT%
echo | set /p x=created folder: src\%NEW_COMPONENT%\
echo. 2>%HTML%
echo | set /p x=created file: %HTML%
echo. 2>%SCSS%
echo | set /p x=created file: %SCSS%
echo. 2>%JS%
echo created file: %JS%
mkdir %IMG%
echo | set /p x=created folder: %IMG%\
echo.