Skip to content

Commit fd8ce7b

Browse files
committed
Provide compatibility with Node 18 and a few more little things
We need to support Node 18 from the CI tools (see MDL-75012). So this commit provides that, basically. The main difference is that the `npm bin` command is not available anymore, and that's what we used both for: 1) Search if a given binary has been installed (say, grunt-cli...) 2) Launch a number of binaries (grunt, eslint, gherkin-lint....) So here we are geting rid of all those uses, changing them by: 1) `npm list` to verify if a package is already installed. 2) `npx` to launch the binaries. Also, we have removed some very-old conditional blocks in the code, that were providing compatibility with very, very old Moodle branches, before we used grunt and friends. Finally, we are now installing stylelint-checkstyle-formatter if it's detected that core has not installed it. After all, it's a CI dependency, not a core one, so maybe the more correct thing is to delegate its installation to the CI / testing tools. And, basically, that is. I've tested it with both Node 16 (current master) and Node 18 (MDL-75012) and seems to be working ok. Going to run some tests now...
1 parent 33e6cd2 commit fd8ce7b

File tree

3 files changed

+49
-82
lines changed

3 files changed

+49
-82
lines changed

grunt_process/grunt_process.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ if [[ -n ${npminstall} ]]; then
3838
source ${mydir}/../prepare_npm_stuff/prepare_npm_stuff.sh
3939
fi
4040

41-
# Ensure we have grunt cli available before continue.
42-
gruntcmd="$(${npmcmd} bin)"/grunt
43-
if [ ! -x $gruntcmd ]; then
41+
# Ensure we have grunt binary available before continue.
42+
#
43+
if [[ ! -x node_modules/.bin/grunt ]]; then
4444
echo "Error: grunt executable not found" | tee "${outputfile}"
4545
exitstatus=1
4646
else
@@ -63,7 +63,7 @@ else
6363
fi
6464

6565
set +e
66-
$gruntcmd $tasks --no-color > >(tee "${outputfile}") 2> >(tee "${outputfile}".stderr >&2)
66+
npx grunt $tasks --no-color > >(tee "${outputfile}") 2> >(tee "${outputfile}".stderr >&2)
6767
exitstatus=$?
6868
set -e
6969
fi

prepare_npm_stuff/prepare_npm_stuff.sh

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -90,72 +90,40 @@ if [[ -f ${gitdir}/package.json ]]; then
9090

9191
echo "INFO: Installing npm stuff following package/shrinkwrap details"
9292

93+
if ! hash ${npmcmd} 2>/dev/null; then
94+
echo "ERROR: npm not found in the system. Use .nvmrc OR install it in the PATH"
95+
exit 2
96+
fi
97+
9398
# Always run npm install to keep our npm packages correct
9499
${npmcmd} --no-color install
95100

96-
# Verify there is a grunt executable available, installing if missing
97-
gruntcmd="$(${npmcmd} bin)"/grunt
98-
if [[ ! -f ${gruntcmd} ]]; then
99-
echo "WARN: grunt-cli executable not found. Installing everything"
100-
${npmcmd} --no-color --no-save install grunt-cli
101-
fi
102-
else
103-
104-
# Install shifter version if there is not package.json
105-
# (this is required for branches < 29_STABLE)
106-
shifterinstall=""
107-
shiftercmd="$(${npmcmd} bin)"/shifter
108-
if [[ ! -f ${shiftercmd} ]]; then
109-
echo "WARN: shifter executable not found. Installing it"
110-
shifterinstall=1
111-
else
112-
# Have shifter, look its version matches expected one
113-
# Cannot use --version because it's varying (performing calls to verify latest). Use --help instead
114-
shiftercurrent=$(${shiftercmd} --no-color --help | head -1 | cut -d "@" -f2)
115-
if [[ "${shiftercurrent}" != "${shifterversion}" ]]; then
116-
echo "WARN: shifter executable "${shiftercurrent}" found, "${shifterversion}" expected. Installing it"
117-
shifterinstall=1
118-
else
119-
# All right, shifter found and version matches
120-
echo "INFO: shifter executable (${shifterversion}) found"
101+
# Verify that grunt-cli is available (locally), installing if missing
102+
if ! ${npmcmd} list --parseable | grep -q grunt-cli; then
103+
# Last chance, look for the binary itself.
104+
if [[ ! -x node_modules/.bin/grunt ]]; then
105+
echo "WARN: grunt binary not found. Installing it now"
106+
${npmcmd} --no-color --no-save install grunt-cli
121107
fi
122108
fi
123-
if [[ -n ${shifterinstall} ]]; then
124-
${npmcmd} --no-color install shifter@${shifterversion}
125-
echo "INFO: shifter executable (${shifterversion}) installed"
126-
fi
127109

128-
# Install recess version if there is not package.json
129-
# (this is required for branches < 29_STABLE)
130-
recessinstall=""
131-
recesscmd="$(${npmcmd} bin)"/recess
132-
if [[ ! -f ${recesscmd} ]]; then
133-
echo "WARN: recess executable not found. Installing it"
134-
recessinstall=1
135-
else
136-
# Have recess, look its version matches expected one
137-
recesscurrent=$(${recesscmd} --no-color --version)
138-
if [[ "${recesscurrent}" != "${recessversion}" ]]; then
139-
echo "WARN: recess executable "${recesscurrent}" found, "${recessversion}" expected. Installing it"
140-
recessinstall=1
141-
else
142-
# All right, recess found and version matches
143-
echo "INFO: recess executable (${recessversion}) found"
144-
fi
145-
fi
146-
if [[ -n ${recessinstall} ]]; then
147-
${npmcmd} --no-color install recess@${recessversion}
148-
echo "INFO: recess executable (${recessversion}) installed"
110+
# Verify that stylelint-checkstyle-formatter is available (locally), installing if missing
111+
if ! ${npmcmd} list --parseable | grep -q stylelint-checkstyle-formatter; then
112+
echo "WARN: stylelint-checkstyle-formatter package not found. Installing it now"
113+
${npmcmd} --no-color --no-save install stylelint-checkstyle-formatter
149114
fi
115+
else
116+
echo "ERROR: Something is wrong. Missing package.json"
150117
fi
151118

152119
# Move back to base directory.
153120
cd ${gitdir}
154121

155122
# Output information about installed binaries.
156123
echo "INFO: Installation ended"
157-
echo "INFO: Available binaries @ ${gitdir}"
158-
echo "INFO: (Contents of $(${npmcmd} bin))"
159-
for binary in $(ls $(${npmcmd} bin)); do
160-
echo "INFO: - Installed ${binary}"
124+
echo "INFO: Installed packages @ $(npm root)"
125+
echo "INFO: (Contents of ${npmcmd} list --depth=1)"
126+
for package in $(${npmcmd} list --depth=1 --parseable); do
127+
echo "INFO: - Installed $(basename ${package})"
161128
done
129+
echo "============== END OF LIST =============="

remote_branch_checker/remote_branch_checker.sh

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,14 @@ cat "${WORKSPACE}/work/mustachelint.txt" | ${phpcmd} ${mydir}/checkstyle_convert
370370

371371
if [ -f $WORKSPACE/.gherkin-lintrc ]; then
372372
echo "Info: Running gherkin-lint..."
373-
gherkinlintcmd="$(${npmcmd} bin)"/gherkin-lint
374-
if [ -x $gherkinlintcmd ]; then
375-
$gherkinlintcmd --format=json '**/tests/behat/*.feature' 2> "${WORKSPACE}/work/gherkin-lint.txt"
376-
cat "${WORKSPACE}/work/gherkin-lint.txt" | ${phpcmd} ${mydir}/checkstyle_converter.php --format=gherkinlint > "${WORKSPACE}/work/gherkin-lint.xml"
377-
else
378-
echo "Error: .gherkin-lintrc file found, but /gherkin-lint executable not found" | tee -a ${errorfile}
373+
if ! ${npmcmd} list --depth=1 --parseable | grep -q gherkin-lint; then
374+
echo "Error: .gherkin-lintrc file found, but gherkin-lint package not found" | tee -a ${errorfile}
379375
exit 1
380376
fi
377+
378+
# Run gherkin-lint
379+
npx gherkin-lint --format=json '**/tests/behat/*.feature' 2> "${WORKSPACE}/work/gherkin-lint.txt"
380+
cat "${WORKSPACE}/work/gherkin-lint.txt" | ${phpcmd} ${mydir}/checkstyle_converter.php --format=gherkinlint > "${WORKSPACE}/work/gherkin-lint.xml"
381381
fi
382382

383383
# Run the grunt checker if Gruntfile exists. node stuff has been already installed.
@@ -425,28 +425,27 @@ set +e
425425

426426
if [ -f $WORKSPACE/.eslintrc ]; then
427427
echo "Info: Running eslint..."
428-
eslintcmd="$(${npmcmd} bin)"/eslint
429-
if [ -x $eslintcmd ]; then
430-
# TODO: Remove this once everybody is using nodejs 14 or up.
431-
# We need to invoke eslint differently depending of the installed version.
432-
# (new versions v6.8 and up have this option to avoid exiting with error if there aren't JS files)
433-
eslintarg="--no-error-on-unmatched-pattern"
434-
# Old versions don't have this option, they exit without error if there aren't JS files, so don't use it.
435-
if ! $eslintcmd --help | grep -q -- $eslintarg; then
436-
eslintarg=""
437-
fi
438-
$eslintcmd -f checkstyle $eslintarg $WORKSPACE > "${WORKSPACE}/work/eslint.xml"
439-
else
440-
echo "Error: .eslintrc file found, but eslint executable not found" | tee -a ${errorfile}
428+
if ! ${npmcmd} list --depth=1 --parseable | grep -q eslint; then
429+
echo "Error: .eslintrc file found, but eslint package not found" | tee -a ${errorfile}
441430
exit 1
442431
fi
432+
433+
# Run eslint
434+
# TODO: Remove this once everybody is using nodejs 14 or up.
435+
# We need to invoke eslint differently depending of the installed version.
436+
# (new versions v6.8 and up have this option to avoid exiting with error if there aren't JS files)
437+
eslintarg="--no-error-on-unmatched-pattern"
438+
# Old versions don't have this option, they exit without error if there aren't JS files, so don't use it.
439+
if ! npx eslint --help | grep -q -- $eslintarg; then
440+
eslintarg=""
441+
fi
442+
npx eslint -f checkstyle $eslintarg $WORKSPACE > "${WORKSPACE}/work/eslint.xml"
443443
fi
444444

445445
if [ -f $WORKSPACE/.stylelintrc ]; then
446446
echo "Info: Running stylelint..."
447-
stylelintcmd="$(${npmcmd} bin)"/stylelint
448-
if [ ! -x $stylelintcmd ]; then
449-
echo "Error: .stylelintrc file found, but stylelint executable not found" | tee -a ${errorfile}
447+
if ! ${npmcmd} list --depth=1 --parseable | grep -q stylelint; then
448+
echo "Error: .stylelintrc file found, but stylelint package not found" | tee -a ${errorfile}
450449
exit 1
451450
fi
452451

@@ -456,10 +455,10 @@ if [ -f $WORKSPACE/.stylelintrc ]; then
456455
# (new versions 7.7.0 and up have this option to avoid exiting with error if there aren't CSS files)
457456
stylelintarg="--allow-empty-input"
458457
# Old versions don't have this option, they exit without error if there aren't CSS files, so don't use it.
459-
if ! $stylelintcmd --help | grep -q -- $stylelintarg; then
458+
if ! npx stylelint --help | grep -q -- $stylelintarg; then
460459
eslintarg=""
461460
fi
462-
if $stylelintcmd $stylelintarg --customFormatter 'node_modules/stylelint-checkstyle-formatter' "*/**/*.{css,less,scss}" > "${WORKSPACE}/work/stylelint.xml"
461+
if npx stylelint $stylelintarg --customFormatter 'node_modules/stylelint-checkstyle-formatter' "*/**/*.{css,less,scss}" > "${WORKSPACE}/work/stylelint.xml"
463462
then
464463
echo "Info: stylelint completed without errors."
465464
else

0 commit comments

Comments
 (0)