Skip to content

Commit

Permalink
prepare version controlling for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lotmek committed Nov 13, 2023
1 parent c278c42 commit 586211b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"build": "tsc",
"start": "nodemon src/index.ts"
"start": "nodemon src/index.ts",
"test": "nodemon util/mongo-updater/index.ts"
},
"keywords": [],
"author": "",
Expand Down
32 changes: 32 additions & 0 deletions server/util/mongo-updater/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as fs from "fs";

/**
* Retrieve version files in order.
*/
export const getSortedVersionFiles = (directoryPath: string) => {
// Get the list of files in the current directory
const files = fs.readdirSync(directoryPath);

// Filter files to include only those matching the 'X.X.X.ts' pattern
const versionedFiles = files.filter((file) => /^\d+\.\d+\.\d+\.ts$/.test(file));

// Sort files in numerical order
const sortedFiles = versionedFiles.sort((a, b) => {
const [aMajor, aMinor, aPatch] = a.split(".").map(Number);
const [bMajor, bMinor, bPatch] = b.split(".").map(Number);

if (aMajor !== bMajor) return aMajor - bMajor;
if (aMinor !== bMinor) return aMinor - bMinor;
return aPatch - bPatch;
});

return sortedFiles;
};

/**
* Extract the file version from the file path.
*/
export const getFileVersion = (file: string): string | null => {
const match = file.match(/^\d+\.\d+\.\d+/);
return match?.at(0) ?? null;
};
56 changes: 56 additions & 0 deletions server/util/mongo-updater/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as mongoDB from "mongodb";
import dotenv from "dotenv";
import path from "path";
import { getSortedVersionFiles } from "./file";

dotenv.config();
/**
* Function to update the test database
*/
async function updateTestDb() {
// Check if MongoDB environment variables are defined
if (!process.env.DB_CONN_URL) {
throw new Error("MongoDB connection URL is not defined");
} else if (!process.env.TEST_COLLECTION_NAME) {
throw new Error("The name of the testing config collection is not defined");
}

// Access the database
const client: mongoDB.MongoClient = new mongoDB.MongoClient(process.env.DB_CONN_URL);
await client.connect().catch((error) => {
console.error(`Error while connection to MongoDB: ${error}`);
});

const db: mongoDB.Db = client.db(process.env.DB_NAME);

const testConfigCollection: mongoDB.Collection = db.collection(process.env.TEST_COLLECTION_NAME);

// Find the existing configuration document or create a new one
let config: mongoDB.WithoutId<mongoDB.BSON.Document> | null =
await testConfigCollection.findOne();

if (!config) {
config = await testConfigCollection.insertOne({ version: "0.0.0" });
}

// Get a sorted list of version files from the versions directory
const versionPath = path.resolve(__dirname, "./versions");
const versionFiles = getSortedVersionFiles(versionPath);

// Import and execute functions in order
for (const file of versionFiles) {
try {
const module = await import(path.resolve(versionPath, file));
if (module.default && typeof module.default === "function") {
module.default();
console.log(`Function in ${file} executed successfully.`);
} else {
console.error(`No valid default export in ${file}.`);
}
} catch (error) {
console.error(`Error executing ${file}: ${error}`);
}
}
}

updateTestDb();
3 changes: 3 additions & 0 deletions server/util/mongo-updater/versions/1.0.0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
console.log("Preparing the mongoUpdater...");
};

0 comments on commit 586211b

Please sign in to comment.