From f0302460ee594eacef1829ccf814b000aef90a2c Mon Sep 17 00:00:00 2001 From: Kirill Date: Sat, 9 Dec 2023 22:29:33 +0100 Subject: [PATCH] Avoid DC's 'variable not set' warnings --- .idea/misc.xml | 1 - lib/browserup_cli.mjs | 6 +++- lib/commands/install.mjs | 4 +-- .../cluster_credentials_repository.mjs | 1 - lib/services/docker_client.mjs | 28 +++++++++++++++---- package-lock.json | 4 +-- package.json | 4 +-- 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d1..6e866721 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/lib/browserup_cli.mjs b/lib/browserup_cli.mjs index 588e2ec3..4e2cfe5e 100644 --- a/lib/browserup_cli.mjs +++ b/lib/browserup_cli.mjs @@ -49,6 +49,7 @@ import path from 'path'; import { fileURLToPath } from 'url'; import {LogSpinner} from "./utils/log_spinner.mjs"; import {readFileSync} from "fs"; +import {BrowserUpPaths} from "./utils/browserup_paths.mjs"; global.appRoot = path.dirname(fileURLToPath(import.meta.url)); @@ -156,7 +157,10 @@ export class BrowserUpCli { } versionMessage() { - return `BrowserUp CLI ${this.getVersion()}\nServices Version: ${SERVICES_VERSION}\nDefault Image: ${BROWSERUP_DEFAULT_IMAGE}`; + return `BrowserUp CLI ${this.getVersion()}\n` + + `Services Version: ${SERVICES_VERSION}\n` + + `Default Image: ${BROWSERUP_DEFAULT_IMAGE}\n` + + `Settings path: ${BrowserUpPaths.appSettingsPath("browserup")}`; } constructor(customExitCallback = undefined, outHsh = null) { diff --git a/lib/commands/install.mjs b/lib/commands/install.mjs index 680cd49f..449c488f 100644 --- a/lib/commands/install.mjs +++ b/lib/commands/install.mjs @@ -43,10 +43,10 @@ async function pullDockerImages(background = false){ log.debug("Looking for yaml at: " + BrowserUpPaths.dockerComposeYmlPath()); log.info("Pulling Docker images."); log.info("Depending on your connection, this may take from five to fifteen minutes."); - + await DockerClient.dockerPull({imageNameAndTag: BROWSERUP_DEFAULT_IMAGE, background: false}); await DockerClient.dockerComposePull( - {dockerComposePath: BrowserUpPaths.dockerComposeYmlPath(), services: '', background: false}); + {dockerComposePath: BrowserUpPaths.dockerComposeYmlPath(), services: '', background: false}); } diff --git a/lib/services/cluster_credentials_repository.mjs b/lib/services/cluster_credentials_repository.mjs index aad35a8b..8795b332 100644 --- a/lib/services/cluster_credentials_repository.mjs +++ b/lib/services/cluster_credentials_repository.mjs @@ -13,7 +13,6 @@ dotenvConfig(); export class ClusterCredentialsRepository { static getCredentials(options, requiredFields = []) { - log.debug("apiToken: " + this.getApiToken(options)); let opts= { apiToken: this.getApiToken(options), diff --git a/lib/services/docker_client.mjs b/lib/services/docker_client.mjs index 3e9969b2..8a9659f2 100644 --- a/lib/services/docker_client.mjs +++ b/lib/services/docker_client.mjs @@ -4,6 +4,8 @@ import os from 'os'; import {exec as execCb, spawn, execSync} from 'child_process'; import { promisify } from 'util'; import {ErrorType, decoratedError} from "../browserup_errors.mjs"; +import {Docker} from "../utils/docker.mjs"; +import yaml from "js-yaml"; const exec = promisify(execCb); @@ -226,7 +228,7 @@ export class DockerClient { if (!fs.existsSync(dockerComposePath)) { throw decoratedError(`Provided docker-compose file doesn't exist at path: '${dockerComposePath}'`); } - + const emptyEnvs = await this.getEmptyDockerComposeEnvs({dockerComposePath}) const cmd = `${await DockerClient.getDockerComposeExecutable()} -f "${dockerComposePath}" pull ${services}`; log.debug(`Running command: ${cmd}`); let result = null; @@ -235,7 +237,7 @@ export class DockerClient { this.execBackgroundCommand(cmd); return; } else { - result = await this.execCommand(cmd); + result = await this.execCommand(cmd, emptyEnvs); } if (result.exitCode !== 0) { throw decoratedError(`Failed to run 'docker-compose pull': \n>> ${result.stderr.join(">> ")}`); @@ -338,15 +340,29 @@ export class DockerClient { if (!fs.existsSync(dockerComposePath)) { throw decoratedError(`Provided docker-compose file doesn't exist at path: '${dockerComposePath}'`); } - + const emptyEnvs = await this.getEmptyDockerComposeEnvs({dockerComposePath}) const cmd = `${await DockerClient.getDockerComposeExecutable()} -f "${dockerComposePath}" rm`; - const result = await this.execCommand(cmd); + const result = await this.execCommand(cmd, emptyEnvs); if (result.exitCode !== 0) { throw decoratedError(`Failed to run 'docker-compose rm': \n>> ${result.stderr.join(">> ")}`); } } + static async getEmptyDockerComposeEnvs({dockerComposePath}) { + const dockerComposeYaml = fs.readFileSync(dockerComposePath, "utf8") + const templateVarRegex = /\${(.*?)}/g; + const matches = dockerComposeYaml.match(templateVarRegex) || []; + + const result = {}; + matches.forEach(match => { + const variableName = match.slice(2, -1).trim(); // Remove `${` and `}` and trim whitespace + result[variableName] = ''; + }); + + return result; + } + static async dockerComposeDown({dockerComposePath, removeVolumes = true, env}) { if (!fs.existsSync(dockerComposePath)) { throw decoratedError(`Provided docker-compose file doesn't exist at path: '${dockerComposePath}'`); @@ -359,8 +375,8 @@ export class DockerClient { if (removeVolumes) { cmd += ' -v'; } - - const result = await this.execCommand(cmd); + const dummyEnvs = await this.getEmptyDockerComposeEnvs({dockerComposePath}) + const result = await this.execCommand(cmd, dummyEnvs); if (result.exitCode !== 0) { let msg = `Failed to run 'docker-compose down': \n>> ${result.stderr.join(">> ")}`; diff --git a/package-lock.json b/package-lock.json index f9d24a13..ca1ca801 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "browserup", - "version": "1.1.31", + "version": "1.1.33", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "browserup", - "version": "1.1.31", + "version": "1.1.33", "license": "AGPL-3.0", "dependencies": { "@aws-sdk/client-auto-scaling": "^3.363.0", diff --git a/package.json b/package.json index be7f4525..9365e685 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ }, "scripts": { "test": "NODE_OPTIONS=--experimental-vm-modules jest", + "windows-test": "set NODE_OPTIONS=--experimental-vm-modules && jest", "prepare": "node prepare.mjs" }, "keywords": [ @@ -52,8 +53,7 @@ "node" ], "testMatch": [ - ",/tests/**/?(*.)+(spec|test).[jt]s?(x)", - "/tests/**/?(*.)+(spec|test).mjs" + "/tests/**/*.{spec,test}.mjs" ], "testPathIgnorePatterns": [ "/node_modules/",