Sorry for my english in advance.
I’ve got nothing installed locally on my pc except the docker. For example, I prefer running npm commands in Makefile like this:
#!/usr/bin/make
TAG := 20.6.0
USER := $(shell id -u)
GROUP := $(shell id -g)
PROJECT_DIR := $(shell pwd)
TIMEZONE := $(shell cat /etc/timezone)
HOME:= /nodejs
PROJECT_NAME ?= some_project
npm := docker run --rm -ti
--name $(PROJECT_NAME)
--volume $(PROJECT_DIR):/nodejs
--volume /etc/passwd:/etc/passwd:ro
--volume /etc/group:/etc/group:ro
--user $(USER):$(GROUP)
--workdir $(SRC)
--env HOME=$(HOME)
--env TZ=$(TIMEZONE)
--network host
node:$(TAG) npm
# install npm dependencies
npm-i:
$(npm) install
# build
build:
$(npm) run build
# And many other commands...
Then later I tried to write one universal command like this (some stuff taken from here: How to pass argument to Makefile from command line?):
#!/usr/bin/make
TAG := 20
THIS_DIR := $(shell pwd)
USER := $(shell id -u)
GROUP := $(shell id -g)
HOME := /nodejs
PROJECT_NAME ?= some_project
npm_cmd:
docker run -it --rm
--name $(PROJECT_NAME)
-v $(PROJECT_DIR):/nodejs
-v /etc/passwd:/etc/passwd:ro
-v /etc/group:/etc/group:ro
-u $(USER):$(USER)
-w $(HOME)/$(PROJECT_NAME)
--env HOME=$(HOME)
--network host
node:$(TAG) npm $(CMD)
.PHONY: npm
# Allows passing additional arguments to npm
npm:
$(MAKE) npm_cmd CMD="$(filter-out $@,$(MAKECMDGOALS))"
%:
@: # explained here: /questions/6273608/how-to-pass-argument-to-makefile-from-command-line
So now I run the commands like this:
make npm install
#or
make npm run build
But I encountered an issue with the npm run dev command and here is why. Usually I run it npm run dev -- --host
or npm run dev -- --host --port 3000
and the problem is in first double dash after dev.
This first double dash is somehow cut after running.
I guess filter-out should be substituted with another stuff. Please help me add dashes to $(CMD). I don’t want to change node:$(TAG) npm $(CMD)
to node:$(TAG) npm $(CMD) -- --host
. Thanks!