Skip to content

Commit 0a4c1dc

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

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

Makefile.node

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

0 commit comments

Comments
 (0)