WhileDo: Cleaning Up Scripts and Docker
Continuing from yesterday.
Today I'll cleanup the absolute mess that is the package.json file(s) and the various docker-related files. We have package.json files in blog and blog-api and one in the root of course. But let's start with the docker files.
Docker
We have the following docker-related files and folders:
docker- a folder for docker-related files - keptdocker/Caddyfile.single- the current Caddy configuration file for the single image deployment method I am using now - keptdocker/entrypoint.single.sh- the entrypoint script for the image - kept./.dockerignore- all kinds of paths that shouldn't be part of the build context (will have to clean this up another time) - kept, but needs work./docker-compose.yml- this runs the local dev-env - kept, but needs work./docker-compose.prod.yml- this was what I used before switiching to just one imagine - removed./docker-compose.single.yml- this is used to run the prod build locally (to check or debug it) - kept./Dockerfile.caddy- Caddy config file from the old docker-compose.prod.yml - removed./apps/blog/Dockerfile.prod- Dockerfile from the olddocker-compose.prod.yml- removed./apps/blog/Dockerfile.dev- some old Dockerfile for the dev env - removed./apps/blog/.dockerignore- a .dockerignore from before api and blog were even part of the same monorepo - removed./apps/blog-api/Dockerfile- this one is still used for building the API in the current local dev setup. It shouldn't be needed to build an image like this if I mount everything into a node/pnpm container - kept, for now./apps/blog-api/.dockerignore- various files and paths excluded to speed up builds of the api. Shouldn't be needed with one of those in the root of the repo. - removed
Scripts
I am looking at this rat king of a package.json scripts section:
"scripts": {
"dev": "pnpx concurrently -k \"pnpm --dir apps/blog start\" \"pnpm --dir apps/blog wrangler:proxy\" \"pnpm --dir apps/cloudflare-blog-api dev\"",
"dev:docker": "op run --env-file=apps/blog-api/.env.1password -- sh -c 'pnpm build:blog:vps && pnpx concurrently -k \"pnpm --dir apps/blog start:vps\" \"docker compose up\"'",
"dev:docker:down": "docker compose down",
"dev:docker:rebuild": "docker compose build blog-api && pnpm dev:docker",
"docker:single:test": "docker compose -f docker-compose.single.yml up --build",
"docker:single:down": "docker compose -f docker-compose.single.yml down",
"docker:single:logs": "docker compose -f docker-compose.single.yml logs -f app",
"dev:api": "pnpm --dir apps/cloudflare-blog-api dev",
"dev:blog": "pnpm --dir apps/blog start",
"dev:vps-api": "pnpm --dir apps/blog-api dev",
"build": "pnpm --dir apps/blog build && pnpm --dir apps/cloudflare-blog-api check",
"build:blog": "pnpm --dir apps/blog build",
"build:blog:vps": "pnpm --dir apps/blog build:vps",
"build:api": "pnpm --dir apps/cloudflare-blog-api check",
"build:vps-api": "pnpm --dir apps/blog-api build",
"build:daily": "pnpx cross-env DAILY_FEED=1 ELEVENTY_RUN_MODE=build pnpm --dir apps/blog build-nocolor",
"deploy:api": "pnpm --dir apps/cloudflare-blog-api run deploy",
"sync:from-cf": "node scripts/sync-from-cloudflare.js",
"sync:dry-run": "node scripts/sync-from-cloudflare.js --dry-run",
"sync:favs": "node scripts/sync-from-cloudflare.js --only=favs",
"sync:guestbook": "node scripts/sync-from-cloudflare.js --only=guestbook",
"test": "pnpm --dir apps/cloudflare-blog-api test",
"test:vps-api": "pnpm --dir apps/blog-api test",
"types:api": "pnpm --dir apps/cloudflare-blog-api types",
"format": "prettier . --check ./.prettierignore",
"format:write": "prettier . --write ./.prettierignore",
"prepare": "husky",
"lint": "eslint apps/cloudflare-blog-api apps/blog",
"lint:fix": "eslint apps/cloudflare-blog-api apps/blog --fix",
"lint:api": "eslint apps/cloudflare-blog-api",
"lint:blog": "eslint apps/blog",
"lint:and:format": "pnpm lint:fix && pnpm format:write",
"test:script": "vitest scripts/post-versions.test.js"
},
I can immediately remove anything that refers to the "cloudflare-blog-api" as we removed that already a few days ago. Same goes for the sync:... scripts as we don't run on cloudflare anymore. The sync script can also be deleted. Next we need to decide what we want to handle where. Personally, I like to have commands that I run all the time in the package.json as it simplifies the work on the blog. It's easier to write pnpm run dev:docker than op run --env-file=apps/blog-api/.env.1password -- sh -c 'pnpm build:blog:vps && pnpx concurrently -k \"pnpm --dir apps/blog start:vps\" \"docker compose up\"'. It would be even easier to run pnpm dev so I add that as an alias (the other dev script is gone by this point). We need that dev:docker:rebuild script for now as we build from a Dockerfile.
I also rename the docker:single:... scripts to dev:docker:single:... and remove the docker:single:logs command completely. I almost always read the logs through the Docker app. Let's skip for the moment down to the formating and linting section. I don't need a format and fromat:write script. I also don't need different linting scripts for all the parts of the repo. One is enough. I simplify here a little. These script are meant to autofix what is possible to autofix... ah but no. We need the check-only scripts for husky/git hooks. Alright. So we need a format script and a lint script that only checks files and a fix version for each of these tasks. And I'll throw in a do both script as well. While I was creating these script entries I realized that it's easier to organize by action first. So check:format or fix:lint. If I want to fix both I can do pnpm run fix:both. Don't know if I remember this going forward. But I'll try this out and see how it goes. I also update the git hook.
So here's where we stand now:
"scripts": {
"dev": "pnpm dev:docker",
"dev:docker": "op run --env-file=apps/blog-api/.env.1password -- sh -c 'pnpm build:blog:vps && pnpx concurrently -k \"pnpm --dir apps/blog start:vps\" \"docker compose up\"'",
"dev:docker:down": "docker compose down",
"dev:docker:rebuild": "docker compose build blog-api && pnpm dev:docker",
"dev:docker:single:test": "docker compose -f docker-compose.single.yml up --build",
"dev:docker:single:down": "docker compose -f docker-compose.single.yml down",
// 👇 still to-do 👇
"dev:blog": "pnpm --dir apps/blog start",
"dev:vps-api": "pnpm --dir apps/blog-api dev",
"build:blog": "pnpm --dir apps/blog build",
"build:blog:vps": "pnpm --dir apps/blog build:vps",
"build:vps-api": "pnpm --dir apps/blog-api build",
"build:daily": "pnpx cross-env DAILY_FEED=1 ELEVENTY_RUN_MODE=build pnpm --dir apps/blog build-nocolor",
"test:vps-api": "pnpm --dir apps/blog-api test",
"test:script": "vitest scripts/post-versions.test.js",
// 👆 still to-do 👆
"prepare": "husky",
"check:format": "prettier . --check ./.prettierignore",
"check:lint": "eslint .",
"check:both": "pnpm check:lint && pnpm check:format",
"fix:format": "prettier . --write ./.prettierignore",
"fix:lint": "eslint . --fix",
"fix:both": "pnpm fix:lint && pnpm fix:format"
},
So I don't think we need most of the ones we have't touched, yet. For example dev:blog. Because if we were to work on the blog only we either still want everything else running - to see how it looks in concert - or we can just cd into the blog sub dir to run the blog directly from there. Some of these can also be called directly (e.g. pnpm --dir apps/blog build:vps instead of pnpm build:blog:vps in dev:docker) in other scripts. Those "aliases" the don't do enough. This leaves us with build:daily, test:vps-api, test:script. The first and the last one refer to a feature not yet released: I'd like to have a summary post every evening on mastodon that shows what posts have been changed throughout the day and this special build and the test for the script are parts of that. I'll have to revisit this at some point, for now I just "namespace" them with daily-update:.... But test:vps-api is not generic enough and doesn't test the whole repo (because vitest is not yet setup for the blog). For now I just rename it to just test.
So cleanup for this session is done! I decided against delving into the package.json file's for each app as I have to get going. Here's the scripts section from my root package.json one more time:
"scripts": {
"dev": "pnpm dev:docker",
"dev:docker": "op run --env-file=apps/blog-api/.env.1password -- sh -c 'pnpm --dir apps/blog build:vps && pnpx concurrently -k \"pnpm --dir apps/blog start:vps\" \"docker compose up\"'",
"dev:docker:down": "docker compose down",
"dev:docker:rebuild": "docker compose build blog-api && pnpm dev:docker",
"dev:docker:single:test": "docker compose -f docker-compose.single.yml up --build",
"dev:docker:single:down": "docker compose -f docker-compose.single.yml down",
"test": "pnpm --dir apps/blog-api test",
"daily-update:build:daily": "pnpx cross-env DAILY_FEED=1 ELEVENTY_RUN_MODE=build pnpm --dir apps/blog build-nocolor",
"daily-update:test:script": "vitest scripts/post-versions.test.js",
"prepare": "husky",
"check:format": "prettier . --check ./.prettierignore",
"check:lint": "eslint .",
"check:both": "pnpm check:lint && pnpm check:format",
"fix:format": "prettier . --write ./.prettierignore",
"fix:lint": "eslint . --fix",
"fix:both": "pnpm fix:lint && pnpm fix:format"
},
Much cleaner!
-
← Previous
DailyDogo 1463 🐶 -
Next →
DailyDogo 1464 🐶