Skip to content

Commit

Permalink
Lint build rework (#196)
Browse files Browse the repository at this point in the history
This PR reworks how the linter is built, such that it's no longer ad-hoc
and is done using the same tooling as is used for the main build.
  • Loading branch information
danfuzz authored Oct 31, 2023
2 parents fe63836 + 17475fe commit 967bc71
Show file tree
Hide file tree
Showing 7 changed files with 1,593 additions and 50 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ Breaking changes:
note that the ESLint update (see below) _did_ require some code tweakage.

Other notable changes:
* Updated to latest ESLint. Adjusted comments and code accordingly.
* Lint:
* Updated to latest ESLint. Adjusted comments and code accordingly.
* Un-janked how the linter gets built. It's now built using the same mechanism
as the main application build.

### v0.5.17 -- 2023-10-30

Expand Down
2 changes: 1 addition & 1 deletion scripts/lib/bashy-node/node-project/build-main-module
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ function suspect-file-check {

suspectFiles="$(
lib ls-files --output=array --cd="${dir}" \
--exclude='\.(cjs|js|json|html|md|mjs|ts|yml)$'
--exclude='\.(cjs|js|json|html|md|mjs|pyc|ts|yml)$'
)" \
|| return "$?"

Expand Down
85 changes: 37 additions & 48 deletions scripts/lib/lactoserv/lint
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
. "$(dirname "$(readlink -f "$0")")/_init.sh" || exit "$?"


TOOL_DEPENDENCIES='{
"eslint": "^8.52.0",
"eslint-plugin-jsdoc": "^46.8.2",
"eslint-plugin-jest": "^27.6.0"
}'


#
# Argument parsing
#
Expand Down Expand Up @@ -45,20 +38,43 @@ process-args "$@" || exit "$?"
# Helper functions
#

# Builds the tool.
function build-tool {
local name="$1"
# Finds a hopefully-unique file at the top level of the project whose name has
# the right prefix. Prints it.
function config-file-path {
local configFile

rm -rf node_modules "${name}" *.json
configFile=($(
lib ls-files --output=lines \
--cd="${srcDir}" --full-paths --depth=1 --include='^\.eslintrc'
)) \
|| return "$?"

jval >package.json \
dependencies:json="${TOOL_DEPENDENCIES}" \
'{ dependencies: $dependencies }'
if (( ${#configFile[@]} != 1 )); then
if (( ${#configFile[@]} == 0 )); then
error-msg 'Did not find a config file in the project!'
else
error-msg 'Did not find a _unique_ config file!'
fi
return 1
fi

npm install \
|| return "$?"
echo "${configFile[0]}"
}

# Builds the linter if necessary, and then prints the path to it main binary.
function linter-path {
local toolModule=linter
local toolDir="${outDir}/${toolModule}"
local toolPath="${toolDir}/bin/eslint"

# Build the linter, if necessary.
if [[ ! -x ${toolPath} ]]; then
lib 1>&2 node-project build-main-module \
--out="${outDir}" --modules-dir="${srcDir}" "${toolModule}" \
|| return "$?"
fi

ln -s node_modules/.bin/eslint "${name}"
echo "${toolPath}"
}


Expand All @@ -70,39 +86,14 @@ srcDir="$(base-dir)/src"
outDir="$(lib buildy out-dir --out="${outDir}")" \
|| exit "$?"

toolDir="${outDir}/linter"
toolBinName='lint'
toolBinary="${toolDir}/${toolBinName}"

if [[ ! -x ${toolBinary} ]]; then
mkdir -p "${toolDir}"
(
cd "${toolDir}"
build-tool "${toolBinName}"
) \
|| exit "$?"
fi

# Find a hopefully-unique file at the top level whose name has the right prefix.
configFile=($(
lib ls-files --output=lines \
--cd="${srcDir}" --full-paths --depth=1 --include='^\.eslintrc'
)) \
configFile="$(config-file-path)" \
|| exit "$?"

if (( ${#configFile[@]} != 1 )); then
if (( ${#configFile[@]} == 0 )); then
error-msg 'No ESLint config file!'
else
error-msg 'No unique ESLint config file!'
fi
exit 1
fi
toolPath="$(linter-path)" \
|| exit "$?"

opts=(
--config "${configFile}"
--resolve-plugins-relative-to "${toolDir}"
--cache --cache-location "${toolDir}/cache"
--max-warnings 0
)

Expand All @@ -115,11 +106,9 @@ if (( ${#args[@]} == 0 )); then
args=("${srcDir}")
fi

"${toolBinary}" "${opts[@]}" -- "${args[@]}"
"${toolPath}" "${opts[@]}" -- "${args[@]}"
status="$?"

info-msg

if (( ${status} == 0 )); then
info-msg 'No linter errors! Yay!'
else
Expand Down
10 changes: 10 additions & 0 deletions src/main-linter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@this/main-lint
====================

This is the main module for the linter.

- - - - - - - - - -
```
Copyright 2022-2023 the Lactoserv Authors (Dan Bornstein et alia).
SPDX-License-Identifier: Apache-2.0
```
18 changes: 18 additions & 0 deletions src/main-linter/bin/eslint
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
#
# Copyright 2022-2023 the Lactoserv Authors (Dan Bornstein et alia).
# SPDX-License-Identifier: Apache-2.0

# Figure out the symlink-resolved program name and directory.
cmdName="$(readlink -f "$0")" || exit "$?"
cmdDir="${cmdName%/*}"
cmdName="${cmdName##*/}"
baseDir="${cmdDir%/*}"

# Call through to the `eslint` that got built by the main ESLint dependency,
# prepending options to get it to find the plugins it was built with and to
# put its cache in an appropriate place.
exec "${baseDir}/lib/node_modules/.bin/eslint" \
--resolve-plugins-relative-to "${baseDir}/lib" \
--cache --cache-location "${baseDir}/cache" \
"$@"
Loading

0 comments on commit 967bc71

Please sign in to comment.