-
-
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.
Merge pull request #9 from CodeLit/development
Huge improvements
- Loading branch information
Showing
24 changed files
with
640 additions
and
490 deletions.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,36 +1,75 @@ | ||
import fs from 'fs-extra'; | ||
import assert from 'node:assert'; | ||
import path from 'path'; | ||
|
||
/** | ||
* Checks if a file exists. | ||
* Checks if a folder is not empty. | ||
* | ||
* @param {string} filePath - The path to the file. | ||
* @return {undefined} | ||
* @param {string} folderPath - The path to the folder. | ||
* @return {void} Throws an exception if the folder is empty. | ||
*/ | ||
export function fileExists(filePath) { | ||
// Check if the file exists | ||
if (!fs.existsSync(filePath)) { | ||
export function folderIsNotEmpty(folderPath) { | ||
// Get the list of files in the folder | ||
const files = fs.readdirSync(folderPath); | ||
if (files.length === 0) { | ||
throw new assert.AssertionError({ | ||
message: `File ${filePath} does not exist.`, | ||
message: `Folder ${folderPath} is empty.`, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Asserts that the number of files in a given folder is equal to a specified length. | ||
* Clears the contents of a folder. | ||
* | ||
* @param {string} folderPath - The path to the folder. | ||
* @param {number} length - The expected number of files. | ||
* @throws {assert.AssertionError} If the number of files is greater than the specified length. | ||
* @throws {AssertionError} If the folder is not empty. | ||
*/ | ||
export function filesLength(folderPath, length) { | ||
// Get the list of files in the folder | ||
export function ensureToClearFolder(folderPath) { | ||
fs.emptyDirSync(folderPath); | ||
const files = fs.readdirSync(folderPath); | ||
if (files.length > length) { | ||
if (files.length !== 0) { | ||
throw new assert.AssertionError({ | ||
message: `Folder ${folderPath} does not contain the expected number of files.`, | ||
actual: files.length, | ||
expected: length, | ||
message: `Folder ${folderPath} must be empty.`, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the given folder structure matches the expected structure. | ||
* | ||
* @param {Array|string} folders - The folders to check the structure for. If a string is provided, it is converted to an array with a single element. | ||
* @param {Array} structure - The expected structure of the folders. | ||
* @throws {AssertionError} Throws an error if the actual structure does not match the expected structure. | ||
*/ | ||
export function structureMatch(folders, structure) { | ||
if (!Array.isArray(folders)) folders = [folders]; | ||
|
||
function actualStructure(folderPath) { | ||
let structure = []; | ||
const objects = fs.readdirSync(folderPath); | ||
objects.forEach((obj) => { | ||
const isDir = fs.lstatSync(path.join(folderPath, obj)).isDirectory(); | ||
if (isDir) { | ||
const subFolderStructure = actualStructure(path.join(folderPath, obj)); | ||
subFolderStructure.forEach((subObj) => { | ||
structure.push(obj + '/' + subObj); | ||
}); | ||
} else structure.push(obj); | ||
}); | ||
return structure; | ||
} | ||
|
||
folders.forEach((folderPath) => { | ||
const actualStructureResult = actualStructure(folderPath); | ||
actualStructureResult.sort(); | ||
structure.sort(); | ||
|
||
if (JSON.stringify(actualStructureResult) !== JSON.stringify(structure)) { | ||
throw new assert.AssertionError({ | ||
message: `Folder ${folderPath} does not match the expected structure.`, | ||
actual: actualStructureResult, | ||
expected: structure, | ||
}); | ||
} | ||
}); | ||
} |
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
Oops, something went wrong.