Skip to content

Latest commit

 

History

History
760 lines (449 loc) · 16.5 KB

README.md

File metadata and controls

760 lines (449 loc) · 16.5 KB

@snickbit/node-utilities

Table of contents

Interfaces

Type Aliases

Variables

Functions

Before Exit

beforeExit

beforeExit(callback): void

Registers a callback function to be executed before the program exits.

Parameters

Name Type Description
callback () => void The callback function to be executed.

Returns

void

Environment

app_data_dir

Const app_data_dir: string


bashrc_path

Const bashrc_path: string


home_dir

Const home_dir: string


is_wsl

Const is_wsl: boolean = isWsl


platform

Const platform: string


temp_dir

Const temp_dir: string


user_config_dir

Const user_config_dir: string


user_data_dir

Const user_data_dir: string = app_data_dir


verbose

Const verbose: number


interpolateEnv

interpolateEnv(str, defaultValues?): string

Interpolate environment variables in the provided string.

Parameters

Name Type Description
str string The string in which to interpolate environment variables.
defaultValues Record<string, string> An object representing the default values for variables.

Returns

string

The interpolated string.


isBundledElectronApp

isBundledElectronApp(): boolean

Determine if the current process is a bundled Electron app.

Returns

boolean

True if the current process is a bundled Electron app, false otherwise.


isElectronApp

isElectronApp(): boolean

Determine if the current process is an Electron app.

Returns

boolean

True if the current process is an Electron app, false otherwise.

Files

fileExists

fileExists(filepath): boolean

Check if a file or directory exists

Parameters

Name Type Description
filepath PathLike The filepath to check

Returns

boolean

  • Return true if the file or directory exists, false otherwise

findUp

findUp(name, options?): any

Search upward from a directory for a file

Parameters

Name Type Description
name PathLike The file to find
options? string | boolean | Partial<FindUpOptions> options to be used while finding file

Returns

any

  • Return the path of the file, if found. Null if not found

getFile

getFile(filepath, fallback?): any | string

Read a file and return its data as string

Parameters

Name Type Description
filepath PathLike The path of the file
fallback? any The fallback data if the file doesn't exist

Returns

any | string

  • The data read from the file, or the fallback data if the file doesn't exist

getFileJSON

getFileJSON(filepath, fallback?): any | object

Reads a JSON file and return its data as Javascript object

Parameters

Name Type Description
filepath PathLike The path of the file
fallback? any The fallback data if the file doesn't exist

Returns

any | object

  • The JSON parsed data, or the fallback data if the file doesn't exist

getPackageJSON

getPackageJSON(cwd): any

Gets the contents of the package.json file located in the specified directory.

Parameters

Name Type Description
cwd string The current working directory.

Returns

any

  • The contents of the package.json file as a JSON object.

isDirectory

isDirectory(filepath): boolean

Check if a path is directory

Parameters

Name Type Description
filepath PathLike The path to check

Returns

boolean

  • Return true if the path is a directory, false otherwise

mkdir

mkdir(dir_path, recursive?): void

Create a new directory

Parameters

Name Type Default value Description
dir_path PathLike undefined The path of the directory to create
recursive? boolean true Whether to create directories recursively

Returns

void


saveFile

saveFile(filepath, content, options?): void

Write data to a file

Parameters

Name Type Default value Description
filepath PathOrFileDescriptor undefined The path of the file
content string | ArrayBufferView undefined The content to write
options? WriteFileOptions 'utf8' options to be used while writing file

Returns

void

  • Return nothing

saveFileJSON

saveFileJSON(filepath, content, options?): void

Write a Javascript object to a JSON file

Parameters

Name Type Default value Description
filepath PathOrFileDescriptor undefined The path of the file
content any undefined The object to write
options? WriteFileOptions 'utf8' options to be used while writing file

Returns

void

  • Return nothing

unlink

unlink(filepath): void

Delete a file

Parameters

Name Type Description
filepath PathLike The path of the file

Returns

void

Imports

ImportMethod

Ƭ ImportMethod<Args, Results>: (...args: Args[]) => Promise<Results> | Results

Type parameters

Name Type
Args any
Results any

Type declaration

▸ (...args): Promise<Results> | Results

Parameters
Name Type
...args Args[]
Returns

Promise<Results> | Results


ImportRecords

Ƭ ImportRecords<I>: Record<string, I | ImportDefinition<I>>

Type parameters

Name Type
I extends ImportMethod = ImportMethod

ParsedImportRecords

Ƭ ParsedImportRecords<I>: Record<string, ParsedImport<I>>

Type parameters

Name Type
I extends ImportMethod = ImportMethod

RawImports

Ƭ RawImports<I>: ImportRecords<I> | RecordOfImportRecords<I> | any

Type parameters

Name Type
I extends ImportMethod = ImportMethod

RecordOfImportRecords

Ƭ RecordOfImportRecords<I>: Record<string, ImportRecords<I>>

Type parameters

Name Type
I extends ImportMethod = ImportMethod

isImport

isImport(data): boolean

Checks whether a given data is of "import" type.

Parameters

Name Type Description
data any The data to be checked.

Returns

boolean

  • Returns true if provided data is an import type.

isImportDefinition

isImportDefinition(data): boolean

Checks whether a given data is of "import definition" type.

Parameters

Name Type Description
data any The data to be checked.

Returns

boolean

  • Returns true if provided data is an import definition type.

parseImports

parseImports<I>(imports, parent?): ParsedImportRecords<I>

Parse provided imports and prepares a record of it.

Type parameters

Name Type
I extends ImportMethod<any, any> = ImportMethod<any, any>

Parameters

Name Type Description
imports any The raw imports to be parsed.
parent? string The parent name.

Returns

ParsedImportRecords<I>

Modules

bufferStream

bufferStream(readable): Promise<Buffer>

Buffers the data from a Readable stream and returns a Promise that resolves to a Buffer.

Parameters

Name Type Description
readable Readable The Readable stream to be buffered.

Returns

Promise<Buffer>

A Promise that resolves to a Buffer containing all the data from the Readable stream.


makeBuffer

makeBuffer(content): Buffer

Creates a new Buffer object from the given content.

Parameters

Name Type Description
content string The content to be used for creating the Buffer.

Returns

Buffer

  • The created Buffer object.

Prompts

AnswerTypes

Ƭ AnswerTypes: Date | boolean | number | string


Answers

Ƭ Answers: Record<string, string>


ChoiceOption

Ƭ ChoiceOption: ChoiceDefinition | string


ChoiceRecords

Ƭ ChoiceRecords: Record<string, ChoiceOption>


PromptType

Ƭ PromptType: "autocomplete" | "autocompleteMultiselect" | "confirm" | "date" | "invisible" | "list" | "multiselect" | "number" | "password" | "select" | "text" | "toggle"


PromptsMethod

Ƭ PromptsMethod: (prev: string, answers: Answers, previousQuestion: Question) => Promise<string> | string

Type declaration

▸ (prev, answers, previousQuestion): Promise<string> | string

Parameters
Name Type
prev string
answers Answers
previousQuestion Question
Returns

Promise<string> | string


Question

Ƭ Question: AutoCompleteMultiSelectQuestion | AutoCompleteQuestion | ConfirmQuestion | DateQuestion | InvisibleQuestion | ListQuestion | MultiSelectQuestion | NumberQuestion | PasswordQuestion | SelectQuestion | TextQuestion | ToggleQuestion


QuestionRecords

Ƭ QuestionRecords: Record<string, Question>


ask

ask(question, defaultAnswer?): Promise<any>

Prompt the user for input using Prompts.

Parameters

Name Type Description
question string The question to be asked to the user.
defaultAnswer? string | boolean The default answer for the question.

Returns

Promise<any>

  • A promise that will be resolved with the answer.

See

https://github.com/terkelg/prompts

ask(question, options?): Promise<any>

Prompt the user for input using Prompts.

Parameters

Name Type Description
question string The question to be asked to the user.
options? Partial<Question> The options for the question.

Returns

Promise<any>

  • A promise that will be resolved with the answer.

See

https://github.com/terkelg/prompts


confirm

confirm(question, defaultAnswer?): Promise<boolean>

Prompt the user for confirmation using Prompts with a default boolean answer.

Parameters

Name Type Description
question string The question to be asked to the user.
defaultAnswer? boolean The default answer for the question.

Returns

Promise<boolean>

  • A promise that will be resolved with the answer.

See

https://github.com/terkelg/prompts

confirm(question, options?): Promise<boolean>

Prompt the user for confirmation using Prompts with question options.

Parameters

Name Type Description
question string The question to be asked to the user.
options? Partial<Question> The options for the question.

Returns

Promise<boolean>

  • A promise that will be resolved with the answer.

See

https://github.com/terkelg/prompts


prompt

prompt(questions): Promise<Answers>

Prompts the user with questions and returns the answers as a promise.

Parameters

Name Type Description
questions QuestionRecords | Question[] The questions to prompt the user with.

Returns

Promise<Answers>

  • A promise that resolves with the answers provided by the user.