11cb0ef41Sopenharmony_ci#!/bin/sh
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci# Shell script to update ESLint in the source tree to the latest release.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci# This script must be in the tools directory when it runs because it uses the
61cb0ef41Sopenharmony_ci# script source file path to determine directories to work in.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciset -ex
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciROOT=$(cd "$(dirname "$0")/../.." && pwd)
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci[ -z "$NODE" ] && NODE="$ROOT/out/Release/node"
131cb0ef41Sopenharmony_ci[ -x "$NODE" ] || NODE=$(command -v node)
141cb0ef41Sopenharmony_ciNPM="$ROOT/deps/npm/bin/npm-cli.js"
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciNEW_VERSION=$("$NODE" "$NPM" view eslint dist-tags.latest)
171cb0ef41Sopenharmony_ciCURRENT_VERSION=$("$NODE" -p "require('./tools/node_modules/eslint/package.json').version")
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciecho "Comparing $NEW_VERSION with $CURRENT_VERSION"
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciif [ "$NEW_VERSION" = "$CURRENT_VERSION" ]; then
221cb0ef41Sopenharmony_ci  echo "Skipped because ESlint is on the latest version."
231cb0ef41Sopenharmony_ci  exit 0
241cb0ef41Sopenharmony_cifi
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cicd "$( dirname "$0" )" || exit
271cb0ef41Sopenharmony_cirm -rf ../node_modules/eslint
281cb0ef41Sopenharmony_ci(
291cb0ef41Sopenharmony_ci    rm -rf eslint-tmp
301cb0ef41Sopenharmony_ci    mkdir eslint-tmp
311cb0ef41Sopenharmony_ci    cd eslint-tmp || exit
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    "$NODE" "$NPM" init --yes
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    "$NODE" "$NPM" install \
361cb0ef41Sopenharmony_ci    --ignore-scripts \
371cb0ef41Sopenharmony_ci    --install-strategy=shallow \
381cb0ef41Sopenharmony_ci    --no-bin-links \
391cb0ef41Sopenharmony_ci    "eslint@$NEW_VERSION"
401cb0ef41Sopenharmony_ci    # Uninstall plugins that we want to install so that they are removed from
411cb0ef41Sopenharmony_ci    # devDependencies. Otherwise --omit=dev will cause them to be skipped.
421cb0ef41Sopenharmony_ci    (
431cb0ef41Sopenharmony_ci        cd node_modules/eslint
441cb0ef41Sopenharmony_ci        "$NODE" "$NPM" uninstall \
451cb0ef41Sopenharmony_ci        --install-links=false \
461cb0ef41Sopenharmony_ci        --ignore-scripts \
471cb0ef41Sopenharmony_ci        eslint-plugin-jsdoc \
481cb0ef41Sopenharmony_ci        eslint-plugin-markdown \
491cb0ef41Sopenharmony_ci        @babel/core \
501cb0ef41Sopenharmony_ci        @babel/eslint-parser \
511cb0ef41Sopenharmony_ci        @babel/plugin-syntax-import-attributes
521cb0ef41Sopenharmony_ci    )
531cb0ef41Sopenharmony_ci    (
541cb0ef41Sopenharmony_ci        cd node_modules/eslint
551cb0ef41Sopenharmony_ci        "$NODE" "$NPM" install \
561cb0ef41Sopenharmony_ci        --ignore-scripts \
571cb0ef41Sopenharmony_ci        --install-links=false \
581cb0ef41Sopenharmony_ci        --no-bin-links \
591cb0ef41Sopenharmony_ci        --no-save \
601cb0ef41Sopenharmony_ci        --omit=dev \
611cb0ef41Sopenharmony_ci        --omit=peer \
621cb0ef41Sopenharmony_ci        eslint-plugin-jsdoc \
631cb0ef41Sopenharmony_ci        eslint-plugin-markdown \
641cb0ef41Sopenharmony_ci        @babel/core \
651cb0ef41Sopenharmony_ci        @babel/eslint-parser \
661cb0ef41Sopenharmony_ci        @babel/plugin-syntax-import-attributes
671cb0ef41Sopenharmony_ci    )
681cb0ef41Sopenharmony_ci    # Use dmn to remove some unneeded files.
691cb0ef41Sopenharmony_ci    "$NODE" "$NPM" exec --package=dmn@2.2.2 --yes -- dmn -f clean
701cb0ef41Sopenharmony_ci    # TODO: Get this into dmn.
711cb0ef41Sopenharmony_ci    find node_modules -name .package-lock.json -exec rm {} \;
721cb0ef41Sopenharmony_ci    find node_modules -name 'README*' -exec rm {} \;
731cb0ef41Sopenharmony_ci)
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_cimv eslint-tmp/node_modules/eslint ../node_modules/eslint
761cb0ef41Sopenharmony_cirm -rf eslint-tmp/
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci# The last line of the script should always print the new version,
791cb0ef41Sopenharmony_ci# as we need to add it to $GITHUB_ENV variable.
801cb0ef41Sopenharmony_ciecho "NEW_VERSION=$NEW_VERSION"
81