-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prepare version controlling for tests
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default () => { | ||
console.log("Preparing the mongoUpdater..."); | ||
}; |