Skip to content

Commit

Permalink
[CONFIG] [Docker] [NPM] building process re-organized.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonzalo Diaz committed Jun 12, 2024
1 parent 4a5659b commit 931b87f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
dist/
**/*.js
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
// "plugin:@typescript-eslint/recommended-type-checked",
"prettier",
// "plugin:import/errors",
// "plugin:import/warnings",
Expand Down
75 changes: 59 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,103 @@ RUN npm install -g --ignore-scripts markdownlint-cli
# [!TIP] Use a bind-mount to "/app" to override following "copys"
# for lint and test against "current" sources in this stage

# YAML sources
COPY ./.github ${WORKDIR}/
COPY ./compose.yaml ${WORKDIR}/

# Markdown sources
COPY ./docs ${WORKDIR}/
COPY ./README.md ${WORKDIR}/
COPY ./LICENSE.md ${WORKDIR}/
COPY ./CODE_OF_CONDUCT.md ${WORKDIR}/

# Code source
COPY ./src ${WORKDIR}/src
COPY ./package.json ${WORKDIR}/package.json
COPY ./package-lock.json ${WORKDIR}/package-lock.json
COPY ./Makefile ${WORKDIR}/

# code linting conf
COPY ./.prettierrc ${WORKDIR}/
COPY ./.prettierignore ${WORKDIR}/
COPY ./.eslintrc ${WORKDIR}/
COPY ./.eslintignore ${WORKDIR}/

# markdownlint conf
COPY ./.markdownlint.yaml ${WORKDIR}/

# yamllint conf
COPY ./.yamllint ${WORKDIR}/
COPY ./.yamlignore ${WORKDIR}/

CMD ["make", "lint"]

###############################################################################
FROM base AS development

###############################################################################
FROM development AS builder
ENV WORKDIR=/app
WORKDIR ${WORKDIR}

COPY ./src ${WORKDIR}/src
COPY ./package.json ${WORKDIR}/package.json
COPY ./package-lock.json ${WORKDIR}/package-lock.json
COPY ./Makefile ${WORKDIR}/
COPY ./tsconfig.json ${WORKDIR}/tsconfig.json
COPY ./tsconfig.prod.json ${WORKDIR}/tsconfig.json

RUN npm ci --verbose --ignore-scripts
RUN ls -alh

# CMD []
###############################################################################
FROM development AS builder

ENV WORKDIR=/app
WORKDIR ${WORKDIR}

RUN npm run build

CMD ["ls", "-alh"]

###############################################################################
### In testing stage, can't use USER, due permissions issue
## in github actions environment:
##
## https://docs.github.com/en/actions/creating-actions/dockerfile-support-for-github-actions
##
FROM builder AS testing
FROM development AS testing

ENV LOG_LEVEL=info
ENV BRUTEFORCE=false
ENV WORKDIR=/app
WORKDIR ${WORKDIR}

WORKDIR /app

COPY ./.eslintrc /app/.eslintrc
COPY ./.prettierrc /app/.prettierrc
COPY ./jest.config.ts /app/jest.config.ts
COPY --from=builder /app/node_modules /app/node_modules
RUN ls -alh

CMD ["npm", "run", "test"]
CMD ["make", "test"]

###############################################################################
### In production stage
## in the production phase, "good practices" such as
## WORKSPACE and USER are maintained
##
FROM builder AS production
FROM base AS production

ENV NODE_ENV=production
ENV LOG_LEVEL=info
ENV BRUTEFORCE=false
ENV WORKDIR=/app
WORKDIR ${WORKDIR}

WORKDIR /app
COPY --from=builder /app/dist ${WORKDIR}/dist

COPY ./.eslintrc /app/.eslintrc
COPY ./.prettierrc /app/.prettierrc
COPY ./jest.config.ts /app/jest.config.ts
COPY --from=builder /app/node_modules /app/node_modules
COPY ./Makefile ${WORKDIR}/
COPY ./package.json ${WORKDIR}/package.json
COPY ./package-lock.json ${WORKDIR}/package-lock.json

RUN npm ci --verbose --omit=dev --omit=optional --ignore-scripts --no-cache
RUN ls -alh

USER node
CMD ["npm", "run", "test"]
CMD ["ls", "-alh"]
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ env:

clean:
npm run jest:clean
rm -vfr ./node_modules
rm -vfr ./coverage
rm -vfr ./dist
rm -vfr ./node_modules
mkdir -p ./coverage
touch ./coverage/.gitkeep

build: dependencies
${NPM} run build

dependencies:
@echo "################################################################################"
@echo "## Dependencies: ###############################################################"
Expand Down
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const jestConfig = {
'^.+\\.ts?$': 'ts-jest'
},
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
transformIgnorePatterns: ['<rootDir>/node_modules/'],
transformIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/node_modules/'],

// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"typescript": "^5.4.5"
},
"scripts": {
"build": "tsc -p tsconfig.prod.json",
"start": "npm run test",
"lint": "npx --yes eslint . --color --max-warnings=0 && echo '✔ Your code looks good.'",
"jest:ci": "node --experimental-vm-modules ./node_modules/.bin/jest --no-cache --ci --color --detectOpenHandles --forceExit --runInBand --debug",
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig",
"exclude": ["jest.config.ts", "**/*.test.ts", "**/*.mock.ts"]
}

0 comments on commit 931b87f

Please sign in to comment.