@zairakai/js-dev-tools 4.1.1 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitlab/ci/parts/quality.yml +20 -7
- package/README.md +12 -1
- package/package.json +4 -4
- package/scripts/bats-run.sh +89 -0
- package/scripts/check-config-copies.sh +83 -0
- package/scripts/config.sh +1 -1
- package/scripts/install-bats.sh +7 -4
- package/scripts/install-shellcheck.sh +9 -4
- package/scripts/security-audit.sh +30 -0
- package/scripts/setup-check.sh +42 -0
- package/scripts/setup-state.sh +175 -0
- package/scripts/setup-update.sh +84 -0
- package/scripts/uninstall-package.sh +235 -0
- package/scripts/update-dependencies.mjs +54 -4
- package/tools/make/bats.mk +4 -9
- package/tools/make/utils-shared.mk +1 -1
- package/tools/make/utils.mk +17 -0
- package/tools/scripts/git-cleanup.sh +19 -8
- package/tools/scripts/git-rebase.sh +6 -0
- package/tools/scripts/git-update.sh +17 -13
|
@@ -105,6 +105,7 @@ js:quality:knip:
|
|
|
105
105
|
script:
|
|
106
106
|
- bash "$JS_DEV_TOOLS_SCRIPTS/knip.sh"
|
|
107
107
|
|
|
108
|
+
# Node is installed here: the tests of the setup and of the audit run node scripts (they read semver in node_modules/)
|
|
108
109
|
js:quality:bats:
|
|
109
110
|
extends:
|
|
110
111
|
- .js:base
|
|
@@ -113,14 +114,26 @@ js:quality:bats:
|
|
|
113
114
|
stage: quality
|
|
114
115
|
image: ${ALPINE_IMAGE}
|
|
115
116
|
before_script:
|
|
116
|
-
- apk add --no-cache bash bats git
|
|
117
|
+
- apk add --no-cache bash bats git nodejs
|
|
118
|
+
- node --version
|
|
117
119
|
script:
|
|
118
|
-
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
120
|
+
- bash "$JS_DEV_TOOLS_SCRIPTS/bats-run.sh"
|
|
121
|
+
|
|
122
|
+
# The security audit of every pipeline, for information: it shows an advisory in the pipeline and does not block. The daily
|
|
123
|
+
# audit (deps:audit) opens the issues and starts the fix.
|
|
124
|
+
js:quality:security:
|
|
125
|
+
extends:
|
|
126
|
+
- .js:node
|
|
127
|
+
- .js:rules
|
|
128
|
+
stage: quality
|
|
129
|
+
allow_failure: true
|
|
130
|
+
script:
|
|
131
|
+
- bash "$JS_DEV_TOOLS_SCRIPTS/security-audit.sh"
|
|
132
|
+
artifacts:
|
|
133
|
+
paths:
|
|
134
|
+
- build/reports/npm-audit.json
|
|
135
|
+
expire_in: 1 month
|
|
136
|
+
when: always
|
|
124
137
|
|
|
125
138
|
js:quality:outdated:
|
|
126
139
|
extends: .js:node
|
package/README.md
CHANGED
|
@@ -198,6 +198,16 @@ bash node_modules/@zairakai/js-dev-tools/scripts/setup-project.sh --publish=tsco
|
|
|
198
198
|
|
|
199
199
|
</details>
|
|
200
200
|
|
|
201
|
+
`make setup-check` tells which files of the setup are missing or differ from what the package publishes now (it writes nothing, and exits with 1 when a file is missing or different). A file that differs can be a change of the project or a file late on the package: the check shows the difference, you decide what to keep. It covers the Makefile (it must include the dev-tools), `.editorconfig`, `config/dev-tools/` and the thin files and copies of the root. The CI file, the governance files, the hooks and `tsconfig.json` are not checked: a project makes them its own. In a Laravel app with `laravel-dev-tools`, the Makefile and `.editorconfig` belong to it and are not checked here.
|
|
202
|
+
|
|
203
|
+
`make setup-update` publishes again the files that `make setup-check` reports (missing or different), and only those. A file of the project that it replaces is copied first in `.dev-tools-backup/`: nothing is merged, you put your own settings back from the copy. `DRY_RUN=true make setup-update` lists the files without writing.
|
|
204
|
+
|
|
205
|
+
`make uninstall` (or `bash node_modules/@zairakai/js-dev-tools/scripts/uninstall-package.sh`) removes the files of the setup that are still what the package publishes, and the git hooks that the setup linked. A file that the project changed is kept and named; the files of `config/dev-tools/` that were changed go only after confirmation (or `--force`). Everything that is removed is copied first in `.dev-tools-backup/<date>/` (UTC), under its path from the root, and that folder is kept: `cp -R .dev-tools-backup/<date>/. .` puts it all back. `--dry-run` lists the files and removes nothing.
|
|
206
|
+
|
|
207
|
+
`bash node_modules/@zairakai/js-dev-tools/scripts/check-config-copies.sh` checks that the root agrees with `config/dev-tools/` (the copies of the ignore files and the thin files); `SYNC=true` writes the copies again from `config/dev-tools/`.
|
|
208
|
+
|
|
209
|
+
In a full-stack project (`laravel-dev-tools` and this package), `make setup-check`, `make setup-update` and `make uninstall` run the Laravel script first, then this one.
|
|
210
|
+
|
|
201
211
|
---
|
|
202
212
|
|
|
203
213
|
## GitLab CI pipeline templates
|
|
@@ -230,7 +240,7 @@ Both files are entry points: the jobs are in `.gitlab/ci/parts/`, one file per r
|
|
|
230
240
|
| :--- | :--- | :---: | :---: |
|
|
231
241
|
| `base.yml` | workflow, runner, variables, hidden jobs | yes | yes |
|
|
232
242
|
| `security.yml` | secret detection, check of the tag | yes | yes |
|
|
233
|
-
| `quality.yml` | install, validate, quality, test | yes | yes |
|
|
243
|
+
| `quality.yml` | install, validate, quality (including `js:quality:security`, the npm audit that fails on an advisory), test | yes | yes |
|
|
234
244
|
| `updates.yml` | `deps:update`, `deps:audit`, `deps:release`, `deps:tag` | yes | yes |
|
|
235
245
|
| `publish.yml` | build, publish, release, documentation, `deps:cascade` | yes | no |
|
|
236
246
|
|
|
@@ -248,6 +258,7 @@ The pipeline template updates the dependencies of a project by itself, and this
|
|
|
248
258
|
- A weekly schedule with the variable `UPDATE_DEPENDENCIES=true`, or a pipeline started by the cascade, runs `deps:update`: it takes the **minor and patch** updates after a cooling period, opens a merge request into `develop` and merges it **when its pipeline succeeds**. Major versions are never applied.
|
|
249
259
|
- `deps:release` brings `develop` into `main` after such an update, and `deps:tag` tags `main` when a published range (`dependencies`, `peerDependencies`) changed. A project that changed only its development dependencies is not tagged: its published archive is the same.
|
|
250
260
|
- After the publication of a tag, `deps:cascade` waits for the version to be on the registry, then starts the update of the projects that have a `.dependency-update.json`.
|
|
261
|
+
- `make security-audit` runs `npm audit` and fails when it cannot run, or finds an advisory that is **high or critical** (the report is `build/reports/npm-audit.json`; the others are listed and do not block, and an unknown severity counts as a medium one). It is the level at which the daily `deps:audit` fixes an advisory at once with `AUTO_FIX=true`, and the rule is in one place, `audit-check` of `update-dependencies.mjs`. The job `js:quality:security` runs it on every pipeline for information: it shows the advisory and does not block the pipeline (`allow_failure`), the daily audit takes care of it. In a full-stack project, the Composer audit runs first, with the same level, then this one.
|
|
251
262
|
- The packages of the [dependency policy](https://gitlab.com/zairakai/dependency-policy) (`node.json`) are never updated. `node node_modules/@zairakai/js-dev-tools/scripts/update-dependencies.mjs policy-check <ref>` fails when the change since `<ref>` updates one of them in `package.json` or `package-lock.json`; it only reads, so it runs on GitHub as well.
|
|
252
263
|
- `dependabot-sync` rewrites the ignore rules of `.github/dependabot.yml`, between `# BEGIN dependency-policy (npm)` and `# END dependency-policy (npm)`, with one rule per package of the policy: the policy is the only place where a package is held back.
|
|
253
264
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zairakai/js-dev-tools",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "The development toolkit of the JavaScript and TypeScript projects for the web: shared ESLint, Prettier, Stylelint, Vitest, TypeScript and markdownlint rules, Knip, Makefile targets, Git hooks and GitLab CI pipeline templates",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dev-tools",
|
|
@@ -110,14 +110,14 @@
|
|
|
110
110
|
"dependencies": {
|
|
111
111
|
"@stanislas-poisson/js-dev-tools": "^3.1.0",
|
|
112
112
|
"eslint": "^10.14.0",
|
|
113
|
-
"markdownlint-cli2": "^0.24.
|
|
114
|
-
"prettier": "^3.
|
|
113
|
+
"markdownlint-cli2": "^0.24.1",
|
|
114
|
+
"prettier": "^3.10.1",
|
|
115
115
|
"semver": "^7.8.6",
|
|
116
116
|
"stylelint": "^18.0.0"
|
|
117
117
|
},
|
|
118
118
|
"devDependencies": {
|
|
119
119
|
"@vitest/coverage-v8": "^5.1.0",
|
|
120
|
-
"knip": "^6.
|
|
120
|
+
"knip": "^6.45.1",
|
|
121
121
|
"sort-package-json": "^4.1.0",
|
|
122
122
|
"typescript": "^6.0.3",
|
|
123
123
|
"vitest": "^5.1.0",
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
set -euo pipefail
|
|
3
|
+
|
|
4
|
+
# Load central configuration
|
|
5
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
6
|
+
# shellcheck disable=SC1091
|
|
7
|
+
source "$SCRIPT_DIR/config.sh"
|
|
8
|
+
|
|
9
|
+
log_header "Shell tests (Bats)"
|
|
10
|
+
|
|
11
|
+
# Runs every Bats file of tests/bats/ and fails when a test fails or disappears: the number of "ok" and "not ok" lines
|
|
12
|
+
# must equal the plan ("1..N"). Used by `make bats` and by the pipeline.
|
|
13
|
+
|
|
14
|
+
if ! command_exists bats; then
|
|
15
|
+
log_error "Bats is not installed - run: bash scripts/install-bats.sh"
|
|
16
|
+
exit 1
|
|
17
|
+
fi
|
|
18
|
+
|
|
19
|
+
mapfile -t files < <(find "${PROJECT_ROOT}/tests/bats" -name '*.bats' -type f 2> /dev/null | sort)
|
|
20
|
+
|
|
21
|
+
if [[ ${#files[@]} -eq 0 ]]; then
|
|
22
|
+
log_info "No Bats tests found - skipping"
|
|
23
|
+
exit 0
|
|
24
|
+
fi
|
|
25
|
+
|
|
26
|
+
report="$(mktemp)"
|
|
27
|
+
bats_pid=""
|
|
28
|
+
trap '[[ -n "$bats_pid" ]] && kill "$bats_pid" 2> /dev/null; rm -f "$report"' EXIT
|
|
29
|
+
|
|
30
|
+
# A counter on one line, updated while Bats runs: the results done out of the plan of the report
|
|
31
|
+
show_progress() {
|
|
32
|
+
local pid="$1" file="$2" total finished
|
|
33
|
+
|
|
34
|
+
while kill -0 "$pid" 2> /dev/null; do
|
|
35
|
+
total="$(grep -m 1 -o -E '^1\.\.[0-9]+' "$file" 2> /dev/null | cut -d. -f3 || true)"
|
|
36
|
+
finished="$(grep -c -E '^(not )?ok ' "$file" 2> /dev/null || true)"
|
|
37
|
+
printf '\r Bats: %s/%s tests' "${finished:-0}" "${total:-?}"
|
|
38
|
+
sleep 0.2
|
|
39
|
+
done
|
|
40
|
+
|
|
41
|
+
# Clear the line
|
|
42
|
+
printf '\r\033[K'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
# The full report is read from a file: it gives the number of tests whatever is shown. VERBOSE=true shows every line.
|
|
46
|
+
# The counter is shown on a terminal only: a log of the CI would hold every update.
|
|
47
|
+
status=0
|
|
48
|
+
if [[ "${VERBOSE:-false}" == "true" ]]; then
|
|
49
|
+
bats --tap "${files[@]}" 2>&1 | tee "$report" || status=$?
|
|
50
|
+
else
|
|
51
|
+
bats --tap "${files[@]}" > "$report" 2>&1 &
|
|
52
|
+
bats_pid=$!
|
|
53
|
+
|
|
54
|
+
[[ -t 1 ]] && show_progress "$bats_pid" "$report"
|
|
55
|
+
|
|
56
|
+
wait "$bats_pid" || status=$?
|
|
57
|
+
bats_pid=""
|
|
58
|
+
fi
|
|
59
|
+
|
|
60
|
+
planned="$(sed -n 's/^1\.\.\([0-9][0-9]*\)$/\1/p' "$report" | head -n 1)"
|
|
61
|
+
reported="$(grep -c -E '^(not )?ok ' "$report" || true)"
|
|
62
|
+
failed="$(grep -c -E '^not ok ' "$report" || true)"
|
|
63
|
+
|
|
64
|
+
# Only what went wrong: the failed tests with their details, and the warnings of Bats
|
|
65
|
+
if [[ "${VERBOSE:-false}" != "true" ]]; then
|
|
66
|
+
awk '
|
|
67
|
+
/^not ok / { show = 1; print; next }
|
|
68
|
+
/^ok / { show = 0; next }
|
|
69
|
+
/^# bats warning/ { print; next }
|
|
70
|
+
show && /^#/ { print }
|
|
71
|
+
' "$report"
|
|
72
|
+
fi
|
|
73
|
+
|
|
74
|
+
if [[ -z "$planned" ]]; then
|
|
75
|
+
log_error "Bats gave no plan (1..N): the run did not start"
|
|
76
|
+
exit 1
|
|
77
|
+
fi
|
|
78
|
+
|
|
79
|
+
if [[ "$reported" -ne "$planned" ]]; then
|
|
80
|
+
log_error "Bats planned ${planned} tests and reported ${reported}: a test disappeared"
|
|
81
|
+
exit 1
|
|
82
|
+
fi
|
|
83
|
+
|
|
84
|
+
if [[ "$failed" -gt 0 ]] || [[ "$status" -ne 0 ]]; then
|
|
85
|
+
log_error "${failed} Bats test(s) failed"
|
|
86
|
+
exit 1
|
|
87
|
+
fi
|
|
88
|
+
|
|
89
|
+
log_success "${reported} Bats tests passed"
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
#
|
|
3
|
+
# Zairakai NPM Dev Tools - Config Copies Check
|
|
4
|
+
# The root of the project and config/dev-tools/ must say the same.
|
|
5
|
+
#
|
|
6
|
+
# The root holds what the editors read: thin files that point at config/dev-tools/ (the bridges and .markdownlint.json),
|
|
7
|
+
# and copies of the ignore files, which cannot point at another file. The scripts read config/dev-tools/.
|
|
8
|
+
#
|
|
9
|
+
# Usage:
|
|
10
|
+
# bash check-config-copies.sh # Exit code 0 when the root agrees, 1 otherwise
|
|
11
|
+
# SYNC=true bash check-config-copies.sh # Writes the copies of the root again from config/dev-tools/ first
|
|
12
|
+
#
|
|
13
|
+
|
|
14
|
+
set -euo pipefail
|
|
15
|
+
|
|
16
|
+
# Load central configuration
|
|
17
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
18
|
+
# shellcheck disable=SC1091
|
|
19
|
+
source "${SCRIPT_DIR}/config.sh"
|
|
20
|
+
|
|
21
|
+
log_header "Configuration of the editor (root and config/dev-tools)"
|
|
22
|
+
|
|
23
|
+
if [[ ! -d "${PROJECT_ROOT}/config/dev-tools" ]]; then
|
|
24
|
+
log_info "No config/dev-tools/ in this project, nothing to compare"
|
|
25
|
+
exit 0
|
|
26
|
+
fi
|
|
27
|
+
|
|
28
|
+
# The ignore files that the root holds as copies
|
|
29
|
+
copies=(.prettierignore .stylelintignore .markdownlintignore)
|
|
30
|
+
|
|
31
|
+
# SYNC=true writes again the copies of the root from config/dev-tools/ before the check
|
|
32
|
+
if [[ "${SYNC:-false}" == "true" ]]; then
|
|
33
|
+
log_step "Writing again the copies of the root…"
|
|
34
|
+
|
|
35
|
+
for name in "${copies[@]}"; do
|
|
36
|
+
if [[ -f "${PROJECT_ROOT}/config/dev-tools/${name}" ]]; then
|
|
37
|
+
cp "${PROJECT_ROOT}/config/dev-tools/${name}" "${PROJECT_ROOT}/${name}"
|
|
38
|
+
fi
|
|
39
|
+
done
|
|
40
|
+
fi
|
|
41
|
+
|
|
42
|
+
problems=()
|
|
43
|
+
|
|
44
|
+
for name in "${copies[@]}"; do
|
|
45
|
+
published="${PROJECT_ROOT}/config/dev-tools/${name}"
|
|
46
|
+
[[ -f "$published" ]] || continue
|
|
47
|
+
|
|
48
|
+
if [[ ! -f "${PROJECT_ROOT}/${name}" ]]; then
|
|
49
|
+
problems+=("${name} is missing at the root: it is a copy of config/dev-tools/${name}")
|
|
50
|
+
elif ! cmp -s "${PROJECT_ROOT}/${name}" "$published"; then
|
|
51
|
+
problems+=("${name} at the root differs from config/dev-tools/${name}")
|
|
52
|
+
fi
|
|
53
|
+
done
|
|
54
|
+
|
|
55
|
+
# A thin file points at the file that it stands for: the one of config/dev-tools/ that exists
|
|
56
|
+
for key in eslint prettier stylelint vitest; do
|
|
57
|
+
bridge="${PROJECT_ROOT}/${key}.config.mjs"
|
|
58
|
+
published="config/dev-tools/${key}.config.js"
|
|
59
|
+
|
|
60
|
+
[[ -f "$bridge" ]] || continue
|
|
61
|
+
[[ -f "${PROJECT_ROOT}/${published}" ]] || continue
|
|
62
|
+
|
|
63
|
+
# A bridge that the project replaced by its own configuration is not a thin file: it does not name config/dev-tools/
|
|
64
|
+
if grep -qF "config/dev-tools/" "$bridge" && ! grep -qF "$published" "$bridge"; then
|
|
65
|
+
problems+=("${key}.config.mjs at the root does not point at ${published}")
|
|
66
|
+
fi
|
|
67
|
+
done
|
|
68
|
+
|
|
69
|
+
if [[ -f "${PROJECT_ROOT}/.markdownlint.json" ]] && [[ -f "${PROJECT_ROOT}/config/dev-tools/.markdownlint.json" ]]; then
|
|
70
|
+
if ! grep -qF "config/dev-tools/.markdownlint.json" "${PROJECT_ROOT}/.markdownlint.json"; then
|
|
71
|
+
problems+=(".markdownlint.json at the root does not point at config/dev-tools/.markdownlint.json")
|
|
72
|
+
fi
|
|
73
|
+
fi
|
|
74
|
+
|
|
75
|
+
if [[ ${#problems[@]} -gt 0 ]]; then
|
|
76
|
+
for line in "${problems[@]}"; do
|
|
77
|
+
log_error "$line"
|
|
78
|
+
done
|
|
79
|
+
log_info "Run: SYNC=true bash node_modules/@zairakai/js-dev-tools/scripts/check-config-copies.sh"
|
|
80
|
+
exit 1
|
|
81
|
+
fi
|
|
82
|
+
|
|
83
|
+
log_success "The files of the root agree with config/dev-tools/"
|
package/scripts/config.sh
CHANGED
|
@@ -261,7 +261,7 @@ backup_file() {
|
|
|
261
261
|
local base="${2:-${PROJECT_ROOT}/.dev-tools-backup}"
|
|
262
262
|
[[ ! -f "$file" || -L "$file" ]] && return 1
|
|
263
263
|
if [[ -z "$BACKUP_DIR" ]]; then
|
|
264
|
-
BACKUP_DIR="${base}/$(date +%Y%m%d-%H%M%S)"
|
|
264
|
+
BACKUP_DIR="${base}/$(date -u +%Y%m%d-%H%M%S)"
|
|
265
265
|
mkdir -p "$BACKUP_DIR"
|
|
266
266
|
fi
|
|
267
267
|
local rel="${file#"${PROJECT_ROOT}"/}"
|
package/scripts/install-bats.sh
CHANGED
|
@@ -57,10 +57,10 @@ version_ge() {
|
|
|
57
57
|
fi
|
|
58
58
|
|
|
59
59
|
local IFS=.
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
local i ver1_arr ver2_arr
|
|
61
|
+
read -ra ver1_arr <<< "$ver1"
|
|
62
|
+
read -ra ver2_arr <<< "$ver2"
|
|
62
63
|
|
|
63
|
-
local i
|
|
64
64
|
for ((i=${#ver1_arr[@]}; i<${#ver2_arr[@]}; i++)); do
|
|
65
65
|
ver1_arr[i]=0
|
|
66
66
|
done
|
|
@@ -221,4 +221,7 @@ main() {
|
|
|
221
221
|
fi
|
|
222
222
|
}
|
|
223
223
|
|
|
224
|
-
|
|
224
|
+
# Not run when the file is sourced (the tests load the functions)
|
|
225
|
+
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
|
|
226
|
+
main "$@"
|
|
227
|
+
fi
|
|
@@ -53,15 +53,17 @@ get_shellcheck_version() {
|
|
|
53
53
|
|
|
54
54
|
# Compare versions (returns 0 if ver1 >= ver2)
|
|
55
55
|
version_ge() {
|
|
56
|
-
local ver1="$1"
|
|
57
|
-
local ver2="$2"
|
|
56
|
+
local ver1="${1#v}"
|
|
57
|
+
local ver2="${2#v}"
|
|
58
58
|
|
|
59
59
|
if [[ "${ver1}" == "${ver2}" ]]; then
|
|
60
60
|
return 0
|
|
61
61
|
fi
|
|
62
62
|
|
|
63
63
|
local IFS=.
|
|
64
|
-
local i ver1_arr
|
|
64
|
+
local i ver1_arr ver2_arr
|
|
65
|
+
read -ra ver1_arr <<< "$ver1"
|
|
66
|
+
read -ra ver2_arr <<< "$ver2"
|
|
65
67
|
|
|
66
68
|
# Fill empty positions with zeros
|
|
67
69
|
for ((i=${#ver1_arr[@]}; i<${#ver2_arr[@]}; i++)); do
|
|
@@ -226,4 +228,7 @@ main() {
|
|
|
226
228
|
install_shellcheck "${required_version}"
|
|
227
229
|
}
|
|
228
230
|
|
|
229
|
-
|
|
231
|
+
# Not run when the file is sourced (the tests load the functions)
|
|
232
|
+
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
|
|
233
|
+
main "$@"
|
|
234
|
+
fi
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
set -euo pipefail
|
|
3
|
+
|
|
4
|
+
# Load central configuration
|
|
5
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
6
|
+
# shellcheck disable=SC1091
|
|
7
|
+
source "$SCRIPT_DIR/config.sh"
|
|
8
|
+
maybe_dockerize "$0" "$@"
|
|
9
|
+
|
|
10
|
+
log_header "Security Audit"
|
|
11
|
+
|
|
12
|
+
# Run npm audit and output a JSON report
|
|
13
|
+
AUDIT_REPORT="${PROJECT_ROOT}/build/reports/npm-audit.json"
|
|
14
|
+
ensure_dir "$(dirname "$AUDIT_REPORT")"
|
|
15
|
+
|
|
16
|
+
log_step "Running npm audit..."
|
|
17
|
+
|
|
18
|
+
# The exit code is not enough alone: npm audit ends with 1 for advisories, and also when it could not run (no network, no
|
|
19
|
+
# lock file). The report decides, and the level is the one of the daily audit (deps:audit): an advisory that is high,
|
|
20
|
+
# or critical blocks, the others (an unknown severity counts as a medium one) are listed. The rule is in
|
|
21
|
+
# update-dependencies.mjs, in one place.
|
|
22
|
+
audit_status=0
|
|
23
|
+
npm audit --json > "$AUDIT_REPORT" || audit_status=$?
|
|
24
|
+
|
|
25
|
+
if ! node "${SCRIPT_DIR}/update-dependencies.mjs" audit-check "$AUDIT_REPORT"; then
|
|
26
|
+
log_error "Security audit failed (npm audit exit ${audit_status}): see the advisories above"
|
|
27
|
+
exit 1
|
|
28
|
+
fi
|
|
29
|
+
|
|
30
|
+
log_success "No security vulnerabilities found at the high level or above"
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
#
|
|
3
|
+
# Zairakai NPM Dev Tools - Setup Check
|
|
4
|
+
# Tells which files of the setup are missing or differ from what the package publishes now. Writes nothing.
|
|
5
|
+
#
|
|
6
|
+
# Usage:
|
|
7
|
+
# bash setup-check.sh # Exit code 0 when every file is the same, 1 when a file is missing or different
|
|
8
|
+
#
|
|
9
|
+
|
|
10
|
+
set -euo pipefail
|
|
11
|
+
|
|
12
|
+
# Load central configuration
|
|
13
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
14
|
+
# shellcheck disable=SC1091
|
|
15
|
+
source "${SCRIPT_DIR}/config.sh"
|
|
16
|
+
# shellcheck source=setup-state.sh
|
|
17
|
+
source "${SCRIPT_DIR}/setup-state.sh"
|
|
18
|
+
|
|
19
|
+
log_header "Setup check (files published by the setup)"
|
|
20
|
+
|
|
21
|
+
# Read only: the files that the setup would publish are generated in a temporary folder and compared with the project's.
|
|
22
|
+
# A file that differs is a change of the project or a file late on the package: the check shows the difference.
|
|
23
|
+
temporary="$(mktemp -d)"
|
|
24
|
+
trap 'rm -rf "$temporary"' EXIT
|
|
25
|
+
|
|
26
|
+
setup_generate "$temporary"
|
|
27
|
+
|
|
28
|
+
problems=()
|
|
29
|
+
while IFS='|' read -r state file detail; do
|
|
30
|
+
[[ "$state" == "same" ]] && continue
|
|
31
|
+
problems+=("${file}: ${state}${detail:+ (${detail})}")
|
|
32
|
+
done < <(setup_results "$temporary")
|
|
33
|
+
|
|
34
|
+
if [[ ${#problems[@]} -gt 0 ]]; then
|
|
35
|
+
for line in "${problems[@]}"; do
|
|
36
|
+
log_warning "$line"
|
|
37
|
+
done
|
|
38
|
+
log_info "To publish again what is missing or late (the old file is kept in .dev-tools-backup/): make setup-update"
|
|
39
|
+
exit 1
|
|
40
|
+
fi
|
|
41
|
+
|
|
42
|
+
log_success "Every file of the setup is published and the same as the package's"
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
#
|
|
3
|
+
# Zairakai NPM Dev Tools - Setup State
|
|
4
|
+
# What the setup publishes in a project, compared with what the project has. Shared by setup-check.sh, setup-update.sh and
|
|
5
|
+
# uninstall-package.sh. This file is sourced after config.sh, it runs nothing.
|
|
6
|
+
#
|
|
7
|
+
# The files are generated in a temporary folder with the publication of setup-project.sh itself, then compared with the
|
|
8
|
+
# project's. Nothing is written in the project here. A file that differs can be a change of the project or a file late on
|
|
9
|
+
# the package: the check cannot tell, it shows the difference.
|
|
10
|
+
#
|
|
11
|
+
|
|
12
|
+
SETUP_BACKUP_FOLDER=".dev-tools-backup"
|
|
13
|
+
|
|
14
|
+
# The groups of the default publication. The hooks are a git setting, the CI file and the governance files are made the
|
|
15
|
+
# project's own when it publishes them, and tsconfig.json is the project's own (it extends the base and adds its paths).
|
|
16
|
+
SETUP_GROUPS=(quality style testing)
|
|
17
|
+
|
|
18
|
+
# The files of the root that the publication reads to choose its files: a bridge is not created when the project has its
|
|
19
|
+
# own configuration of the tool.
|
|
20
|
+
SETUP_INPUTS=(
|
|
21
|
+
package.json composer.json artisan
|
|
22
|
+
eslint.config.{js,mjs,cjs,ts,mts,cts}
|
|
23
|
+
prettier.config.{js,mjs,cjs,ts} .prettierrc{,.json,.yaml,.yml,.js,.mjs,.cjs}
|
|
24
|
+
stylelint.config.{js,mjs,cjs} .stylelintrc{,.json,.yaml,.yml,.js,.mjs,.cjs}
|
|
25
|
+
vitest.config.{js,mjs,cjs,ts} vitest.workspace.{js,ts} vite.config.{js,mjs,cjs,ts}
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
# The files of the project that the generation copied, so they are not part of the setup
|
|
29
|
+
SETUP_MIRRORED=()
|
|
30
|
+
|
|
31
|
+
# Helpers
|
|
32
|
+
# ================
|
|
33
|
+
|
|
34
|
+
# A Laravel app with laravel-dev-tools: it owns the Makefile and the .editorconfig (the full-stack ones), so the JS setup
|
|
35
|
+
# does not check them
|
|
36
|
+
setup_is_fullstack() {
|
|
37
|
+
[[ "$PROJECT_TYPE" == "laravel-app" ]] \
|
|
38
|
+
&& [[ -f "${PROJECT_ROOT}/vendor/zairakai/laravel-dev-tools/scripts/setup-package.sh" ]]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
# Whether the Makefile of the project includes the dev-tools (generated by the setup, or injected into an existing one)
|
|
42
|
+
setup_makefile_includes() {
|
|
43
|
+
grep -qE 'js-dev-tools|DEV_TOOLS_NPM' "$1" 2> /dev/null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
# What differs between the file of the project and the file of the package, in a few words: the keys of a JSON file, or
|
|
47
|
+
# the number of lines of another file
|
|
48
|
+
setup_difference() {
|
|
49
|
+
local mine="$1" generated="$2" file="$3" detail=""
|
|
50
|
+
|
|
51
|
+
if [[ "$file" == *.json ]] && command_exists node; then
|
|
52
|
+
detail="$(node -e '
|
|
53
|
+
const fs = require("fs")
|
|
54
|
+
const flat = (value, path, out) => {
|
|
55
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
56
|
+
for (const key of Object.keys(value)) flat(value[key], path ? path + "." + key : key, out)
|
|
57
|
+
} else out[path] = JSON.stringify(value)
|
|
58
|
+
return out
|
|
59
|
+
}
|
|
60
|
+
let mine, theirs
|
|
61
|
+
try {
|
|
62
|
+
mine = flat(JSON.parse(fs.readFileSync(process.argv[1], "utf8")), "", {})
|
|
63
|
+
theirs = flat(JSON.parse(fs.readFileSync(process.argv[2], "utf8")), "", {})
|
|
64
|
+
} catch { process.exit(3) }
|
|
65
|
+
const only = (a, b) => Object.keys(a).filter((key) => !(key in b))
|
|
66
|
+
const changed = Object.keys(mine).filter((key) => key in theirs && mine[key] !== theirs[key])
|
|
67
|
+
const list = (label, keys) => (keys.length ? label + ": " + keys.join(", ") : "")
|
|
68
|
+
console.log([
|
|
69
|
+
list("only in the project", only(mine, theirs)),
|
|
70
|
+
list("only in the package", only(theirs, mine)),
|
|
71
|
+
list("changed", changed),
|
|
72
|
+
].filter(Boolean).join("; "))
|
|
73
|
+
' "$mine" "$generated" 2> /dev/null || true)"
|
|
74
|
+
fi
|
|
75
|
+
|
|
76
|
+
if [[ -z "$detail" ]]; then
|
|
77
|
+
local in_project in_package
|
|
78
|
+
in_project="$(grep -cvxFf "$generated" "$mine" || true)"
|
|
79
|
+
in_package="$(grep -cvxFf "$mine" "$generated" || true)"
|
|
80
|
+
detail="${in_project} line(s) only in the project, ${in_package} line(s) only in the package"
|
|
81
|
+
fi
|
|
82
|
+
|
|
83
|
+
echo "$detail"
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
# Copies a file of the project in .dev-tools-backup/<UTC date>/, under its path from the root (the folder of backup_file).
|
|
87
|
+
# The folder ignores itself for git: the copies are never committed by mistake.
|
|
88
|
+
setup_backup() {
|
|
89
|
+
backup_file "$1" || return 0
|
|
90
|
+
|
|
91
|
+
local base="${BACKUP_DIR%/*}"
|
|
92
|
+
[[ -f "${base}/.gitignore" ]] || printf '*\n' > "${base}/.gitignore"
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
# Generation
|
|
96
|
+
# ================
|
|
97
|
+
|
|
98
|
+
# Generates in the folder $1 the files that the setup would publish in this project
|
|
99
|
+
setup_generate() {
|
|
100
|
+
local temporary="$1" name key group publisher
|
|
101
|
+
local -a first=(--with-makefile) kept=()
|
|
102
|
+
|
|
103
|
+
mkdir -p "${temporary}/node_modules/@zairakai"
|
|
104
|
+
ln -s "$DEV_TOOLS_ROOT" "${temporary}/node_modules/@zairakai/js-dev-tools"
|
|
105
|
+
publisher="${temporary}/node_modules/@zairakai/js-dev-tools/scripts/setup-project.sh"
|
|
106
|
+
|
|
107
|
+
# What the publication reads in the project to choose its files: the type of the project and its configurations
|
|
108
|
+
SETUP_MIRRORED=()
|
|
109
|
+
for name in "${SETUP_INPUTS[@]}"; do
|
|
110
|
+
if [[ -f "${PROJECT_ROOT}/${name}" ]]; then
|
|
111
|
+
cp "${PROJECT_ROOT}/${name}" "${temporary}/${name}"
|
|
112
|
+
SETUP_MIRRORED+=("$name")
|
|
113
|
+
fi
|
|
114
|
+
done
|
|
115
|
+
|
|
116
|
+
# A bridge that the setup made is part of the setup: it is generated again, not read as the project's own configuration
|
|
117
|
+
for key in eslint prettier stylelint vitest; do
|
|
118
|
+
name="${key}.config.mjs"
|
|
119
|
+
if [[ -f "${temporary}/${name}" ]] && grep -qF "config/dev-tools/${key}.config.js" "${temporary}/${name}"; then
|
|
120
|
+
rm -f "${temporary}/${name}"
|
|
121
|
+
fi
|
|
122
|
+
done
|
|
123
|
+
for name in "${SETUP_MIRRORED[@]}"; do
|
|
124
|
+
[[ -f "${temporary}/${name}" ]] && kept+=("$name")
|
|
125
|
+
done
|
|
126
|
+
SETUP_MIRRORED=("${kept[@]}")
|
|
127
|
+
|
|
128
|
+
setup_is_fullstack && first=()
|
|
129
|
+
|
|
130
|
+
for group in "${SETUP_GROUPS[@]}"; do
|
|
131
|
+
(cd "$temporary" && bash "$publisher" "--publish=${group}" --force --silent "${first[@]}") > /dev/null 2>&1 || true
|
|
132
|
+
first=()
|
|
133
|
+
done
|
|
134
|
+
|
|
135
|
+
if ! setup_is_fullstack; then
|
|
136
|
+
cp "${DEV_TOOLS_ROOT}/.editorconfig" "${temporary}/.editorconfig"
|
|
137
|
+
fi
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
# The files that the generation wrote, from the root of the project, in order
|
|
141
|
+
setup_generated_files() {
|
|
142
|
+
local temporary="$1" file name skip
|
|
143
|
+
|
|
144
|
+
while IFS= read -r file; do
|
|
145
|
+
skip=false
|
|
146
|
+
for name in "${SETUP_MIRRORED[@]}"; do
|
|
147
|
+
[[ "$file" == "$name" ]] && skip=true
|
|
148
|
+
done
|
|
149
|
+
[[ "$skip" == "true" ]] || echo "$file"
|
|
150
|
+
done < <(cd "$temporary" && find . -type f ! -path './node_modules/*' ! -path "./${SETUP_BACKUP_FOLDER}/*" | sed 's|^\./||' | sort)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
# One line per file of the setup: "<state>|<file>|<detail>", the state being same, missing or different
|
|
154
|
+
setup_results() {
|
|
155
|
+
local temporary="$1" file mine generated state detail
|
|
156
|
+
|
|
157
|
+
while IFS= read -r file; do
|
|
158
|
+
mine="${PROJECT_ROOT}/${file}"
|
|
159
|
+
generated="${temporary}/${file}"
|
|
160
|
+
state="same"
|
|
161
|
+
detail=""
|
|
162
|
+
|
|
163
|
+
if [[ "$file" == "Makefile" ]]; then
|
|
164
|
+
# A project adds its own targets: its Makefile is right when it includes the dev-tools
|
|
165
|
+
setup_makefile_includes "$mine" || state="missing"
|
|
166
|
+
elif [[ ! -f "$mine" ]]; then
|
|
167
|
+
state="missing"
|
|
168
|
+
elif ! cmp -s "$mine" "$generated"; then
|
|
169
|
+
state="different"
|
|
170
|
+
detail="$(setup_difference "$mine" "$generated" "$file")"
|
|
171
|
+
fi
|
|
172
|
+
|
|
173
|
+
printf '%s|%s|%s\n' "$state" "$file" "$detail"
|
|
174
|
+
done < <(setup_generated_files "$temporary")
|
|
175
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
#
|
|
3
|
+
# Zairakai NPM Dev Tools - Setup Update
|
|
4
|
+
# Publishes again the files that setup-check.sh reports (missing or different), and only those.
|
|
5
|
+
#
|
|
6
|
+
# A file of the project that is replaced is copied in .dev-tools-backup/ first: nothing is merged, the project puts its own
|
|
7
|
+
# settings back from the copy.
|
|
8
|
+
#
|
|
9
|
+
# Usage:
|
|
10
|
+
# bash setup-update.sh # Publish again the files
|
|
11
|
+
# DRY_RUN=true bash setup-update.sh # Lists the files, writes nothing
|
|
12
|
+
#
|
|
13
|
+
|
|
14
|
+
set -euo pipefail
|
|
15
|
+
|
|
16
|
+
# Load central configuration
|
|
17
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
18
|
+
# shellcheck disable=SC1091
|
|
19
|
+
source "${SCRIPT_DIR}/config.sh"
|
|
20
|
+
# shellcheck source=setup-state.sh
|
|
21
|
+
source "${SCRIPT_DIR}/setup-state.sh"
|
|
22
|
+
|
|
23
|
+
log_header "Setup update (files published by the setup)"
|
|
24
|
+
|
|
25
|
+
DRY_RUN="${DRY_RUN:-false}"
|
|
26
|
+
|
|
27
|
+
temporary="$(mktemp -d)"
|
|
28
|
+
trap 'rm -rf "$temporary"' EXIT
|
|
29
|
+
|
|
30
|
+
setup_generate "$temporary"
|
|
31
|
+
|
|
32
|
+
result=()
|
|
33
|
+
while IFS='|' read -r state file _; do
|
|
34
|
+
[[ "$state" == "same" ]] && continue
|
|
35
|
+
|
|
36
|
+
target="${PROJECT_ROOT}/${file}"
|
|
37
|
+
|
|
38
|
+
# A symbolic link is the project's: writing in it would write in what it points at, and no copy of it is kept
|
|
39
|
+
if [[ -L "$target" ]]; then
|
|
40
|
+
result+=("keep: ${file} (symbolic link)")
|
|
41
|
+
continue
|
|
42
|
+
fi
|
|
43
|
+
|
|
44
|
+
# A dry run says what it would do
|
|
45
|
+
would=""
|
|
46
|
+
[[ "$DRY_RUN" == "true" ]] && would="would "
|
|
47
|
+
|
|
48
|
+
if [[ "$state" == "missing" ]]; then
|
|
49
|
+
result+=("${would}create: ${file}")
|
|
50
|
+
else
|
|
51
|
+
result+=("${would}replace: ${file}")
|
|
52
|
+
fi
|
|
53
|
+
|
|
54
|
+
[[ "$DRY_RUN" == "true" ]] && continue
|
|
55
|
+
|
|
56
|
+
# A Makefile that does not include the dev-tools is not replaced: the setup adds its block to it
|
|
57
|
+
if [[ "$file" == "Makefile" ]]; then
|
|
58
|
+
setup_backup "$target"
|
|
59
|
+
bash "${SCRIPT_DIR}/setup-project.sh" --with-makefile --silent > /dev/null 2>&1 || true
|
|
60
|
+
continue
|
|
61
|
+
fi
|
|
62
|
+
|
|
63
|
+
if [[ "$state" == "different" ]]; then
|
|
64
|
+
setup_backup "$target"
|
|
65
|
+
fi
|
|
66
|
+
|
|
67
|
+
mkdir -p "$(dirname "$target")"
|
|
68
|
+
cp "${temporary}/${file}" "$target"
|
|
69
|
+
done < <(setup_results "$temporary")
|
|
70
|
+
|
|
71
|
+
if [[ ${#result[@]} -eq 0 ]]; then
|
|
72
|
+
log_success "Nothing to update: every file of the setup is the same as the package's"
|
|
73
|
+
exit 0
|
|
74
|
+
fi
|
|
75
|
+
|
|
76
|
+
for line in "${result[@]}"; do
|
|
77
|
+
log_info "$line"
|
|
78
|
+
done
|
|
79
|
+
|
|
80
|
+
if [[ "$DRY_RUN" == "true" ]]; then
|
|
81
|
+
log_info "Dry run: nothing was written. Run: make setup-update"
|
|
82
|
+
else
|
|
83
|
+
log_success "Updated. Put your own settings back from the copies in .dev-tools-backup/, then run: make setup-check"
|
|
84
|
+
fi
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
#
|
|
3
|
+
# Zairakai NPM Dev Tools - Package Uninstall Script
|
|
4
|
+
# Removes the files installed by setup-project.sh, and only the ones that are still what the package publishes.
|
|
5
|
+
#
|
|
6
|
+
# Usage:
|
|
7
|
+
# bash uninstall-package.sh # Interactive mode (shows the files, asks before removing)
|
|
8
|
+
# bash uninstall-package.sh --dry-run # Lists the files, removes nothing
|
|
9
|
+
# bash uninstall-package.sh --force # Removes without asking, the files that the project changed included
|
|
10
|
+
#
|
|
11
|
+
|
|
12
|
+
set -euo pipefail
|
|
13
|
+
|
|
14
|
+
# Bootstrap
|
|
15
|
+
# ================
|
|
16
|
+
|
|
17
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
18
|
+
# shellcheck disable=SC1091
|
|
19
|
+
source "${SCRIPT_DIR}/config.sh"
|
|
20
|
+
# shellcheck source=setup-state.sh
|
|
21
|
+
source "${SCRIPT_DIR}/setup-state.sh"
|
|
22
|
+
|
|
23
|
+
# Arguments Parsing
|
|
24
|
+
# ================
|
|
25
|
+
|
|
26
|
+
FORCE_MODE=false
|
|
27
|
+
# DRY_RUN=true in the environment does the same as --dry-run (`DRY_RUN=true make uninstall`)
|
|
28
|
+
DRY_RUN="${DRY_RUN:-false}"
|
|
29
|
+
|
|
30
|
+
for arg in "$@"; do
|
|
31
|
+
case "$arg" in
|
|
32
|
+
--force|-f)
|
|
33
|
+
FORCE_MODE=true
|
|
34
|
+
;;
|
|
35
|
+
--dry-run|-n)
|
|
36
|
+
DRY_RUN=true
|
|
37
|
+
;;
|
|
38
|
+
--help|-h)
|
|
39
|
+
echo "Usage: $0 [--dry-run] [--force]"
|
|
40
|
+
echo ""
|
|
41
|
+
echo "Removes the files installed by setup-project.sh that are still what the package publishes:"
|
|
42
|
+
echo " - Makefile and .editorconfig"
|
|
43
|
+
echo " - the thin files of the root (the bridges of the editors, .markdownlint.json) that point at config/dev-tools/"
|
|
44
|
+
echo " - the copies of the ignore files of the root"
|
|
45
|
+
echo " - config/dev-tools/ (the files that the project changed only after confirmation)"
|
|
46
|
+
echo " - the git hooks that the setup linked"
|
|
47
|
+
echo ""
|
|
48
|
+
echo "A file that the project changed is kept and named. Everything that is removed is copied first in"
|
|
49
|
+
echo ".dev-tools-backup/<date>/, under its path from the root, and that folder is kept: to put everything"
|
|
50
|
+
echo "back, copy it at the root (cp -R .dev-tools-backup/<date>/. .)."
|
|
51
|
+
echo ""
|
|
52
|
+
echo "Options:"
|
|
53
|
+
echo " --dry-run, -n List the files, remove nothing"
|
|
54
|
+
echo " --force, -f Remove without confirmation, the files of config/dev-tools/ that were changed included"
|
|
55
|
+
echo " --help, -h Show this help message"
|
|
56
|
+
exit 0
|
|
57
|
+
;;
|
|
58
|
+
*)
|
|
59
|
+
log_error "Unknown option: $arg"
|
|
60
|
+
echo "Use --help for usage information"
|
|
61
|
+
exit 1
|
|
62
|
+
;;
|
|
63
|
+
esac
|
|
64
|
+
done
|
|
65
|
+
|
|
66
|
+
# Helpers
|
|
67
|
+
# ================
|
|
68
|
+
|
|
69
|
+
# What the uninstall does with a file of the setup that the project has, one line per file: "remove: <file>" when the file
|
|
70
|
+
# is still what the package publishes, "keep: <file> (<reason>)" when the project changed it. $2 is true to remove the
|
|
71
|
+
# files of config/dev-tools/ that the project changed too.
|
|
72
|
+
plan() {
|
|
73
|
+
local temporary="$1" customized="$2" state file
|
|
74
|
+
|
|
75
|
+
while IFS='|' read -r state file _; do
|
|
76
|
+
[[ "$state" == "missing" ]] && continue
|
|
77
|
+
|
|
78
|
+
local mine="${PROJECT_ROOT}/${file}"
|
|
79
|
+
local generated="${temporary}/${file}"
|
|
80
|
+
local ours=false
|
|
81
|
+
local reason="customized"
|
|
82
|
+
|
|
83
|
+
# A symbolic link is the project's: it stays, and no copy of it is kept
|
|
84
|
+
if [[ -L "$mine" ]]; then
|
|
85
|
+
echo "keep: ${file} (symbolic link)"
|
|
86
|
+
continue
|
|
87
|
+
fi
|
|
88
|
+
|
|
89
|
+
case "$file" in
|
|
90
|
+
Makefile)
|
|
91
|
+
# The Makefile that the setup generated goes; one with the project's own targets stays
|
|
92
|
+
cmp -s "$mine" "$generated" && ours=true
|
|
93
|
+
;;
|
|
94
|
+
*.config.mjs | .markdownlint.json)
|
|
95
|
+
# A thin file points at config/dev-tools/: without it, it would point at nothing
|
|
96
|
+
grep -qF "config/dev-tools/" "$mine" && ours=true
|
|
97
|
+
;;
|
|
98
|
+
config/dev-tools/*)
|
|
99
|
+
if [[ "$state" == "same" ]] || [[ "$customized" == "true" ]]; then
|
|
100
|
+
ours=true
|
|
101
|
+
fi
|
|
102
|
+
;;
|
|
103
|
+
*)
|
|
104
|
+
[[ "$state" == "same" ]] && ours=true
|
|
105
|
+
;;
|
|
106
|
+
esac
|
|
107
|
+
|
|
108
|
+
if [[ "$ours" == "true" ]]; then
|
|
109
|
+
echo "remove: ${file}"
|
|
110
|
+
else
|
|
111
|
+
echo "keep: ${file} (${reason})"
|
|
112
|
+
fi
|
|
113
|
+
done < <(setup_results "$temporary")
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
# The git hooks that the setup linked (install-hooks.sh links the hooks of the package in .git/hooks)
|
|
117
|
+
linked_hooks() {
|
|
118
|
+
local hook count=0
|
|
119
|
+
|
|
120
|
+
for hook in commit-msg prepare-commit-msg pre-commit pre-push; do
|
|
121
|
+
[[ -L "${PROJECT_ROOT}/.git/hooks/${hook}" ]] && count=$((count + 1))
|
|
122
|
+
done
|
|
123
|
+
|
|
124
|
+
echo "$count"
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
remove_hooks() {
|
|
128
|
+
local count
|
|
129
|
+
count="$(linked_hooks)"
|
|
130
|
+
[[ "$count" -gt 0 ]] || return 0
|
|
131
|
+
|
|
132
|
+
if [[ "$DRY_RUN" == "true" ]]; then
|
|
133
|
+
log_info " would remove the ${count} git hook(s) that the setup linked"
|
|
134
|
+
else
|
|
135
|
+
bash "${SCRIPT_DIR}/install-hooks.sh" --remove > /dev/null
|
|
136
|
+
log_success " Removed the ${count} git hook(s) that the setup linked"
|
|
137
|
+
fi
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
# Main
|
|
141
|
+
# ================
|
|
142
|
+
|
|
143
|
+
# The folder of the generation: a global one, so the trap that removes it still sees it when the script ends
|
|
144
|
+
temporary=""
|
|
145
|
+
trap 'rm -rf "$temporary"' EXIT
|
|
146
|
+
|
|
147
|
+
uninstall_package() {
|
|
148
|
+
log_header "🗑️ Package Development Uninstall"
|
|
149
|
+
|
|
150
|
+
local lines changed line response
|
|
151
|
+
temporary="$(mktemp -d)"
|
|
152
|
+
|
|
153
|
+
setup_generate "$temporary"
|
|
154
|
+
|
|
155
|
+
lines="$(plan "$temporary" "$FORCE_MODE")"
|
|
156
|
+
|
|
157
|
+
if [[ -z "$lines" ]]; then
|
|
158
|
+
log_info "Nothing to remove: no file of the setup is left"
|
|
159
|
+
remove_hooks
|
|
160
|
+
exit 0
|
|
161
|
+
fi
|
|
162
|
+
|
|
163
|
+
# With --force the removal itself lists the files: the plan is shown to ask, or for a dry run
|
|
164
|
+
if [[ "$FORCE_MODE" != "true" ]] || [[ "$DRY_RUN" == "true" ]]; then
|
|
165
|
+
while IFS= read -r line; do
|
|
166
|
+
case "$line" in
|
|
167
|
+
remove:*) log_info " ${line}" ;;
|
|
168
|
+
keep:*) log_warning " ${line}" ;;
|
|
169
|
+
esac
|
|
170
|
+
done <<< "$lines"
|
|
171
|
+
echo ""
|
|
172
|
+
fi
|
|
173
|
+
|
|
174
|
+
# The files of config/dev-tools/ that the project changed: removing them loses what the project wrote
|
|
175
|
+
changed="$(grep -c '^keep: config/dev-tools/' <<< "$lines" || true)"
|
|
176
|
+
|
|
177
|
+
if [[ "$DRY_RUN" == "true" ]]; then
|
|
178
|
+
log_info "Dry run: nothing was removed"
|
|
179
|
+
remove_hooks
|
|
180
|
+
exit 0
|
|
181
|
+
fi
|
|
182
|
+
|
|
183
|
+
local customized="$FORCE_MODE"
|
|
184
|
+
|
|
185
|
+
if [[ "$FORCE_MODE" != "true" ]]; then
|
|
186
|
+
read -r -p "Remove these files? [y/N] " response
|
|
187
|
+
[[ "$response" =~ ^[Yy]$ ]] || {
|
|
188
|
+
log_info "Uninstall cancelled"
|
|
189
|
+
exit 0
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if [[ "$changed" -gt 0 ]]; then
|
|
193
|
+
echo ""
|
|
194
|
+
read -r -p "Remove the ${changed} file(s) of config/dev-tools/ that were changed too (a copy is kept)? [y/N] " response
|
|
195
|
+
[[ "$response" =~ ^[Yy]$ ]] && customized=true
|
|
196
|
+
fi
|
|
197
|
+
|
|
198
|
+
lines="$(plan "$temporary" "$customized")"
|
|
199
|
+
fi
|
|
200
|
+
|
|
201
|
+
echo ""
|
|
202
|
+
log_step "Removing…"
|
|
203
|
+
|
|
204
|
+
while IFS= read -r line; do
|
|
205
|
+
case "$line" in
|
|
206
|
+
remove:*)
|
|
207
|
+
# Everything that goes is copied first: the folder of the backup is what puts it back
|
|
208
|
+
setup_backup "${PROJECT_ROOT}/${line#remove: }"
|
|
209
|
+
rm -f "${PROJECT_ROOT}/${line#remove: }"
|
|
210
|
+
log_success " ${line}"
|
|
211
|
+
;;
|
|
212
|
+
keep:*) log_warning " ${line}" ;;
|
|
213
|
+
esac
|
|
214
|
+
done <<< "$lines"
|
|
215
|
+
|
|
216
|
+
# The folder of the configurations goes with its last file
|
|
217
|
+
rmdir "${PROJECT_ROOT}/config/dev-tools" 2> /dev/null || true
|
|
218
|
+
|
|
219
|
+
remove_hooks
|
|
220
|
+
|
|
221
|
+
echo ""
|
|
222
|
+
log_header "✅ Uninstall completed"
|
|
223
|
+
echo ""
|
|
224
|
+
|
|
225
|
+
if [[ -n "$BACKUP_DIR" ]]; then
|
|
226
|
+
log_info "Everything that was removed is copied in ${BACKUP_DIR#"${PROJECT_ROOT}"/}/"
|
|
227
|
+
log_info "To put it back: cp -R ${BACKUP_DIR#"${PROJECT_ROOT}"/}/. ."
|
|
228
|
+
echo ""
|
|
229
|
+
fi
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
# Execute
|
|
233
|
+
# ================
|
|
234
|
+
|
|
235
|
+
uninstall_package
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Automatic updates of the dependencies of a package of the zairakai group.
|
|
4
4
|
*
|
|
5
|
-
* Usage: node update-dependencies.mjs update | release | tag | cascade | audit | policy | policy-validate | policy-check <ref> | dependabot-sync
|
|
5
|
+
* Usage: node update-dependencies.mjs update | release | tag | cascade | audit | audit-check <report> | policy | policy-validate | policy-check <ref> | dependabot-sync
|
|
6
6
|
*
|
|
7
7
|
* - update: prepare a branch with the minor and patch updates, open a merge request into develop and merge it when its
|
|
8
8
|
* pipeline succeeds. The pipeline of the merge request is the gate: nothing is tested here.
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
* - audit: look for security advisories, open one issue per advisory and, with AUTO_FIX=true, start an update to fix them
|
|
13
13
|
* (UPDATE_URGENT=true: the new versions are taken without waiting, `npm audit fix` is run and an override that pins a
|
|
14
14
|
* vulnerable version is raised).
|
|
15
|
+
* - audit-check: read the report of `npm audit --json` and fail on an advisory that the daily audit fixes at once (high or
|
|
16
|
+
* critical): the same level as `audit`, for the blocking audit of every pipeline (scripts/security-audit.sh). An unknown
|
|
17
|
+
* severity counts as a medium one: it does not block.
|
|
15
18
|
* - policy: in the repository dependency-policy, lift the entries whose condition is met (merge request).
|
|
16
19
|
* - policy-validate: in the repository dependency-policy, check the shape of the files.
|
|
17
20
|
* - policy-check: fail when the change since a ref updates a package of the policy (GitLab or GitHub, it only reads).
|
|
@@ -589,9 +592,9 @@ export function advisoryTitle(row) {
|
|
|
589
592
|
return `${ADVISORY_TITLE_PREFIX}${row.package} (${row.id})`
|
|
590
593
|
}
|
|
591
594
|
|
|
592
|
-
/** Whether an advisory is fixed without waiting for the next weekly update. An unknown severity counts as
|
|
595
|
+
/** Whether an advisory is fixed without waiting for the next weekly update. An unknown severity counts as a medium one: it is not. */
|
|
593
596
|
export function isUrgent(severity) {
|
|
594
|
-
return ['
|
|
597
|
+
return ['high', 'critical'].includes(severity)
|
|
595
598
|
}
|
|
596
599
|
|
|
597
600
|
/** Whether an advisory is worth a push notification: moderate and above. An unknown severity counts as notable. */
|
|
@@ -599,6 +602,30 @@ export function isNotable(severity) {
|
|
|
599
602
|
return ['', 'moderate', 'high', 'critical'].includes(severity)
|
|
600
603
|
}
|
|
601
604
|
|
|
605
|
+
/**
|
|
606
|
+
* The audit that blocks a pipeline, from the JSON report of `npm audit`: an advisory blocks at the level that the daily
|
|
607
|
+
* audit fixes at once (isUrgent: high or critical). The others, an unknown severity included (it counts as a medium
|
|
608
|
+
* one), are listed and do not block. A report without
|
|
609
|
+
* the list of vulnerabilities is not an audit: npm could not run.
|
|
610
|
+
*/
|
|
611
|
+
export function auditGate(report) {
|
|
612
|
+
if (
|
|
613
|
+
typeof report?.vulnerabilities !== 'object' ||
|
|
614
|
+
report.vulnerabilities === null
|
|
615
|
+
) {
|
|
616
|
+
throw new Error(
|
|
617
|
+
'npm audit could not run: its report has no list of vulnerabilities',
|
|
618
|
+
)
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
const rows = parseNpmAudit(report)
|
|
622
|
+
|
|
623
|
+
return {
|
|
624
|
+
blocking: rows.filter((row) => isUrgent(row.severity)),
|
|
625
|
+
other: rows.filter((row) => !isUrgent(row.severity)),
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
602
629
|
/**
|
|
603
630
|
* The one notification of an audit, or null when nothing notable was opened. The advisories of a low severity only
|
|
604
631
|
* get their issue: they are counted, not listed.
|
|
@@ -1596,12 +1623,35 @@ async function dependabotSync() {
|
|
|
1596
1623
|
)
|
|
1597
1624
|
}
|
|
1598
1625
|
|
|
1626
|
+
/** The audit of a pipeline: reads the report of `npm audit --json`, and fails on an advisory of the urgent level. */
|
|
1627
|
+
function auditCheck(_config, file) {
|
|
1628
|
+
if (!file)
|
|
1629
|
+
throw new Error(
|
|
1630
|
+
'Usage: node update-dependencies.mjs audit-check <report of npm audit --json>',
|
|
1631
|
+
)
|
|
1632
|
+
|
|
1633
|
+
const { blocking, other } = auditGate(JSON.parse(readFileSync(file, 'utf8')))
|
|
1634
|
+
const line = (row) =>
|
|
1635
|
+
`${row.package} (${row.id}, ${row.severity || 'unknown'}): ${row.link}`
|
|
1636
|
+
|
|
1637
|
+
for (const row of other) console.info(`Not blocking: ${line(row)}`)
|
|
1638
|
+
for (const row of blocking) console.error(`Blocking: ${line(row)}`)
|
|
1639
|
+
|
|
1640
|
+
if (blocking.length > 0) {
|
|
1641
|
+
process.exitCode = 1
|
|
1642
|
+
return
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
console.info('No advisory at the high level or above.')
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1599
1648
|
const modes = {
|
|
1600
1649
|
update,
|
|
1601
1650
|
release,
|
|
1602
1651
|
tag,
|
|
1603
1652
|
cascade,
|
|
1604
1653
|
audit,
|
|
1654
|
+
'audit-check': auditCheck,
|
|
1605
1655
|
policy: policyWatch,
|
|
1606
1656
|
'policy-validate': policyValidate,
|
|
1607
1657
|
'policy-check': policyCheck,
|
|
@@ -1611,7 +1661,7 @@ const modes = {
|
|
|
1611
1661
|
export async function main(mode, argument) {
|
|
1612
1662
|
if (!modes[mode]) {
|
|
1613
1663
|
console.error(
|
|
1614
|
-
'Usage: node update-dependencies.mjs update | release | tag | cascade | audit | policy | policy-validate',
|
|
1664
|
+
'Usage: node update-dependencies.mjs update | release | tag | cascade | audit | audit-check <report> | policy | policy-validate',
|
|
1615
1665
|
)
|
|
1616
1666
|
process.exitCode = 1
|
|
1617
1667
|
return
|
package/tools/make/bats.mk
CHANGED
|
@@ -4,16 +4,11 @@
|
|
|
4
4
|
## —— 🧪 Shell Script Testing ——
|
|
5
5
|
.PHONY: bats
|
|
6
6
|
bats:: ## Run shell script tests (BATS)
|
|
7
|
-
@if !
|
|
8
|
-
echo "
|
|
9
|
-
exit
|
|
10
|
-
fi; \
|
|
11
|
-
FILES=$$(find tests/bats -name "*.bats" 2>/dev/null); \
|
|
12
|
-
if [ -z "$$FILES" ]; then \
|
|
13
|
-
echo "ℹ️ No BATS tests found, skipping"; \
|
|
14
|
-
else \
|
|
15
|
-
bats $$FILES; \
|
|
7
|
+
@if [ ! -d tests/bats ]; then \
|
|
8
|
+
echo "ℹ️ No BATS tests found - skipping"; \
|
|
9
|
+
exit 0; \
|
|
16
10
|
fi
|
|
11
|
+
@bash $(NPM_DIRECTORY_TOOLS_SCRIPTS_DIR)/bats-run.sh
|
|
17
12
|
|
|
18
13
|
.PHONY: bats-unit
|
|
19
14
|
bats-unit:: ## Run BATS unit tests
|
|
@@ -16,7 +16,7 @@ git-update: ## Fast-forward all local branches that track a remote
|
|
|
16
16
|
@bash $(NPM_DIRECTORY_TOOLS_PACKAGE_ROOT)/tools/scripts/git-update.sh
|
|
17
17
|
|
|
18
18
|
.PHONY: git-cleanup
|
|
19
|
-
git-cleanup: ## Remove local branches whose remote
|
|
19
|
+
git-cleanup: ## Remove merged local branches whose remote is gone
|
|
20
20
|
@bash $(NPM_DIRECTORY_TOOLS_PACKAGE_ROOT)/tools/scripts/git-cleanup.sh
|
|
21
21
|
|
|
22
22
|
.PHONY: git-rebase
|
package/tools/make/utils.mk
CHANGED
|
@@ -26,3 +26,20 @@ package-validate: ## Validate package.json and lockfile sync
|
|
|
26
26
|
.PHONY: outdated
|
|
27
27
|
outdated: ## Check for outdated npm dependencies
|
|
28
28
|
@bash $(NPM_DIRECTORY_TOOLS_SCRIPTS_DIR)/outdated.sh
|
|
29
|
+
|
|
30
|
+
# Double colon: in a full-stack project, the targets of laravel-dev-tools and the ones of this package both run
|
|
31
|
+
.PHONY: security-audit
|
|
32
|
+
security-audit:: ## Run npm security audit
|
|
33
|
+
@bash $(NPM_DIRECTORY_TOOLS_SCRIPTS_DIR)/security-audit.sh
|
|
34
|
+
|
|
35
|
+
.PHONY: setup-check
|
|
36
|
+
setup-check:: ## Tell which files of the setup are missing or differ from the package (writes nothing)
|
|
37
|
+
@bash $(NPM_DIRECTORY_TOOLS_SCRIPTS_DIR)/setup-check.sh
|
|
38
|
+
|
|
39
|
+
.PHONY: setup-update
|
|
40
|
+
setup-update:: ## Replace the files that setup-check reports
|
|
41
|
+
@bash $(NPM_DIRECTORY_TOOLS_SCRIPTS_DIR)/setup-update.sh
|
|
42
|
+
|
|
43
|
+
.PHONY: uninstall
|
|
44
|
+
uninstall:: ## Remove the setup files (copy kept in .dev-tools-backup/)
|
|
45
|
+
@bash $(NPM_DIRECTORY_TOOLS_SCRIPTS_DIR)/uninstall-package.sh
|
|
@@ -3,7 +3,7 @@ set -euo pipefail
|
|
|
3
3
|
|
|
4
4
|
echo ""
|
|
5
5
|
echo "==== Git Branch Cleanup ===="
|
|
6
|
-
echo "This script will
|
|
6
|
+
echo "This script will delete the local branches whose remote has been deleted, when they are merged."
|
|
7
7
|
echo ""
|
|
8
8
|
|
|
9
9
|
# List local branches whose remote has been deleted
|
|
@@ -14,14 +14,14 @@ if [ -z "$gone_branches" ]; then
|
|
|
14
14
|
exit 0
|
|
15
15
|
fi
|
|
16
16
|
|
|
17
|
-
current_branch=$(git symbolic-ref --short HEAD)
|
|
17
|
+
current_branch=$(git symbolic-ref --short HEAD 2> /dev/null || true)
|
|
18
18
|
|
|
19
19
|
echo "Found branches with deleted remotes:"
|
|
20
20
|
echo "━━━━━━━━━━━━━━━━"
|
|
21
21
|
echo "$gone_branches"
|
|
22
22
|
echo "━━━━━━━━━━━━━━━━"
|
|
23
23
|
echo ""
|
|
24
|
-
echo "
|
|
24
|
+
echo "A branch that is not merged is kept: its commits would be lost."
|
|
25
25
|
echo ""
|
|
26
26
|
|
|
27
27
|
# Count branches
|
|
@@ -30,7 +30,7 @@ echo "Total: $count branch(es) to delete"
|
|
|
30
30
|
echo ""
|
|
31
31
|
|
|
32
32
|
# Ask for confirmation
|
|
33
|
-
read -rp "Continue? [y/N] " -n 1
|
|
33
|
+
read -rp "Continue? [y/N] " -n 1 || true
|
|
34
34
|
echo ""
|
|
35
35
|
|
|
36
36
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
@@ -40,14 +40,25 @@ fi
|
|
|
40
40
|
|
|
41
41
|
echo ""
|
|
42
42
|
echo "==== Deleting branches ===="
|
|
43
|
+
kept=()
|
|
43
44
|
for branch in $gone_branches; do
|
|
44
|
-
if [ "$branch"
|
|
45
|
-
echo "Deleting: $branch"
|
|
46
|
-
git branch -D "$branch"
|
|
47
|
-
else
|
|
45
|
+
if [ "$branch" = "$current_branch" ]; then
|
|
48
46
|
echo "⚠️ Skipping $branch (currently checked out)"
|
|
47
|
+
elif git branch -d "$branch" > /dev/null 2>&1; then
|
|
48
|
+
echo "Deleted: $branch"
|
|
49
|
+
else
|
|
50
|
+
echo "Kept (not merged): $branch"
|
|
51
|
+
kept+=("$branch")
|
|
49
52
|
fi
|
|
50
53
|
done
|
|
51
54
|
|
|
55
|
+
if [ ${#kept[@]} -gt 0 ]; then
|
|
56
|
+
echo ""
|
|
57
|
+
echo "These branches hold commits that are in no other branch. To delete one anyway:"
|
|
58
|
+
for branch in "${kept[@]}"; do
|
|
59
|
+
echo " git branch -D $branch"
|
|
60
|
+
done
|
|
61
|
+
fi
|
|
62
|
+
|
|
52
63
|
echo ""
|
|
53
64
|
echo "==== Cleanup completed ===="
|
|
@@ -62,6 +62,12 @@ fi
|
|
|
62
62
|
|
|
63
63
|
# Resolve selected branch
|
|
64
64
|
total_local=${#local_branches[@]}
|
|
65
|
+
total=$((total_local + ${#remote_branches[@]}))
|
|
66
|
+
|
|
67
|
+
if (( choice < 1 || choice > total )); then
|
|
68
|
+
echo "Invalid choice: enter a number from 1 to $total."
|
|
69
|
+
exit 1
|
|
70
|
+
fi
|
|
65
71
|
|
|
66
72
|
if (( choice <= total_local )); then
|
|
67
73
|
target_branch="${local_branches[$((choice-1))]}"
|
|
@@ -15,21 +15,25 @@ current_branch=$(git symbolic-ref --short HEAD)
|
|
|
15
15
|
echo ""
|
|
16
16
|
echo "==== Updating local branches ===="
|
|
17
17
|
for branch in $(git for-each-ref --format='%(refname:short)' refs/heads/); do
|
|
18
|
-
upstream=$(git rev-parse --abbrev-ref "$branch@{upstream}" 2
|
|
19
|
-
if [ -
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
upstream=$(git rev-parse --abbrev-ref "$branch@{upstream}" 2> /dev/null || true)
|
|
19
|
+
if [ -z "$upstream" ]; then
|
|
20
|
+
continue
|
|
21
|
+
fi
|
|
22
|
+
|
|
23
|
+
echo "---- Updating $branch from $upstream ----"
|
|
24
|
+
# Fast-forward only, without a checkout: a branch that is checked out in another worktree, or that diverged, is skipped
|
|
25
|
+
if [ "$branch" = "$current_branch" ]; then
|
|
26
|
+
update=(git merge --ff-only "$upstream")
|
|
27
|
+
else
|
|
28
|
+
update=(git fetch . "$upstream:$branch")
|
|
28
29
|
fi
|
|
29
|
-
done
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
if "${update[@]}"; then
|
|
32
|
+
echo "Updated $branch ✅"
|
|
33
|
+
else
|
|
34
|
+
echo "Cannot fast-forward $branch (diverged or checked out elsewhere). Skipping."
|
|
35
|
+
fi
|
|
36
|
+
done
|
|
33
37
|
|
|
34
38
|
echo ""
|
|
35
39
|
echo "==== Update completed ===="
|