Introduce retry mechanism for npm install in CI/CD
This commit addresses occasional pipeline failures caused by transient network errors during dependency installation with `npm ci`. It centralizes the logic for installing npm dependencies and introduces a retry mechanism. The new approach will attempt `npm ci` up to 5 times with a 5-second interval between each attempt, thereby increasing the resilience of CI/CD pipelines. This commit adds a new script `npm-install.js` with `npm run install-deps` command to centralize npm dependency installation process throughout the project. Separate testing of scripts to a separate workflow. It removes unused `install` dependency from `package.json`.
This commit is contained in:
11
.github/actions/npm-install-dependencies/action.yml
vendored
Normal file
11
.github/actions/npm-install-dependencies/action.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
inputs:
|
||||||
|
working-directory:
|
||||||
|
required: false
|
||||||
|
default: '.'
|
||||||
|
runs:
|
||||||
|
using: composite
|
||||||
|
steps:
|
||||||
|
-
|
||||||
|
name: Run `npm ci` with retries
|
||||||
|
shell: bash
|
||||||
|
run: npm run install-deps -- --ci --root-directory "${{ inputs.working-directory }}"
|
||||||
24
.github/workflows/checks.build.yaml
vendored
24
.github/workflows/checks.build.yaml
vendored
@@ -27,7 +27,7 @@ jobs:
|
|||||||
uses: ./.github/actions/setup-node
|
uses: ./.github/actions/setup-node
|
||||||
-
|
-
|
||||||
name: Install dependencies
|
name: Install dependencies
|
||||||
run: npm ci
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
-
|
-
|
||||||
name: Build web
|
name: Build web
|
||||||
run: npm run build -- --mode ${{ matrix.mode }}
|
run: npm run build -- --mode ${{ matrix.mode }}
|
||||||
@@ -55,7 +55,7 @@ jobs:
|
|||||||
uses: ./.github/actions/setup-node
|
uses: ./.github/actions/setup-node
|
||||||
-
|
-
|
||||||
name: Install dependencies
|
name: Install dependencies
|
||||||
run: npm ci
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
-
|
-
|
||||||
name: Prebuild desktop
|
name: Prebuild desktop
|
||||||
run: npm run electron:prebuild -- --mode ${{ matrix.mode }}
|
run: npm run electron:prebuild -- --mode ${{ matrix.mode }}
|
||||||
@@ -68,23 +68,3 @@ jobs:
|
|||||||
-
|
-
|
||||||
name: Verify bundled desktop build artifacts
|
name: Verify bundled desktop build artifacts
|
||||||
run: npm run check:verify-build-artifacts -- --electron-bundled
|
run: npm run check:verify-build-artifacts -- --electron-bundled
|
||||||
|
|
||||||
create-icons:
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [ macos, ubuntu, windows ]
|
|
||||||
fail-fast: false # Allows to see results from other combinations
|
|
||||||
runs-on: ${{ matrix.os }}-latest
|
|
||||||
steps:
|
|
||||||
-
|
|
||||||
name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
-
|
|
||||||
name: Setup node
|
|
||||||
uses: ./.github/actions/setup-node
|
|
||||||
-
|
|
||||||
name: Install dependencies
|
|
||||||
run: npm ci
|
|
||||||
-
|
|
||||||
name: Create icons
|
|
||||||
run: npm run icons:build
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ jobs:
|
|||||||
uses: ./.github/actions/setup-node
|
uses: ./.github/actions/setup-node
|
||||||
-
|
-
|
||||||
name: Install dependencies
|
name: Install dependencies
|
||||||
run: npm ci
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
-
|
-
|
||||||
name: Configure Ubuntu
|
name: Configure Ubuntu
|
||||||
if: matrix.os == 'ubuntu'
|
if: matrix.os == 'ubuntu'
|
||||||
|
|||||||
14
.github/workflows/checks.external-urls.yaml
vendored
14
.github/workflows/checks.external-urls.yaml
vendored
@@ -8,11 +8,15 @@ jobs:
|
|||||||
run-check:
|
run-check:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
-
|
||||||
|
name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
- name: Setup node
|
-
|
||||||
|
name: Setup node
|
||||||
uses: ./.github/actions/setup-node
|
uses: ./.github/actions/setup-node
|
||||||
- name: Install dependencies
|
-
|
||||||
run: npm ci
|
name: Install dependencies
|
||||||
- name: Test
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
|
-
|
||||||
|
name: Test
|
||||||
run: npm run check:external-urls
|
run: npm run check:external-urls
|
||||||
|
|||||||
14
.github/workflows/checks.quality.yaml
vendored
14
.github/workflows/checks.quality.yaml
vendored
@@ -16,11 +16,15 @@ jobs:
|
|||||||
os: [ macos, ubuntu, windows ]
|
os: [ macos, ubuntu, windows ]
|
||||||
fail-fast: false # Still interested to see results from other combinations
|
fail-fast: false # Still interested to see results from other combinations
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
-
|
||||||
|
name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
- name: Setup node
|
-
|
||||||
|
name: Setup node
|
||||||
uses: ./.github/actions/setup-node
|
uses: ./.github/actions/setup-node
|
||||||
- name: Install dependencies
|
-
|
||||||
run: npm ci
|
name: Install dependencies
|
||||||
- name: Lint
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
|
-
|
||||||
|
name: Lint
|
||||||
run: ${{ matrix.lint-command }}
|
run: ${{ matrix.lint-command }}
|
||||||
|
|||||||
55
.github/workflows/checks.scripts.yaml
vendored
Normal file
55
.github/workflows/checks.scripts.yaml
vendored
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
name: checks.scripts
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
icons-build:
|
||||||
|
runs-on: ${{ matrix.os }}-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ macos, ubuntu, windows ]
|
||||||
|
fail-fast: false # Still interested to see results from other combinations
|
||||||
|
steps:
|
||||||
|
-
|
||||||
|
name: Checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
-
|
||||||
|
name: Setup node
|
||||||
|
uses: ./.github/actions/setup-node
|
||||||
|
-
|
||||||
|
name: Install dependencies
|
||||||
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
|
-
|
||||||
|
name: Create icons
|
||||||
|
run: npm run icons:build
|
||||||
|
|
||||||
|
install-deps:
|
||||||
|
runs-on: ${{ matrix.os }}-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
install-deps-before: [true, false]
|
||||||
|
install-command:
|
||||||
|
- npm run install-deps
|
||||||
|
- npm run install-deps -- --no-errors
|
||||||
|
- npm run install-deps -- --ci
|
||||||
|
- npm run install-deps -- --fresh --non-deterministic
|
||||||
|
- npm run install-deps -- --fresh
|
||||||
|
- npm run install-deps -- --non-deterministic
|
||||||
|
os: [ macos, ubuntu, windows ]
|
||||||
|
fail-fast: false # Still interested to see results from other combinations
|
||||||
|
steps:
|
||||||
|
-
|
||||||
|
name: Checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
-
|
||||||
|
name: Setup node
|
||||||
|
uses: ./.github/actions/setup-node
|
||||||
|
-
|
||||||
|
name: Install dependencies
|
||||||
|
if: matrix.install-deps-before == true
|
||||||
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
|
-
|
||||||
|
name: Run install-deps
|
||||||
|
run: ${{ matrix.install-command }}
|
||||||
2
.github/workflows/release.desktop.yaml
vendored
2
.github/workflows/release.desktop.yaml
vendored
@@ -26,7 +26,7 @@ jobs:
|
|||||||
uses: ./.github/actions/setup-node
|
uses: ./.github/actions/setup-node
|
||||||
-
|
-
|
||||||
name: Install dependencies
|
name: Install dependencies
|
||||||
run: npm ci
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
-
|
-
|
||||||
name: Run unit tests
|
name: Run unit tests
|
||||||
run: npm run test:unit
|
run: npm run test:unit
|
||||||
|
|||||||
5
.github/workflows/release.site.yaml
vendored
5
.github/workflows/release.site.yaml
vendored
@@ -84,8 +84,9 @@ jobs:
|
|||||||
uses: ./app/.github/actions/setup-node
|
uses: ./app/.github/actions/setup-node
|
||||||
-
|
-
|
||||||
name: "App: Install dependencies"
|
name: "App: Install dependencies"
|
||||||
run: npm ci
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
working-directory: app
|
with:
|
||||||
|
working-directory: app
|
||||||
-
|
-
|
||||||
name: "App: Run unit tests"
|
name: "App: Run unit tests"
|
||||||
run: npm run test:unit
|
run: npm run test:unit
|
||||||
|
|||||||
2
.github/workflows/tests.e2e.yaml
vendored
2
.github/workflows/tests.e2e.yaml
vendored
@@ -20,7 +20,7 @@ jobs:
|
|||||||
uses: ./.github/actions/setup-node
|
uses: ./.github/actions/setup-node
|
||||||
-
|
-
|
||||||
name: Install dependencies
|
name: Install dependencies
|
||||||
run: npm ci
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
-
|
-
|
||||||
name: Run e2e tests
|
name: Run e2e tests
|
||||||
run: npm run test:cy:run
|
run: npm run test:cy:run
|
||||||
|
|||||||
2
.github/workflows/tests.integration.yaml
vendored
2
.github/workflows/tests.integration.yaml
vendored
@@ -22,7 +22,7 @@ jobs:
|
|||||||
uses: ./.github/actions/setup-node
|
uses: ./.github/actions/setup-node
|
||||||
-
|
-
|
||||||
name: Install dependencies
|
name: Install dependencies
|
||||||
run: npm ci
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
-
|
-
|
||||||
name: Run integration tests
|
name: Run integration tests
|
||||||
run: npm run test:integration
|
run: npm run test:integration
|
||||||
|
|||||||
2
.github/workflows/tests.unit.yaml
vendored
2
.github/workflows/tests.unit.yaml
vendored
@@ -20,7 +20,7 @@ jobs:
|
|||||||
uses: ./.github/actions/setup-node
|
uses: ./.github/actions/setup-node
|
||||||
-
|
-
|
||||||
name: Install dependencies
|
name: Install dependencies
|
||||||
run: npm ci
|
uses: ./.github/actions/npm-install-dependencies
|
||||||
-
|
-
|
||||||
name: Run unit tests
|
name: Run unit tests
|
||||||
run: npm run test:unit
|
run: npm run test:unit
|
||||||
|
|||||||
@@ -76,6 +76,12 @@
|
|||||||
src="https://github.com/undergroundwires/privacy.sexy/workflows/checks.desktop-runtime-errors/badge.svg"
|
src="https://github.com/undergroundwires/privacy.sexy/workflows/checks.desktop-runtime-errors/badge.svg"
|
||||||
/>
|
/>
|
||||||
</a>
|
</a>
|
||||||
|
<a href="https://github.com/undergroundwires/privacy.sexy/actions/workflows/checks.scripts.yaml" target="_blank" rel="noopener noreferrer">
|
||||||
|
<img
|
||||||
|
alt="Status of script checks"
|
||||||
|
src="https://github.com/undergroundwires/privacy.sexy/workflows/checks.scripts/badge.svg"
|
||||||
|
/>
|
||||||
|
</a>
|
||||||
<a href="https://github.com/undergroundwires/privacy.sexy/actions/workflows/checks.external-urls.yaml" target="_blank" rel="noopener noreferrer">
|
<a href="https://github.com/undergroundwires/privacy.sexy/actions/workflows/checks.external-urls.yaml" target="_blank" rel="noopener noreferrer">
|
||||||
<img
|
<img
|
||||||
alt="Status of external URL checks"
|
alt="Status of external URL checks"
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ See [ci-cd.md](./ci-cd.md) for more information.
|
|||||||
|
|
||||||
### Prerequisites
|
### Prerequisites
|
||||||
|
|
||||||
- Install node >15.x.
|
- Install Node >16.x.
|
||||||
- Install dependencies using `npm install`.
|
- Install dependencies using `npm install` (or [`npm run install-deps`](#utility-scripts) for more options).
|
||||||
|
|
||||||
### Testing
|
### Testing
|
||||||
|
|
||||||
@@ -73,9 +73,9 @@ See [ci-cd.md](./ci-cd.md) for more information.
|
|||||||
|
|
||||||
#### Utility scripts
|
#### Utility scripts
|
||||||
|
|
||||||
- [**`./scripts/fresh-npm-install.sh`**](../scripts/fresh-npm-install.sh):
|
- [**`npm run install-deps [-- <options>]`**](../scripts/npm-install.js):
|
||||||
- Run fresh NPM install.
|
- Manages NPM dependency installation, it offers capabilities like doing a fresh install, retries on network errors, and other features.
|
||||||
- This script provides a clean NPM install, removing existing node modules and optionally the package-lock.json (when run with -n), then installs dependencies and runs unit tests.
|
- For example, you can run `npm run install-deps -- --fresh` to do clean installation of dependencies.
|
||||||
- [**`./scripts/configure-vscode.sh`**](../scripts/configure-vscode.sh):
|
- [**`./scripts/configure-vscode.sh`**](../scripts/configure-vscode.sh):
|
||||||
- This script checks and sets the necessary configurations for VSCode in `settings.json` file.
|
- This script checks and sets the necessary configurations for VSCode in `settings.json` file.
|
||||||
|
|
||||||
|
|||||||
14
package-lock.json
generated
14
package-lock.json
generated
@@ -19,7 +19,6 @@
|
|||||||
"cross-fetch": "^4.0.0",
|
"cross-fetch": "^4.0.0",
|
||||||
"electron-progressbar": "^2.1.0",
|
"electron-progressbar": "^2.1.0",
|
||||||
"file-saver": "^2.0.5",
|
"file-saver": "^2.0.5",
|
||||||
"install": "^0.13.0",
|
|
||||||
"liquor-tree": "^0.2.70",
|
"liquor-tree": "^0.2.70",
|
||||||
"markdown-it": "^13.0.1",
|
"markdown-it": "^13.0.1",
|
||||||
"npm": "^9.8.1",
|
"npm": "^9.8.1",
|
||||||
@@ -9425,14 +9424,6 @@
|
|||||||
"node": ">=10"
|
"node": ">=10"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/install": {
|
|
||||||
"version": "0.13.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/install/-/install-0.13.0.tgz",
|
|
||||||
"integrity": "sha512-zDml/jzr2PKU9I8J/xyZBQn8rPCAY//UOYNmR01XwNwyfhEWObo2SWfSl1+0tm1u6PhxLwDnfsT/6jB7OUxqFA==",
|
|
||||||
"engines": {
|
|
||||||
"node": ">= 0.10"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/internal-slot": {
|
"node_modules/internal-slot": {
|
||||||
"version": "1.0.5",
|
"version": "1.0.5",
|
||||||
"resolved": "https://registry.npmjs.org/internal-slot/-/internal-slot-1.0.5.tgz",
|
"resolved": "https://registry.npmjs.org/internal-slot/-/internal-slot-1.0.5.tgz",
|
||||||
@@ -28223,11 +28214,6 @@
|
|||||||
"integrity": "sha512-7PnF4oN3CvZF23ADhA5wRaYEQpJ8qygSkbtTXWBeXWXmEVRXK+1ITciHWwHhsjv1TmW0MgacIv6hEi5pX5NQdA==",
|
"integrity": "sha512-7PnF4oN3CvZF23ADhA5wRaYEQpJ8qygSkbtTXWBeXWXmEVRXK+1ITciHWwHhsjv1TmW0MgacIv6hEi5pX5NQdA==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"install": {
|
|
||||||
"version": "0.13.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/install/-/install-0.13.0.tgz",
|
|
||||||
"integrity": "sha512-zDml/jzr2PKU9I8J/xyZBQn8rPCAY//UOYNmR01XwNwyfhEWObo2SWfSl1+0tm1u6PhxLwDnfsT/6jB7OUxqFA=="
|
|
||||||
},
|
|
||||||
"internal-slot": {
|
"internal-slot": {
|
||||||
"version": "1.0.5",
|
"version": "1.0.5",
|
||||||
"resolved": "https://registry.npmjs.org/internal-slot/-/internal-slot-1.0.5.tgz",
|
"resolved": "https://registry.npmjs.org/internal-slot/-/internal-slot-1.0.5.tgz",
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
"test:cy:run": "start-server-and-test \"vite build && vite preview --port 7070\" http://localhost:7070 \"cypress run --config baseUrl=http://localhost:7070\"",
|
"test:cy:run": "start-server-and-test \"vite build && vite preview --port 7070\" http://localhost:7070 \"cypress run --config baseUrl=http://localhost:7070\"",
|
||||||
"test:cy:open": "start-server-and-test \"vite --port 7070 --mode production\" http://localhost:7070 \"cypress open --config baseUrl=http://localhost:7070\"",
|
"test:cy:open": "start-server-and-test \"vite --port 7070 --mode production\" http://localhost:7070 \"cypress open --config baseUrl=http://localhost:7070\"",
|
||||||
"lint": "npm run lint:md && npm run lint:md:consistency && npm run lint:md:relative-urls && npm run lint:eslint && npm run lint:yaml",
|
"lint": "npm run lint:md && npm run lint:md:consistency && npm run lint:md:relative-urls && npm run lint:eslint && npm run lint:yaml",
|
||||||
|
"install-deps": "node scripts/npm-install.js",
|
||||||
"icons:build": "node scripts/logo-update.js",
|
"icons:build": "node scripts/logo-update.js",
|
||||||
"check:desktop": "vitest run --dir tests/checks/desktop-runtime-errors --environment node",
|
"check:desktop": "vitest run --dir tests/checks/desktop-runtime-errors --environment node",
|
||||||
"check:external-urls": "vitest run --dir tests/checks/external-urls --environment node",
|
"check:external-urls": "vitest run --dir tests/checks/external-urls --environment node",
|
||||||
@@ -43,7 +44,6 @@
|
|||||||
"cross-fetch": "^4.0.0",
|
"cross-fetch": "^4.0.0",
|
||||||
"electron-progressbar": "^2.1.0",
|
"electron-progressbar": "^2.1.0",
|
||||||
"file-saver": "^2.0.5",
|
"file-saver": "^2.0.5",
|
||||||
"install": "^0.13.0",
|
|
||||||
"liquor-tree": "^0.2.70",
|
"liquor-tree": "^0.2.70",
|
||||||
"markdown-it": "^13.0.1",
|
"markdown-it": "^13.0.1",
|
||||||
"npm": "^9.8.1",
|
"npm": "^9.8.1",
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
# Description:
|
|
||||||
# This script ensures npm is available, removes existing node modules, optionally
|
|
||||||
# removes package-lock.json (when -n flag is used), installs dependencies and runs unit tests.
|
|
||||||
# Usage:
|
|
||||||
# ./fresh-npm-install.sh # Regular execution
|
|
||||||
# ./fresh-npm-install.sh -n # Non-deterministic mode (removes package-lock.json)
|
|
||||||
|
|
||||||
declare NON_DETERMINISTIC_FLAG=0
|
|
||||||
|
|
||||||
|
|
||||||
main() {
|
|
||||||
parse_args "$@"
|
|
||||||
ensure_npm_is_available
|
|
||||||
ensure_npm_root
|
|
||||||
remove_existing_modules
|
|
||||||
if [[ $NON_DETERMINISTIC_FLAG -eq 1 ]]; then
|
|
||||||
remove_package_lock_json
|
|
||||||
fi
|
|
||||||
install_dependencies
|
|
||||||
run_unit_tests
|
|
||||||
}
|
|
||||||
|
|
||||||
ensure_npm_is_available() {
|
|
||||||
if ! command -v npm &> /dev/null; then
|
|
||||||
log::fatal 'npm could not be found, please install it first.'
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
ensure_npm_root() {
|
|
||||||
if [ ! -f package.json ]; then
|
|
||||||
log::fatal 'Current directory is not a npm root. Please run the script in a npm root directory.'
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
remove_existing_modules() {
|
|
||||||
if [ -d ./node_modules ]; then
|
|
||||||
log::info 'Removing existing node modules...'
|
|
||||||
if ! rm -rf ./node_modules; then
|
|
||||||
log::fatal 'Could not remove existing node modules.'
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
install_dependencies() {
|
|
||||||
log::info 'Installing dependencies...'
|
|
||||||
if ! npm install; then
|
|
||||||
log::fatal 'Failed to install dependencies.'
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
remove_package_lock_json() {
|
|
||||||
if [ -f ./package-lock.json ]; then
|
|
||||||
log::info 'Removing package-lock.json...'
|
|
||||||
if ! rm -rf ./package-lock.json; then
|
|
||||||
log::fatal 'Could not remove package-lock.json.'
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
run_unit_tests() {
|
|
||||||
log::info 'Running unit tests...'
|
|
||||||
if ! npm run test:unit; then
|
|
||||||
pwd
|
|
||||||
log::fatal 'Failed to run unit tests.'
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
log::info() {
|
|
||||||
local -r message="$1"
|
|
||||||
echo "📣 ${message}"
|
|
||||||
}
|
|
||||||
|
|
||||||
log::fatal() {
|
|
||||||
local -r message="$1"
|
|
||||||
echo "❌ ${message}" >&2
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
parse_args() {
|
|
||||||
while getopts "n" opt; do
|
|
||||||
case ${opt} in
|
|
||||||
n)
|
|
||||||
NON_DETERMINISTIC_FLAG=1
|
|
||||||
;;
|
|
||||||
\?)
|
|
||||||
echo "Invalid option: $OPTARG" 1>&2
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
main "$1"
|
|
||||||
199
scripts/npm-install.js
Normal file
199
scripts/npm-install.js
Normal file
@@ -0,0 +1,199 @@
|
|||||||
|
/*
|
||||||
|
Description:
|
||||||
|
This script manages NPM dependencies for a project.
|
||||||
|
It offers capabilities like doing a fresh install, retries on network errors, and other features.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
npm run install-deps [-- <options>]
|
||||||
|
node scripts/npm-install.js [options]
|
||||||
|
|
||||||
|
Options:
|
||||||
|
--root-directory <path>
|
||||||
|
Specifies the root directory where package.json resides
|
||||||
|
Defaults to the current working directory.
|
||||||
|
Example: npm run install-deps -- --root-directory /your/path/here
|
||||||
|
|
||||||
|
--no-errors
|
||||||
|
Ignores errors and continues the execution.
|
||||||
|
Example: npm run install-deps -- --no-errors
|
||||||
|
|
||||||
|
--ci
|
||||||
|
Uses 'npm ci' for dependency installation instead of 'npm install'.
|
||||||
|
Example: npm run install-deps -- --ci
|
||||||
|
|
||||||
|
--fresh
|
||||||
|
Removes the existing node_modules directory before installing dependencies.
|
||||||
|
Example: npm run install-deps -- --fresh
|
||||||
|
|
||||||
|
--non-deterministic
|
||||||
|
Removes package-lock.json for a non-deterministic installation.
|
||||||
|
Example: npm run install-deps -- --non-deterministic
|
||||||
|
|
||||||
|
Note:
|
||||||
|
|
||||||
|
Flags can be combined as needed.
|
||||||
|
Example: npm run install-deps -- --fresh --non-deterministic
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { exec } from 'child_process';
|
||||||
|
import { resolve } from 'path';
|
||||||
|
import { access, rm, unlink } from 'fs/promises';
|
||||||
|
import { constants } from 'fs';
|
||||||
|
|
||||||
|
const MAX_RETRIES = 5;
|
||||||
|
const RETRY_DELAY_IN_MS = 5 /* seconds */ * 1000;
|
||||||
|
const ARG_NAMES = {
|
||||||
|
rootDirectory: '--root-directory',
|
||||||
|
ignoreErrors: '--no-errors',
|
||||||
|
ci: '--ci',
|
||||||
|
fresh: '--fresh',
|
||||||
|
nonDeterministic: '--non-deterministic',
|
||||||
|
};
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const options = getOptions();
|
||||||
|
console.log('Options:', options);
|
||||||
|
await ensureNpmRootDirectory(options.rootDirectory);
|
||||||
|
await ensureNpmIsAvailable();
|
||||||
|
if (options.fresh) {
|
||||||
|
await removeNodeModules(options.rootDirectory);
|
||||||
|
}
|
||||||
|
if (options.nonDeterministic) {
|
||||||
|
await removePackageLockJson(options.rootDirectory);
|
||||||
|
}
|
||||||
|
const command = buildCommand(options.ci, options.outputErrors);
|
||||||
|
console.log('Starting dependency installation...');
|
||||||
|
const exitCode = await executeWithRetry(
|
||||||
|
command,
|
||||||
|
options.workingDirectory,
|
||||||
|
MAX_RETRIES,
|
||||||
|
RETRY_DELAY_IN_MS,
|
||||||
|
);
|
||||||
|
if (exitCode === 0) {
|
||||||
|
console.log('🎊 Installed dependencies...');
|
||||||
|
} else {
|
||||||
|
console.error(`💀 Failed to install dependencies, exit code: ${exitCode}`);
|
||||||
|
}
|
||||||
|
process.exit(exitCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function removeNodeModules(workingDirectory) {
|
||||||
|
const nodeModulesDirectory = resolve(workingDirectory, 'node_modules');
|
||||||
|
if (await exists('./node_modules')) {
|
||||||
|
console.log('Removing node_modules...');
|
||||||
|
await rm(nodeModulesDirectory, { recursive: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function removePackageLockJson(workingDirectory) {
|
||||||
|
const packageLockJsonFile = resolve(workingDirectory, 'package-lock.json');
|
||||||
|
if (await exists(packageLockJsonFile)) {
|
||||||
|
console.log('Removing package-lock.json...');
|
||||||
|
await unlink(packageLockJsonFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ensureNpmIsAvailable() {
|
||||||
|
const exitCode = await executeCommand('npm --version');
|
||||||
|
if (exitCode !== 0) {
|
||||||
|
throw new Error('`npm` in not available!');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ensureNpmRootDirectory(workingDirectory) {
|
||||||
|
const packageJsonPath = resolve(workingDirectory, 'package.json');
|
||||||
|
if (!await exists(packageJsonPath)) {
|
||||||
|
throw new Error(`Not an NPM project root: ${workingDirectory}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildCommand(ci, outputErrors) {
|
||||||
|
const baseCommand = ci ? 'npm ci' : 'npm install';
|
||||||
|
if (!outputErrors) {
|
||||||
|
return `${baseCommand} --loglevel=error`;
|
||||||
|
}
|
||||||
|
return baseCommand;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getOptions() {
|
||||||
|
const processArgs = process.argv.slice(2); // Slice off the node and script name
|
||||||
|
return {
|
||||||
|
rootDirectory: processArgs.includes('--root-directory') ? processArgs[processArgs.indexOf('--root-directory') + 1] : process.cwd(),
|
||||||
|
outputErrors: !processArgs.includes(ARG_NAMES.ignoreErrors),
|
||||||
|
ci: processArgs.includes(ARG_NAMES.ci),
|
||||||
|
fresh: processArgs.includes(ARG_NAMES.fresh),
|
||||||
|
nonDeterministic: processArgs.includes(ARG_NAMES.nonDeterministic),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function executeWithRetry(
|
||||||
|
command,
|
||||||
|
workingDirectory,
|
||||||
|
maxRetries,
|
||||||
|
retryDelayInMs,
|
||||||
|
currentAttempt = 1,
|
||||||
|
) {
|
||||||
|
const statusCode = await executeCommand(command, workingDirectory, true, true);
|
||||||
|
if (statusCode === 0 || currentAttempt >= maxRetries) {
|
||||||
|
return statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`⚠️🔄 Attempt ${currentAttempt} failed. Retrying in ${retryDelayInMs / 1000} seconds...`);
|
||||||
|
await sleep(retryDelayInMs);
|
||||||
|
|
||||||
|
const retryResult = await executeWithRetry(
|
||||||
|
command,
|
||||||
|
workingDirectory,
|
||||||
|
maxRetries,
|
||||||
|
retryDelayInMs,
|
||||||
|
currentAttempt + 1,
|
||||||
|
);
|
||||||
|
return retryResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function executeCommand(
|
||||||
|
command,
|
||||||
|
workingDirectory = process.cwd(),
|
||||||
|
logStdout = false,
|
||||||
|
logCommand = false,
|
||||||
|
) {
|
||||||
|
if (logCommand) {
|
||||||
|
console.log(`▶️ Executing command "${command}" at "${workingDirectory}"`);
|
||||||
|
}
|
||||||
|
const process = exec(
|
||||||
|
command,
|
||||||
|
{
|
||||||
|
cwd: workingDirectory,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (logStdout) {
|
||||||
|
process.stdout.on('data', (data) => {
|
||||||
|
console.log(data.toString());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
process.stderr.on('data', (data) => {
|
||||||
|
console.error(data.toString());
|
||||||
|
});
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
process.on('exit', (code) => {
|
||||||
|
resolve(code);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(milliseconds) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
setTimeout(resolve, milliseconds);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function exists(path) {
|
||||||
|
try {
|
||||||
|
await access(path, constants.F_OK);
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await main();
|
||||||
@@ -1,13 +1,8 @@
|
|||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { rm, readFile } from 'fs/promises';
|
import { rm, readFile } from 'fs/promises';
|
||||||
import { exists, isDirMissingOrEmpty } from './io';
|
import { exists, isDirMissingOrEmpty } from './io';
|
||||||
import { CommandResult, runCommand } from './run-command';
|
import { runCommand } from './run-command';
|
||||||
import { LogLevel, die, log } from './log';
|
import { LogLevel, die, log } from './log';
|
||||||
import { sleep } from './sleep';
|
|
||||||
import type { ExecOptions } from 'child_process';
|
|
||||||
|
|
||||||
const NPM_INSTALL_MAX_RETRIES = 3;
|
|
||||||
const NPM_INSTALL_RETRY_DELAY_MS = 5 /* seconds */ * 1000;
|
|
||||||
|
|
||||||
export async function ensureNpmProjectDir(projectDir: string): Promise<void> {
|
export async function ensureNpmProjectDir(projectDir: string): Promise<void> {
|
||||||
if (!projectDir) { throw new Error('missing project directory'); }
|
if (!projectDir) { throw new Error('missing project directory'); }
|
||||||
@@ -20,13 +15,16 @@ export async function npmInstall(projectDir: string): Promise<void> {
|
|||||||
if (!projectDir) { throw new Error('missing project directory'); }
|
if (!projectDir) { throw new Error('missing project directory'); }
|
||||||
const npmModulesPath = join(projectDir, 'node_modules');
|
const npmModulesPath = join(projectDir, 'node_modules');
|
||||||
if (!await isDirMissingOrEmpty(npmModulesPath)) {
|
if (!await isDirMissingOrEmpty(npmModulesPath)) {
|
||||||
log(`Directory "${npmModulesPath}" exists and has content. Skipping \`npm install\`.`);
|
log(`Directory "${npmModulesPath}" exists and has content. Skipping installing dependencies.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
log('Starting dependency installation...');
|
log('Starting dependency installation...');
|
||||||
const { error } = await executeWithRetry('npm install --loglevel=error', {
|
const { error } = await runCommand(
|
||||||
cwd: projectDir,
|
`npm run install-deps -- --no-errors --root-directory ${projectDir}`,
|
||||||
}, NPM_INSTALL_MAX_RETRIES, NPM_INSTALL_RETRY_DELAY_MS);
|
{
|
||||||
|
cwd: projectDir,
|
||||||
|
},
|
||||||
|
);
|
||||||
if (error) {
|
if (error) {
|
||||||
die(error);
|
die(error);
|
||||||
}
|
}
|
||||||
@@ -103,29 +101,3 @@ async function readPackageJsonContents(projectDir: string): Promise<string> {
|
|||||||
return die(`Error detail: ${error}`);
|
return die(`Error detail: ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function executeWithRetry(
|
|
||||||
command: string,
|
|
||||||
options: ExecOptions,
|
|
||||||
maxRetries: number,
|
|
||||||
retryDelayInMs: number,
|
|
||||||
currentAttempt = 1,
|
|
||||||
): Promise<CommandResult> {
|
|
||||||
const result = await runCommand(command, options);
|
|
||||||
|
|
||||||
if (!result.error || currentAttempt >= maxRetries) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
log(`Attempt ${currentAttempt} failed. Retrying in ${retryDelayInMs / 1000} seconds...`);
|
|
||||||
await sleep(retryDelayInMs);
|
|
||||||
|
|
||||||
const retryResult = await executeWithRetry(
|
|
||||||
command,
|
|
||||||
options,
|
|
||||||
maxRetries,
|
|
||||||
retryDelayInMs,
|
|
||||||
currentAttempt + 1,
|
|
||||||
);
|
|
||||||
return retryResult;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user