Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Only adding folders to stack with a compose file. #299

Merged
merged 6 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions backend/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import yaml from "yaml";
import { DockgeSocket, fileExists, ValidationError } from "./util-server";
import path from "path";
import {
acceptedComposeFileNames,
COMBINED_TERMINAL_COLS,
COMBINED_TERMINAL_ROWS,
CREATED_FILE,
Expand Down Expand Up @@ -40,8 +41,7 @@ export class Stack {

if (!skipFSOperations) {
// Check if compose file name is different from compose.yaml
const supportedFileNames = [ "compose.yaml", "compose.yml", "docker-compose.yml", "docker-compose.yaml" ];
for (const filename of supportedFileNames) {
for (const filename of acceptedComposeFileNames) {
if (fs.existsSync(path.join(this.path, filename))) {
this._composeFileName = filename;
break;
Expand Down Expand Up @@ -222,6 +222,26 @@ export class Stack {
}
}

/**
* Checks if a compose file exists in the specified directory.
* @async
* @static
* @param {string} stacksDir - The directory of the stack.
* @param {string} filename - The name of the directory to check for the compose file.
* @returns {Promise<boolean>} A promise that resolves to a boolean indicating whether any compose file exists.
*/
static async composeFileExists(stacksDir : string, filename : string) : Promise<boolean> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it is a bit over used await and promise here. I try to rewrite it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No kidding, that is definitely a weak point for me hahaha

let filenamePath = path.join(stacksDir, filename);
// Check if any compose file exists
let composeFileExistsList = await Promise.all(acceptedComposeFileNames.map(async (composeFileName) => {
return await fsAsync.stat(path.join(filenamePath, composeFileName))
.then((stat) => stat.isFile())
.catch(() => false);
}));
log.debug("getStackList", `${filename}: ${composeFileExistsList.includes(true)}`);
return composeFileExistsList.includes(true);
}

static async getStackList(server : DockgeServer, useCacheForManaged = false) : Promise<Map<string, Stack>> {
let stacksDir = server.stacksDir;
let stackList : Map<string, Stack>;
Expand All @@ -242,6 +262,10 @@ export class Stack {
if (!stat.isDirectory()) {
continue;
}
// If no compose file exists, skip it
if (await Stack.composeFileExists(stacksDir, filename) === false) {
continue;
}
let stack = await this.getStack(server, filename);
stack._status = CREATED_FILE;
stackList.set(filename, stack);
Expand Down
2 changes: 2 additions & 0 deletions backend/util-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export const allowedRawKeys = [
"\u0003", // Ctrl + C
];

export const acceptedComposeFileNames = [ "compose.yaml", "compose.yml", "docker-compose.yml", "docker-compose.yaml" ];

/**
* Generate a decimal integer number from a string
* @param str Input
Expand Down
Loading