Skip to content

Commit f6b492b

Browse files
committed
feat(node): Start a Makefile for node projects
1 parent 0c17c3c commit f6b492b

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

Makefile.node

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#Serial 2025110601
2+
# This makefile is intended to enable NodeJS/NPM repositories.
3+
4+
## Recommended .gitignore additions:
5+
# .ash_history
6+
# .container-env
7+
# .node_repl_history
8+
# node_modules/
9+
# .npm/
10+
11+
## Override any of the below ?= variables in .config.mk
12+
-include .config.mk
13+
## If you have any local to your repository modifications or extensions
14+
## for this makefile load them into local.mk
15+
## A good usecase would be creating another .check-env:: that looks for
16+
## specific NPM/Node related environment variables
17+
-include local.mk
18+
19+
DEFAULT_CLEAN_PATHS ?= *.zip *.backup .node_repl_history .ash_history $(CONTAINER_ENV) ## Default paths for the main clean target
20+
CLEAN_PATHS ?= ## Overrideable extra paths for cleanup
21+
CONTAINER_ENGINE ?= docker ## Commands will be executed via the container engine, expected to be docker cli compatible
22+
CONTAINER_ENV ?= .container-env ## Collects the necessary environment variables for your docker runs
23+
CONTAINER_WORK_DIR ?= /data
24+
CONTAINER_IMAGE ?= docker.io/library/node
25+
CONTAINER_VERSION ?= lts-alpine ## Override this in .config.mk to pin an image
26+
CONTAINER_SHELL ?= /bin/ash
27+
NODE_ENTRYPOINT ?= server.js
28+
USER_AWS_CONFIG ?= ${HOME}/.aws
29+
30+
# Helper switches for the BASE_COMMAND
31+
ifeq ("$(USER_AWS_CONFIG)", "$(wildcard $(USER_AWS_CONFIG))")
32+
BASE_ENV := -v $(USER_AWS_CONFIG):/.aws:Z
33+
endif
34+
BASE_ENV := $(BASE_ENV) --env-file=$(CONTAINER_ENV)
35+
BASE_USER := -u $(shell id -u ${USER}):$(shell id -g ${USER})
36+
BASE_WORKDIR := -w $(CONTAINER_WORK_DIR) -v "$(CURDIR)":$(CONTAINER_WORK_DIR):Z
37+
38+
# Container based commands to for use handling target steps
39+
BASE_COMMAND := $(CONTAINER_ENGINE) run --rm -it $(BASE_USER) $(BASE_ENV) $(BASE_WORKDIR)
40+
COMMAND := $(BASE_COMMAND) $(CONTAINER_IMAGE):$(CONTAINER_VERSION)
41+
42+
all: help
43+
44+
# Exports the variables for shell use
45+
export
46+
47+
# This helper function makes debuging much easier.
48+
.PHONY: debug-%
49+
.SILENT: debug-%
50+
debug-%: ## Debug a variable by calling `make debug-VARIABLE`
51+
echo $(*) = $($(*))
52+
53+
.PHONY: .check-env
54+
.SILENT: .check-env
55+
.check-env:: # This is ::'d so that you can add a custom version to local.mk for repo specific checks
56+
57+
.PHONY: help
58+
.SILENT: help
59+
help: ## Show this help, includes list of all actions.
60+
awk 'BEGIN {FS = ":.*?## "}; /^.+: .*?## / && !/awk/ {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' ${MAKEFILE_LIST}
61+
62+
.PHONY: clean
63+
clean: ## Cleanup the local checkout
64+
-rm -rf $(DEFAULT_CLEAN_PATHS) $(CLEAN_PATHS)
65+
66+
.SILENT: $(CONTAINER_ENV)
67+
$(CONTAINER_ENV):
68+
env | awk '!/TOKEN/ && /^(AWS)/ { print }' | sort > $(CONTAINER_ENV)
69+
70+
package.json: ## If does not exist will `npm init` to create a new package.json for a new project
71+
$(COMMAND) npm init
72+
73+
.PHONY: init
74+
init: package.json $(CONTAINER_ENV) ## Used to initialize repository by loading dependencies
75+
$(COMMAND) npm install
76+
77+
.PHONY: repl
78+
repl: init ## To enter Node REPL interactively
79+
$(COMMAND)
80+
81+
.PHONY: run-%
82+
run-%: init ## Standard entry point for running run scripts from packages.json in the container
83+
$(COMMAND) npm run $*
84+
85+
.PHONY: shell
86+
shell: init ## To interactively enter the container in a shell
87+
$(COMMAND) $(CONTAINER_SHELL)
88+
89+
# Your server needs to be able to catch SIGINT to be able to close this once it starts from the same terminal
90+
.PHONY: start
91+
start: init ## Standard entry point for running your application in the container
92+
$(COMMAND) node $(NODE_ENTRYPOINT)
93+
94+
.PHONY: test
95+
test: ## Standard entry point for running tests.
96+
$(COMMAND) npm test
97+
98+
.PHONY: update-dependencies
99+
update-dependencies: ## Run command to update dependencies in `packages.json`
100+
$(COMMAND) npx npm-check-updates --upgrade

0 commit comments

Comments
 (0)