file-managment-ts
v1.0.5
Published
This module should be used to do stuff like file creating moving, copieng, etc. via Typescript/Javascript. It will contain basic API functions to input in your code. Implementation by implementing in ts and adding the ts flavour later.
Downloads
2
Maintainers
Readme
file-managment-ts
This module should be used to do stuff like file creating moving, copieng, etc. via Typescript/Javascript. It will contain basic API functions to input in your code. Implementation by implementing in ts and adding the ts flavour later.
Features
Creating
- [x] create an empty file
- [x] create an file with the content of a string
- [ ] creating a directory.
- [ ] creating a directory with files.
Check existences
- [x] check if a file exists
- [ ] check if a directory exists
Get content
- [x] get the content of a file.
- [ ] show metadata of a file.
- [ ] show metadata of a dir.
- [ ] (optional) search files (by regex).
- [ ] (optional) search file content (by regex).
Add content
- [ ] overwrite content of a file by string.
- [ ] add content at the end of a file.
- [ ] add content to a list of files.
- [ ] add content to every file of a dir.
Deleting
- [ ] delete a file.
- [ ] delete a dir (recursive and non recursive)
Copieng
- [ ] copy content from a file into another file.
- [ ] copy a file.
- [ ] copy a dir.
Rename
- [ ] rename a file
- [ ] rename a directory
Move
- [ ] move a file.
- [ ] move a dir.
Limitations
The library is at the moment only usable for utf-8 encoded files in terms of reading, and adding content to the file. All other features should work properly (not tested atm). Encoding of other file formats, e.g. imaage formats, could be an enhancement in the future.
Structure
Methods should be fully functional, meaning they should have no side-effects. For the start every function is in the same file, maybe a proper reorganisation of the functions later. Methods are only available by a js/ts call a cli API is not planned, because of the Linux build in tools.
Roadmap
- [x] Defining project goals in this README
- [ ] Define the methods with an NotImplementedError
- [ ] Write unit tests for each function (all file interactions will be done in an dir in this folder, which is cleared after every test and should be empty at any time, beside test run time.
- [ ] Implementing the functions.
- [ ] Add an proper logger to the tool.
Build
To work with this project run:
git clone https://github.com/jakobdanel/file-managment-ts
Transpile
- cd into this directory.
- Run
npx tsc main.ts
Test
- cd into this directory
- Run
npm run test
Documentation
The documentation will be build automatically via typedoc and the typedoc plugin typedoc-plugin-markdown. Via the concat-md tool the generated Markdown Files are getting pushed into this README file.
Rebuild the README file.
- cd into this directory.
- Run
./build_documentation
Manually modify the README
If you want to add content to this self written part of the README:
- Modify the content inside of
./readme_content.md
- Rebuild the markdwon file.
About
Author: Jakob Danel Date: 2022-01-02
Classes
file-managment-ts / Modules / file / File
Class: File
file.File
Abstract
This class is used to represent a file. It shoul use at the one Hand as helper for the functions of the main.ts file, but also can be used individually. The main advantage of this class is that you can easily chain multiple operations e.g. first adding content to a file and the moving a file to another location without having to worry about the files path.
Author
Jakob Danel
Table of contents
Constructors
Properties
Methods
- #checkDelete
- addContent
- copy
- copyContent
- delete
- getContent
- getMetadata
- getPath
- modifyContent
- move
- updateContent
Constructors
constructor
• new File(path
)
Abstract
Constructing the file object. Setting the properties. Logging "Logged file: ${path}". If the file is not created yet it will create the file and add an log message to the log.
Throws
Error If the file is not created yet and cannot be created due to permissions/invalid path pattern
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| path
| string
| The path to the file. Should be provided in an absolute way for robustness reasons. |
Defined in
Properties
#path
• Private
#path: string
Abstract
represents the path of the file. Should always be an absolute path.
Property
Defined in
log
• log: string
[]
Abstract
containing strings with informations about the actions which have taken place on the file.
Property
Global
Defined in
Methods
#checkDelete
▸ Private
#checkDelete(): void
Abstract
checking if the file was already deleted. If the file is deleted throw an error.
Throws
Error If the file was already deleted it throws an error.
Returns
void
Defined in
addContent
▸ addContent(content
): void
Abstract
Adding content to the of a file.
Throws
Error if the File was already deleted.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| content
| string
| A string with the content to be appended at the end of the file. |
Returns
void
Defined in
copy
▸ copy(destFilePath
): void
Abstract
Copy the content of the file inside another file.
Throws
Error if the file was already deleted.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| destFilePath
| string
| The path where the file was copied |
Returns
void
Defined in
copyContent
▸ copyContent(destFile
): void
Abstract
Copieng the content of the file inside another file.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| destFile
| File
| The file were the data to be copied. |
Returns
void
Defined in
delete
▸ delete(): void
Abstract
Delete the file. After that move the object is not useable anymore. Every function call will throw an error.
Throws
Error if the File was already deleted.
Returns
void
Defined in
getContent
▸ getContent(): string
Abstract
Get the content of a file.
Throws
Error If the file was already deleted
Returns
string
The content of the file as a string
Defined in
getMetadata
▸ getMetadata(): any
Abstract
Get the metadata of the file.
Throws
Error If the file was already deleted
Throws
Error If the metadata could not be read.
Returns
any
The metadata of the file.
Defined in
getPath
▸ getPath(): string
Abstract
Returns the path of the file.
Returns
string
The path of the file
Defined in
modifyContent
▸ modifyContent(lambda
): void
Parameters
| Name | Type |
| :------ | :------ |
| lambda
| StringModifier
|
Returns
void
Defined in
move
▸ move(newPath
): void
Abstract
Moving the File to a new location. Update the path property.
Throws
Error if the file was already deleted.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| newPath
| string
| The path where to move the file. |
Returns
void
Defined in
updateContent
▸ updateContent(content
): void
Abstract
Update the content of a file. Overwrite all existing content inside the file.
Throws
Error if the File was already deleted.
Parameters
| Name | Type |
| :------ | :------ |
| content
| string
|
Returns
void
Defined in
file-managment-ts / Modules
file-managment-ts
Table of contents
Modules
Modules
file-managment-ts / Modules / file
Module: file
Table of contents
Classes
file-managment-ts / Modules / main
Module: main
Table of contents
Functions
- addContentDir
- addContentFile
- addContentFileList
- copyContent
- copyDir
- copyFile
- createDir
- createEmptyFile
- createFile
- createFileTree
- deleteDir
- deleteFile
- dirExists
- fileExists
- getContent
- getMetaDataDir
- getMetaDataFile
- moveDir
- moveFile
- renameDir
- renameFile
- writeFile
Functions
addContentDir
▸ addContentDir(dirPath
, content
): void
Parameters
| Name | Type |
| :------ | :------ |
| dirPath
| string
|
| content
| string
|
Returns
void
Defined in
addContentFile
▸ addContentFile(filePath
, content
): File
Abstract
This function will add the content of the file at the specified file path to the end of the file. If the file already exists, the content is the form oldContent + content, if the file was not created before, the file will be created and the content are placed inside the File.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| filePath
| string
| The path from the file where the content will be placed. |
| content
| string
| The content which will be placed inside the file. |
Returns
The file object, for operation chaining purposes.
Defined in
addContentFileList
▸ addContentFileList(filePaths
, content
): File
[]
Parameters
| Name | Type |
| :------ | :------ |
| filePaths
| string
[] |
| content
| string
|
Returns
File
[]
Defined in
copyContent
▸ copyContent(srcFilePath
, destDirPath
): File
Parameters
| Name | Type |
| :------ | :------ |
| srcFilePath
| string
|
| destDirPath
| string
|
Returns
Defined in
copyDir
▸ copyDir(srcDirPath
, destDirPath
): void
Parameters
| Name | Type |
| :------ | :------ |
| srcDirPath
| string
|
| destDirPath
| string
|
Returns
void
Defined in
copyFile
▸ copyFile(srcFilePath
, destFilePath
): File
Parameters
| Name | Type |
| :------ | :------ |
| srcFilePath
| string
|
| destFilePath
| string
|
Returns
Defined in
createDir
▸ createDir(dirPath
): void
Parameters
| Name | Type |
| :------ | :------ |
| dirPath
| string
|
Returns
void
Defined in
createEmptyFile
▸ createEmptyFile(filePath
): File
Abstract
This function will create an empty File. It uses the File class from file.ts It will creating a new File object and then checking if at the given path a File already exists. If so it will throw an error.
Throws
Error if the path pattern is invalid {@see file.ts}
Throws
Error if the file already exists.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| filePath
| string
| The path to the file |
Returns
File the File object, for further manipulation
Defined in
createFile
▸ createFile(filePath
, content
): File
Abstract
This function will create a new file at the given path with the given contents. It will creating a new File object and then checking if at the given path a File already exists. If so it will throw an error. For setting the contents of the file, it will use the File.updateContent() method.
Throws
Error if the path pattern can not be resolved {@see file.ts}
Throws
Error if the file already exists.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| filePath
| string
| The path to the file to create |
| content
| string
| The content to set into the file |
Returns
File the File object, for further manipulation
Defined in
createFileTree
▸ createFileTree(fileTree
): void
Parameters
| Name | Type |
| :------ | :------ |
| fileTree
| FileTree
|
Returns
void
Defined in
deleteDir
▸ deleteDir(dirPath
): void
Parameters
| Name | Type |
| :------ | :------ |
| dirPath
| string
|
Returns
void
Defined in
deleteFile
▸ deleteFile(filePath
): void
Parameters
| Name | Type |
| :------ | :------ |
| filePath
| string
|
Returns
void
Defined in
dirExists
▸ dirExists(dirPath
): boolean
Parameters
| Name | Type |
| :------ | :------ |
| dirPath
| string
|
Returns
boolean
Defined in
fileExists
▸ fileExists(filePath
): boolean
Abstract
This method checks if the inputted file exists or not. If the filePath can not be resolved, the function will return false.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| filePath
| string
| The path to the file which will be checked. |
Returns
boolean
True if the file exists, false if not
Defined in
getContent
▸ getContent(filePath
): string
| null
Abstract
Returning the of the file. If the file exsists. If the file do not exists then the function will return null.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| filePath
| string
| The path to the file where the file is located. |
Returns
string
| null
A string containing the content of the file. Null if the file do not exist.
Defined in
getMetaDataDir
▸ getMetaDataDir(dirPath
): void
Parameters
| Name | Type |
| :------ | :------ |
| dirPath
| string
|
Returns
void
Defined in
getMetaDataFile
▸ getMetaDataFile(filePath
): any
Abstract
This function returning the metadata of the file.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| filePath
| string
| The path from the file with the metadata to check. |
Returns
any
The metadata of the file.
Defined in
moveDir
▸ moveDir(srcDirPath
, destDirPath
): void
Parameters
| Name | Type |
| :------ | :------ |
| srcDirPath
| string
|
| destDirPath
| string
|
Returns
void
Defined in
moveFile
▸ moveFile(srcFilePath
, destFilePath
): File
Parameters
| Name | Type |
| :------ | :------ |
| srcFilePath
| string
|
| destFilePath
| string
|
Returns
Defined in
renameDir
▸ renameDir(srcDirPath
, newName
): void
Parameters
| Name | Type |
| :------ | :------ |
| srcDirPath
| string
|
| newName
| string
|
Returns
void
Defined in
renameFile
▸ renameFile(srcFilePath
, newName
): File
Parameters
| Name | Type |
| :------ | :------ |
| srcFilePath
| string
|
| newName
| string
|
Returns
Defined in
writeFile
▸ writeFile(filePath
, content
): File
Abstract
This function will overwrite the content of the file at the specified file path, with content. If the file already exists the old content will be overwritten, if the file did not exist the file will be created and the content placed inside the file.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| filePath
| string
| The path from the file where the content will be placed |
| content
| string
| The content which will be placed inside the file |
Returns
The file object, for operation chaining purposes.
Defined in
file-managment-ts / Modules / test
Module: test
file-managment-ts / Modules / utils
Module: utils
Table of contents
Functions
- addingNewLinesToString
- assert
- assertInteger
- assertNotNegative
- generateFileName
- generateRandomCharacters
- leadingZeros
- randomArrayValue
Functions
addingNewLinesToString
▸ addingNewLinesToString(string
, interval
): string
Abstract
Adding new lines every interval characters to the string. It iterates over the characters of the string. Than the function will build a substring for each interval and adding a \n to the string
Throws
Error if the inputted number is negative
Throws
Error if the inputted number is not an Integer
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| string
| string
| The string to be modified |
| interval
| number
| The interval after which to add new lines |
Returns
string
The modified string, with evenly new lines added
Defined in
assert
▸ assert(condition
, message?
): void
Abstract
This method checking if the condition is true, if not it will throw an exception with the inputted error message. If the condition is true the function will return void.
Throws
Error if the condition is false
Parameters
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| condition
| boolean
| undefined
| The condition to check |
| message
| string
| "Assertion failed"
| The error message which will be thrown if the condition is false |
Returns
void
Defined in
assertInteger
▸ assertInteger(n
): void
Abstract
Implementation of assert method to check that the number is an integer.
Throws
Error if the number is not an integer.
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| n
| number
| The number to check. |
Returns
void
Defined in
assertNotNegative
▸ assertNotNegative(n
): void
Abstract
Implementation of assert method to check that a number is positive or 0.
Throws
Error if the number is not positive or 0
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| n
| number
| The number to check |
Returns
void
Defined in
generateFileName
▸ generateFileName(n
): string
Abstract
Generating a file name with the schema: test_${leadingZeros(n, 3)}.${randomArrayValue<string>(fileExtensions)}
Throws
Error if n is negative
Throws
Error if n is not an integer
Example
generateFileName(10) = "test_010.js"
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| n
| number
| The number which the file name will have. |
Returns
string
The semi random file name
Defined in
generateRandomCharacters
▸ generateRandomCharacters(n
): string
Abstract
Creating a random string with n characters. Uses only the lowercase alphabetical characters.
Throws
Error if the inputted number is negative
Throws
Error if the inputted number is not an integer
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| n
| number
| the number of characters to generate |
Returns
string
A string concenate the generated characters
Defined in
leadingZeros
▸ leadingZeros(n
, targetLength
): string
Abstract
Generating a string representing an integer having targetLength leading characters, so that all left spaces which are not needed to represent the number are filled with 0. If more characters are needed to represent the number then the string will have more characters than the targetLength specified.
Throws
An error if n or targetLength is negative
Throws
An error if n or targetLength is not an integer
Example
leadingZeros(34,3) = "034"
Example
leadingZeros(187,2) = "187" //Note that targetLength could not be fullfilled, because n > 99
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| n
| number
| The number which are modified |
| targetLength
| number
| The length of the target string. |
Returns
string
A string representing n with targetLength characters
Defined in
randomArrayValue
▸ randomArrayValue<T
>(array
): T
Abstract
Generating a random value from an array of abitrary data.
Type parameters
| Name |
| :------ |
| T
|
Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| array
| T
[] | The array of values. |
Returns
T
The random choosen value.