NPM/Yarn script를 Platform(Windows, macOS, linux 등)으로 구분하여 동작하기
NPM/Yarn script를 Platform(Windows, macOS, linux 등)으로 구분하여 동작하기
Platform이 다르면, 호출 명령이 차이가 있는 경우가 있고, path delimeter('⧵', '/') 문제가 종종 발생한다.
- package.json
...
"script": {
"build": "cd android && ./gradlew assembleDebug",
// Platform별로 하려면
"build.windows": "cd android && .\\gradlew assembleDebug", // Windows
"build.macOS": "cd android && ./gradlew assembleDebug", // macOS
"build.linux": "cd android && ./gradlew assembleDebug", // linux
...
},
...
위에서, "npm run build"를 Windows 에서 호출하면,
...
'.'은(는) 내부 또는 외부 명령, 실행할 수 있는 프로그램, 또는
배치 파일이 아닙니다.
와 같은, 오류가 발생하게 된다. 반대로, macOS/Linux에서는 문제 없이 실행될 것이다.
이를 문제 없이, Windows에서 호출하려면, "npm run build.windows"처럼 해줘야 하는데, 이는 굉장히 불편하고 복잡하다.
이를 해결하기 위해서, 좋은 방법 중 하나가, "run-script-os"를 사용하는 것을 추천한다.
아래와 같이 하면 된다.
1. run-script-os를 추가 (global ("-g")로 추가하면, 되지 않는다.)
yarn add run-script-os
npm install run-script-os
2. package.json에 "script" 변경
...
"script": {
"build": "run-script-os",
"build:win32": "cd android && .\\gradlew assembleDebug", // Windows
"build:darwin": "cd android && ./gradlew assembleDebug", // macOS
"test": "run-script-os",
"test:win32": "cd android && .\\gradlew assembleDebug", // Window
"test:darwin:linux": "cd android && ./gradlew assembleDebug", // macOS/linux
...
},
...
":win32", ":macOS", ":linux" 등의 postfix를 붙여서, 명령을 구분하도록 할 수 있게 되는 것이다.
단점은, 여전히 번잡하다......
댓글
댓글 쓰기