1#!/bin/sh 2set -e 3# Shell script to update cjs-module-lexer in the source tree to a specific version 4 5BASE_DIR=$(cd "$(dirname "$0")/../.." && pwd) 6 7DEPS_DIR="$BASE_DIR/deps" 8[ -z "$NODE" ] && NODE="$BASE_DIR/out/Release/node" 9[ -x "$NODE" ] || NODE=$(command -v node) 10 11NPM="$DEPS_DIR/npm/bin/npm-cli.js" 12 13NEW_VERSION="$("$NODE" --input-type=module <<'EOF' 14const res = await fetch('https://api.github.com/repos/nodejs/cjs-module-lexer/tags'); 15if (!res.ok) throw new Error(`FetchError: ${res.status} ${res.statusText}`, { cause: res }); 16const tags = await res.json(); 17const { name } = tags.at(0) 18console.log(name); 19EOF 20)" 21 22CURRENT_VERSION=$("$NODE" -p "require('./deps/cjs-module-lexer/package.json').version") 23 24echo "Comparing $NEW_VERSION with $CURRENT_VERSION" 25 26if [ "$NEW_VERSION" = "$CURRENT_VERSION" ]; then 27 echo "Skipped because cjs-module-lexer is on the latest version." 28 exit 0 29fi 30 31echo "Making temporary workspace" 32 33WORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp') 34 35cleanup () { 36 EXIT_CODE=$? 37 [ -d "$WORKSPACE" ] && rm -rf "$WORKSPACE" 38 exit $EXIT_CODE 39} 40 41trap cleanup INT TERM EXIT 42 43cd "$WORKSPACE" 44 45"$NODE" "$NPM" init --yes 46 47"$NODE" "$NPM" install --global-style --no-bin-links --ignore-scripts "cjs-module-lexer@$NEW_VERSION" 48 49rm -rf "$DEPS_DIR/cjs-module-lexer" 50 51mv node_modules/cjs-module-lexer "$DEPS_DIR/cjs-module-lexer" 52 53echo "All done!" 54echo "" 55echo "Please git add cjs-module-lexer, commit the new version:" 56echo "" 57echo "$ git add -A deps/cjs-module-lexer src/cjs_module_lexer_version.h" 58echo "$ git commit -m \"deps: update cjs-module-lexer to $NEW_VERSION\"" 59echo "" 60 61# update cjs_module_lexer_version.h 62cat > "$BASE_DIR/src/cjs_module_lexer_version.h" << EOL 63// This is an auto generated file, please do not edit. 64// Refer to tools/dep_updaters/update-cjs-module-lexer.sh 65#ifndef SRC_CJS_MODULE_LEXER_VERSION_H_ 66#define SRC_CJS_MODULE_LEXER_VERSION_H_ 67#define CJS_MODULE_LEXER_VERSION "$NEW_VERSION" 68#endif // SRC_CJS_MODULE_LEXER_VERSION_H_ 69EOL 70 71# The last line of the script should always print the new version, 72# as we need to add it to $GITHUB_ENV variable. 73echo "NEW_VERSION=$NEW_VERSION" 74