npm installer ignore git versions

This commit is contained in:
Evert Prants 2023-08-07 16:19:32 +03:00
parent 0523a79b4a
commit bed3cba38b
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 40 additions and 16 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@squeebot/core",
"version": "3.6.1",
"version": "3.6.2",
"description": "Squeebot v3 core for the execution environment",
"main": "lib/index.js",
"types": "lib/index.d.ts",

View File

@ -9,12 +9,12 @@ import { spawnProcess } from '../util/run';
*/
export class NPMExecutor {
private installed: Record<string, string> = {};
private packageFile: string = path.join(this.environment.path, 'package.json');
private packageFile: string = path.join(
this.environment.path,
'package.json'
);
constructor(
private environment: IEnvironment,
private coreModule: string
) {}
constructor(private environment: IEnvironment, private coreModule: string) {}
/**
* Create a package.json file and install the core.
@ -27,7 +27,11 @@ export class NPMExecutor {
}
// Install core module
const c2 = await spawnProcess('npm', ['install', this.coreModule], this.environment);
const c2 = await spawnProcess(
'npm',
['install', this.coreModule],
this.environment
);
if (c2.code > 0) {
throw new Error(c2.stderr.join('\n'));
}
@ -37,7 +41,7 @@ export class NPMExecutor {
* Load a package.json file into memory
*/
public async loadPackageFile(): Promise<void> {
if (!await fs.pathExists(this.packageFile)) {
if (!(await fs.pathExists(this.packageFile))) {
await this.init();
}
@ -54,7 +58,7 @@ export class NPMExecutor {
* @param pkg Package name
*/
public async installPackage(pkg: string): Promise<void> {
if (!await fs.pathExists(this.packageFile)) {
if (!(await fs.pathExists(this.packageFile))) {
await this.init();
}
@ -65,16 +69,32 @@ export class NPMExecutor {
const pkgRefSplit = pkg.split('@');
const pkgFullName = (spi === 1 ? '@' : '') + pkgRefSplit[spi];
const pkgVersion = this.removeVersionWildcard(pkgRefSplit[spi + 1]) || 'latest';
const pkgVersion =
this.removeVersionWildcard(pkgRefSplit[spi + 1]) || 'latest';
const installedVersion = this.installed[pkgFullName];
if (installedVersion && installedVersion !== 'latest' && pkgVersion !== 'latest') {
const cardless = this.removeVersionWildcard(installedVersion) as string;
if (semver.lte(pkgVersion, cardless)) {
if (installedVersion) {
// Ignore git or http version
if (
installedVersion.startsWith('http') ||
installedVersion.startsWith('git+') ||
installedVersion.startsWith('github:')
)
return;
if (installedVersion !== 'latest' && pkgVersion !== 'latest') {
const cardless = this.removeVersionWildcard(installedVersion) as string;
if (semver.lte(pkgVersion, cardless)) {
return;
}
}
}
const { code, stderr, stdout } = await spawnProcess('npm', ['install', pkg], this.environment);
const { code, stderr, stdout } = await spawnProcess(
'npm',
['install', pkg],
this.environment
);
if (code > 0) {
throw new Error(stderr.join('\n'));
}
@ -89,11 +109,15 @@ export class NPMExecutor {
* @param pkg Package name
*/
public async uninstallPackage(pkg: string): Promise<void> {
if (!await fs.pathExists(this.packageFile)) {
if (!(await fs.pathExists(this.packageFile))) {
await this.init();
}
const { code, stderr, stdout } = await spawnProcess('npm', ['remove', pkg], this.environment);
const { code, stderr, stdout } = await spawnProcess(
'npm',
['remove', pkg],
this.environment
);
if (code > 0) {
throw new Error(stderr.join('\n'));
}