From 83e7827ab817493db4c9dedef68944194ca66832 Mon Sep 17 00:00:00 2001 From: Orkun Tokdemir Date: Thu, 30 May 2024 14:03:05 +0200 Subject: [PATCH] Package: Add package checker `npm run checkPackage` compares versions in both `package.json` and `package-lock.json`. Also, it checks `npm install` needs to be run. Change-Id: I860e1eb901164dfbfc27a5faca91b4d28dcaa780 Reviewed-by: Marcus Tillmanns --- package.json | 3 +- src/scripts/check_package.ts | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/scripts/check_package.ts diff --git a/package.json b/package.json index eeb4075..5ad2bd0 100644 --- a/package.json +++ b/package.json @@ -412,12 +412,13 @@ "compile": "node ./esbuild.mjs", "watch": "node ./esbuild.mjs --watch", "pretest": "npm run compile && npm run lint", - "ci-lint": "prettier -c \"**/*.{js,ts,json,mjs,cjs}\" && eslint . && npm run checkLicenses", + "ci-lint": "prettier -c \"**/*.{js,ts,json,mjs,cjs}\" && eslint . && npm run checkLicenses && npm run checkPackage", "lint": "npm run prettier && eslint . --fix", "unitTests": "npm run pretest && node ./out/test/unit/runTest.js", "integrationTests": "npm run pretest && node ./out/test/integration/runTest.js", "allTests": "ts-node ./src/scripts/run_all_tests.ts", "checkLicenses": "ts-node ./src/scripts/check_licenses.ts", + "checkPackage": "ts-node ./src/scripts/check_package.ts", "generateLicenses": "ts-node ./src/scripts/generate_licenses.ts", "prettier": "prettier --write \"**/*.{js,ts,json,mjs,cjs}\" --log-level silent", "package": "npm ci && vsce package --out out" diff --git a/src/scripts/check_package.ts b/src/scripts/check_package.ts new file mode 100644 index 0000000..fff7f89 --- /dev/null +++ b/src/scripts/check_package.ts @@ -0,0 +1,62 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only + +import * as path from 'path'; +import * as fs from 'fs'; +import { execSync } from 'child_process'; + +interface PackageJson { + name: string; + version: string; + dependencies: Record; + devDependencies: Record; +} + +interface RootPackage { + version: string; +} + +interface PackageLockJson { + name: string; + version: string; + packages: { + '': RootPackage; + }; +} + +function main() { + const extensionRoot = path.resolve(__dirname, '../../'); + const packageJsonPath = path.join(extensionRoot, 'package.json'); + const packageLockJsonPath = path.join(extensionRoot, 'package-lock.json'); + const packageJson = JSON.parse( + fs.readFileSync(packageJsonPath, 'utf-8') + ) as PackageJson; + const packageLockJson = JSON.parse( + fs.readFileSync(packageLockJsonPath, 'utf-8') + ) as PackageLockJson; + process.chdir(extensionRoot); + console.log('Checking package versions...'); + if ( + packageJson.version !== packageLockJson.version || + packageJson.version !== packageLockJson.packages[''].version + ) { + const errorMessage = + 'Package versions do not match. Please run `npm install` to update package-lock.json'; + console.error(errorMessage); + process.exit(1); + } + console.log('Package versions match'); + + console.log('Checking for missing dependencies...'); + try { + execSync('npm ls'); + } catch (error) { + const errorMessage = + "Missing dependencies found. Please run 'npm install' to install missing dependencies."; + console.error(errorMessage); + process.exit(1); + } + console.log('All dependencies are installed'); +} + +main();