@vimlet/commons
v2.7.25
Published
Provide utilities for common tasks such as, file compression, file download, OS integration, exec, etc
Downloads
30
Readme
Provides a cross environment standardized API for common used functions.
This module is a constant effort of improvement to achieve a stable and flexible, free open-source solution, for common case developing needs.
Currently supporting:
- OS
- Run
- Request
- Compress
- Util
- Progress
You can access the source code at vimlet/commons
Instalation:
- Via NPM:
npm install @vimlet/commons
Basic usage:
const commons = require("@vimlet/commons");
var src = require("path").join(__dirname, "resources/compress/pack");
var dst = require("path").join(__dirname, "resources/compress/file.zip");
commons.compress.pack(src, dst, "zip", null, function(error) {
if(error) {
console.log("Fail");
} else {
console.log("Success");
}
});
Compress
compress.pack(file, dest, options, doneHandler)
Compress.
- file: Source file or directory.
- dest: Destination file.
- options:
- packHandler: Progression callback.
function(error, entry, entrySize, totalSize, totalCount)
. - outputHandler: Default output callback
function(out)
, redirects stdout when provided. - format: Compression format (zip, tar, tgz).
- doneHandler: Default done callback
function(error, data)
.
compress.unpack(file, dest, options, doneHandler)
Uncompress.
- file: Source file or directory.
- dest: Destination folder.
- options:
- unpackHandler: Progression callback.
function(error, entry, entrySize, totalSize, totalCount)
. - outputHandler: Default output callback
function(out)
, redirects stdout when provided. - format: Compression format (zip, tar, tgz).
- doneHandler: Default done callback
function(error, data)
.
Copy
copy.copy(include, output, options, callback);
Copy files in given pattern.
- include: Directory to look for files.
- output: Directory where files will be written.
- options:
- exclude: Used to skip files that you don't want to copy.
- clean: Empty output directory before copy.
- callback.
copy.watch(include, output, options);
Watch for file changes in given pattern. Watch for file changes in given pattern.
- include: Directory to look for files.
- output: Directory where files will be written.
- options:
- exclude: Used to skip files that you don't want to copy.
- clean: Empty output directory before copy.
Io
io.getFiles(dir, options)
Get files from directory. Return an array of objects like:
{
root: "Root path",
files: "Array with paths to files"
}
- dir: Array of patterns to search or single pattern.
- options:
- exclude: patterns to exclude from search.
- ignoreExtension: ignore file extensions.
io.absoluteFiles(index)
Get files from index object. Return an array of absolute paths.
- index: Object with root and relative paths.
io.getRootFromPattern(pattern)
Get root folder from a given pattern.
io.isDirectory(path)
Return true if path is a path to a directory, false if it is a path to a file.
io.getFileSize(path)
Return the size of the file in bytes.
io.deleteFolderRecursive(folderPath, callback)
io.deleteFolderRecursiveSync(folderPath)
Delete a folder and all its content.
io.isInPattern(filePath, pattern, options)
Return true if given file belongs to given pattern. False if not.
- options:
- exlude: files to exclude from search.
io.writeToDisk(output, data, callback)
Write data to disk.
- output: Path to write the file.
- data: Data to write.
io.getCommonBasePath(paths)
Return a path that all given paths have in common.
- paths: String with multiple paths to compare.
Os
os.isWindows()
os.isLinux()
os.isMac()
os.is64Bit()
os.getUnixUserProfile()
os.setUserEnvironmentVariable(key, value, callback)
Sets environment variables without admin privileges.
- key: Enviroment variable key.
- value: Enviroment variable value.
- callback
os.addToUserPath(value, callback)
Sets path variables without admin privileges.
- value: Path value to append.
- callback
os.killProcessByName(name, options, callback)
Kill a process by its name.
- name: Name of the process to be killed.
- options:
- execHandler: Default output callback
function(out)
, redirects stdout when provided.
- callback
os.createSymlink(dest, src, options, callback)
Creates a symbolic link without admin privileges.
- dest: Symlink destination path.
- src: Symlink source path.
- options:
- execHandler: Default output callback
function(out)
, redirects stdout when provided.
- callback
os.findExec(binary, callback)
Asserts if a command is accessible from the command line.
- binary: Symlink destination path.
- callback
Progress
progress.paintProgress(value, outputHandler)
Prints progress at a given percent.
- value: Progress percent.
- outputHandler: Default output callback
function(out)
, redirects stdout when provided.
progress.showProgress(value, options, outputHandler)
Prints progress percent by value and total and returns the percent.
- value: Current progress value.
- options:
- paintProgress: Function that actually does the painting.
- total: Total progress value.
- outputHandler: Default output callback
function(out)
, redirects stdout when provided.
progress.progressHandler(total, max, options, mainOutputHandler)
Handle progress painting avoiding duplicated output of the same progress.
- total: Total percent value.
- max:Provide a virtual limit that avoids printing over this value.
- options:
- paintProgress: Function that actually does the painting.
- mainOutputHandler: Default output callback
function(out)
, redirects stdout when provided.
Request
request.download(url, dest, options, doneHandler)
Downloads a file.
- url: The URL source to download.
- dest: he place where the downloaded file will be stored.
- options:
- downloadHandler: A progress callback
function(received, total, statusCode)
. - outputHandler: Default output callback
function(out)
, redirects stdout when provided.
- doneHandler: Default done callback
function(error, status)
.
Run
run.exec(command, options, doneHandler)
Runs a file or command and streams its output.
- command: File or command to be executed.
- options:
- execHandler: Default output callback
function(out, error)
, redirects stdout when provided. - args: Executable arguments(string[]).
- workingDirectory: The path from where the executable will run.
- doneHandler: Default done callback
function(error, exitCode)
.
run.fetch(command, options, doneHandler)
Runs a file or command and buffers its output.
- command: File or command to be executed.
- options:
- args: Executable arguments(string[]).
- workingDirectory: The path from where the executable will run.
- doneHandler: Default done callback
function(error, exitCode)
.
License
This project is under MIT License. See LICENSE for details.