Skip to content

Commit

Permalink
Package: Add package checker
Browse files Browse the repository at this point in the history
`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 <[email protected]>
  • Loading branch information
OrkunTokdemir committed May 31, 2024
1 parent 317d1e0 commit 83e7827
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
62 changes: 62 additions & 0 deletions src/scripts/check_package.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>;
devDependencies: Record<string, string>;
}

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();

0 comments on commit 83e7827

Please sign in to comment.