1.0.5 • Published 1 year ago

file-managment-ts v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

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

  • create an empty file
  • create an file with the content of a string
  • creating a directory.
  • creating a directory with files.

Check existences

  • check if a file exists
  • check if a directory exists

Get content

  • 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

  • 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

  1. cd into this directory.
  2. Run npx tsc main.ts

Test

  1. cd into this directory
  2. 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.
  1. cd into this directory.
  2. Run ./build_documentation
Manually modify the README

If you want to add content to this self written part of the README: 1. Modify the content inside of ./readme_content.md 2. 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

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
NameTypeDescription
pathstringThe path to the file. Should be provided in an absolute way for robustness reasons.
Defined in

file.ts:45

Properties

#path

Private #path: string

Abstract

represents the path of the file. Should always be an absolute path.

Property

Defined in

file.ts:27


log

log: string[]

Abstract

containing strings with informations about the actions which have taken place on the file.

Property

Global

Defined in

file.ts:35

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

file.ts:71


addContent

addContent(content): void

Abstract

Adding content to the of a file.

Throws

Error if the File was already deleted.

Parameters
NameTypeDescription
contentstringA string with the content to be appended at the end of the file.
Returns

void

Defined in

file.ts:122


copy

copy(destFilePath): void

Abstract

Copy the content of the file inside another file.

Throws

Error if the file was already deleted.

Parameters
NameTypeDescription
destFilePathstringThe path where the file was copied
Returns

void

Defined in

file.ts:162


copyContent

copyContent(destFile): void

Abstract

Copieng the content of the file inside another file.

Parameters
NameTypeDescription
destFileFileThe file were the data to be copied.
Returns

void

Defined in

file.ts:150


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

file.ts:138


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

file.ts:83


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

file.ts:94


getPath

getPath(): string

Abstract

Returns the path of the file.

Returns

string

The path of the file

Defined in

file.ts:62


modifyContent

modifyContent(lambda): void

Parameters
NameType
lambdaStringModifier
Returns

void

Defined in

file.ts:128


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
NameTypeDescription
newPathstringThe path where to move the file.
Returns

void

Defined in

file.ts:173


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
NameType
contentstring
Returns

void

Defined in

file.ts:111

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

Functions

addContentDir

addContentDir(dirPath, content): void

Parameters
NameType
dirPathstring
contentstring
Returns

void

Defined in

main.ts:157


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
NameTypeDescription
filePathstringThe path from the file where the content will be placed.
contentstringThe content which will be placed inside the file.
Returns

File

The file object, for operation chaining purposes.

Defined in

main.ts:151


addContentFileList

addContentFileList(filePaths, content): File[]

Parameters
NameType
filePathsstring[]
contentstring
Returns

File[]

Defined in

main.ts:162


copyContent

copyContent(srcFilePath, destDirPath): File

Parameters
NameType
srcFilePathstring
destDirPathstring
Returns

File

Defined in

main.ts:196


copyDir

copyDir(srcDirPath, destDirPath): void

Parameters
NameType
srcDirPathstring
destDirPathstring
Returns

void

Defined in

main.ts:192


copyFile

copyFile(srcFilePath, destFilePath): File

Parameters
NameType
srcFilePathstring
destFilePathstring
Returns

File

Defined in

main.ts:186


createDir

createDir(dirPath): void

Parameters
NameType
dirPathstring
Returns

void

Defined in

main.ts:69


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
NameTypeDescription
filePathstringThe path to the file
Returns

File

File the File object, for further manipulation

Defined in

main.ts:33


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
NameTypeDescription
filePathstringThe path to the file to create
contentstringThe content to set into the file
Returns

File

File the File object, for further manipulation

Defined in

main.ts:58


createFileTree

createFileTree(fileTree): void

Parameters
NameType
fileTreeFileTree
Returns

void

Defined in

main.ts:74


deleteDir

deleteDir(dirPath): void

Parameters
NameType
dirPathstring
Returns

void

Defined in

main.ts:180


deleteFile

deleteFile(filePath): void

Parameters
NameType
filePathstring
Returns

void

Defined in

main.ts:174


dirExists

dirExists(dirPath): boolean

Parameters
NameType
dirPathstring
Returns

boolean

Defined in

main.ts:90


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
NameTypeDescription
filePathstringThe path to the file which will be checked.
Returns

boolean

True if the file exists, false if not

Defined in

main.ts:86


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
NameTypeDescription
filePathstringThe 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

main.ts:103


getMetaDataDir

getMetaDataDir(dirPath): void

Parameters
NameType
dirPathstring
Returns

void

Defined in

main.ts:122


getMetaDataFile

getMetaDataFile(filePath): any

Abstract

This function returning the metadata of the file.

Parameters
NameTypeDescription
filePathstringThe path from the file with the metadata to check.
Returns

any

The metadata of the file.

Defined in

main.ts:117


moveDir

moveDir(srcDirPath, destDirPath): void

Parameters
NameType
srcDirPathstring
destDirPathstring
Returns

void

Defined in

main.ts:225


moveFile

moveFile(srcFilePath, destFilePath): File

Parameters
NameType
srcFilePathstring
destFilePathstring
Returns

File

Defined in

main.ts:219


renameDir

renameDir(srcDirPath, newName): void

Parameters
NameType
srcDirPathstring
newNamestring
Returns

void

Defined in

main.ts:212


renameFile

renameFile(srcFilePath, newName): File

Parameters
NameType
srcFilePathstring
newNamestring
Returns

File

Defined in

main.ts:205


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
NameTypeDescription
filePathstringThe path from the file where the content will be placed
contentstringThe content which will be placed inside the file
Returns

File

The file object, for operation chaining purposes.

Defined in

main.ts:137

file-managment-ts / Modules / test

Module: test

file-managment-ts / Modules / utils

Module: utils

Table of contents

Functions

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
NameTypeDescription
stringstringThe string to be modified
intervalnumberThe interval after which to add new lines
Returns

string

The modified string, with evenly new lines added

Defined in

utils.ts:62


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
NameTypeDefault valueDescription
conditionbooleanundefinedThe condition to check
messagestring"Assertion failed"The error message which will be thrown if the condition is false
Returns

void

Defined in

utils.ts:12


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
NameTypeDescription
nnumberThe number to check.
Returns

void

Defined in

utils.ts:32


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
NameTypeDescription
nnumberThe number to check
Returns

void

Defined in

utils.ts:23


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
NameTypeDescription
nnumberThe number which the file name will have.
Returns

string

The semi random file name

Defined in

utils.ts:143


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
NameTypeDescription
nnumberthe number of characters to generate
Returns

string

A string concenate the generated characters

Defined in

utils.ts:43


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
NameTypeDescription
nnumberThe number which are modified
targetLengthnumberThe length of the target string.
Returns

string

A string representing n with targetLength characters

Defined in

utils.ts:122


randomArrayValue

randomArrayValue<T>(array): T

Abstract

Generating a random value from an array of abitrary data.

Type parameters
Name
T
Parameters
NameTypeDescription
arrayT[]The array of values.
Returns

T

The random choosen value.

Defined in

utils.ts:106

ansi-escapesansi-regexansi-stylesanymatchargparsebabel-jestbabel-plugin-istanbulbabel-plugin-jest-hoistbabel-preset-current-node-syntaxbabel-preset-jestbalanced-matchbrace-expansionbracesbrowserslistbs-loggerbserbuffer-fromcallsitescamelcasecaniuse-litechalkchar-regexci-infocjs-module-lexercliuicocollect-v8-coveragecolor-convertcolor-nameconcat-mapconvert-source-mapcross-spawndebugdedentdeepmergedetect-newlinediff-sequenceselectron-to-chromiumemitteryemoji-regexerror-exescaladeescape-string-regexpesprimaexecaexitexpectfast-json-stable-stringifyfb-watchmanfill-rangefind-upfsfs.realpathfunction-bindgensyncget-caller-fileget-package-typeget-streamglobglobalsgraceful-fshashas-flaghtml-escaperhuman-signalsimport-localimurmurhashinflightinheritsis-arrayishis-core-moduleis-fullwidth-code-pointis-generator-fnis-numberis-streamisexeistanbul-lib-coverageistanbul-lib-instrumentistanbul-lib-reportistanbul-lib-source-mapsistanbul-reportsjest-changed-filesjest-circusjest-clijest-configjest-diffjest-docblockjest-eachjest-environment-nodejest-get-typejest-haste-mapjest-leak-detectorjest-matcher-utilsjest-message-utiljest-mockjest-pnp-resolverjest-regex-utiljest-resolvejest-resolve-dependenciesjest-runnerjest-runtimejest-snapshotjest-utiljest-validatejest-watcherjest-workerjs-tokensjs-yamljsescjson-parse-even-better-errorsjson5kleurlevenlines-and-columnslocate-pathlodash.memoizelru-cachemake-dirmake-errormakeerrormerge-streammicromatchmimic-fnminimatchmsnatural-comparenode-int64node-releasesnormalize-pathnpm-run-pathonceonetimep-limitp-locatep-tryparse-jsonpath-existspath-is-absolutepath-keypath-parsepicocolorspicomatchpiratespkg-dirpretty-formatpromptsreact-isrequire-directoryresolveresolve-cwdresolve-fromresolve.exportssemvershebang-commandshebang-regexsignal-exitsisteransislashsource-mapsource-map-supportsprintf-jsstack-utilsstring-lengthstring-widthstrip-ansistrip-bomstrip-final-newlinestrip-json-commentssupports-colorsupports-preserve-symlinks-flagtest-excludetmplto-fast-propertiesto-regex-rangetype-detecttype-festtypescriptupdate-browserslist-dbv8-to-istanbulwalkerwhichwrap-ansiwrappywrite-file-atomicy18nyallistyargsyargs-parseryocto-queue
1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago