0.0.1-alpha.33 • Published 5 years ago

toolkit-utils v0.0.1-alpha.33

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

⚠️ This is very much work in progress. The API will be facing breaking changes until it is no longer alpha.

Inspired by "Tools without config", "The Melting Pot of JavaScript", and kcd-scripts.

This library provides utility classes and methods for creating toolkits, used to abstract much of the overhead involved in regular JS and TS tasks, such as testing, linting, formatting, building, etc.

API

Classes

Functions

Project

Kind: global class

new Project(options)

ParamDescription
optionsOptions
options.debugEnables debug logs.
options.silentSilences the logger.
options.loggerThe instance of Signale to be used as a logger.
options.filesDirThe directory of the scripts and config directories. May be the src or lib directory where the toolkit is called from.
options.toolkitRootThe root of the toolkit using this library.

project.srcDir

Kind: instance property of Project

project.scriptsDir

Kind: instance property of Project

project.configDir

Kind: instance property of Project

project.toolkitName

Kind: instance property of Project

project.toolkitRootDir

Kind: instance property of Project

project.name

Kind: instance property of Project

project.package

Kind: instance property of Project

project.isCompiled

Kind: instance property of Project

project.isTypeScript

Kind: instance property of Project

project.toolkitBin

Kind: instance property of Project

project.availableScripts

Kind: instance property of Project

project.fromRoot(...part) ⇒

Kind: instance method of Project
Returns: Path in root directory.

ParamDescription
...partPath relative to the root dir.

project.fromToolkitRoot(...part) ⇒

Kind: instance method of Project
Returns: Path in toolkit root directory.

ParamDescription
...partPath relative to the root dir of the toolkit.

project.fromConfigDir(...part) ⇒

Kind: instance method of Project
Returns: Path in config directory.

ParamDescription
...partPath relative to the config dir.

project.fromScriptsDir(...part) ⇒

Kind: instance method of Project
Returns: Path in scripts dir.

ParamDescription
...partPath relative to the scripts dir.

project.hasAnyDep(deps) ⇒

Kind: instance method of Project
Returns: Boolean value based on the existence of dependency in package.json.

ParamDescription
depsDependency or dependencies to check.

project.envIsSet(name) ⇒

Kind: instance method of Project
Returns: Whether the given environment variable is set.

ParamDescription
nameName of the environment variable to look for.

project.parseEnv(name, defaultValue) ⇒

Kind: instance method of Project
Returns: Environment variable or default value

ParamDescription
nameName of the environment variable to look for.
defaultValueDefault value if the environment variable is not net.

project.packageHas(jsonPath) ⇒

Kind: instance method of Project
Returns: Whether the given path is in the package file

ParamDescription
jsonPathThe path to check

project.packageGet(jsonPath) ⇒

Kind: instance method of Project
Returns: The value at the given path in the package file

ParamDescription
jsonPathThe path to get a value from

project.packageSet(jsonPath, value)

Kind: instance method of Project

ParamDescription
jsonPathThe path to get a value from
valueThe value to set at the path

project.hasScript(scriptFile) ⇒

Kind: instance method of Project
Returns: Full path of the script. Null if none is found.

ParamDescription
scriptFileScript file to check for the existance of.

project.hasAnyFile(fileNames)

Kind: instance method of Project

ParamDescription
fileNamesThe filename(s) including the extension to look for in the project root.

project.writeFile(fileName, data)

Kind: instance method of Project

ParamDescription
fileNameThe name of the file to be written
dataThe data to be written to the file. Objects that are provided will be serialized.

project.copyFile(sourceFile, newFile)

Kind: instance method of Project

ParamDescription
sourceFileThe path to the source file.
newFileThe path to the destination file.

project.bin(executable)

Kind: instance method of Project

ParamDescription
executableThe name of the executable.

project.getConcurrentlyArgs(scripts, killOthers) ⇒

Kind: instance method of Project
Returns:

ParamDescription
scriptsObject with script names as keys and commands as values.
killOthersWhether all scripts should be killed if one fails.

project.executeScriptFile(scriptFile, args)

Kind: instance method of Project

ParamDescription
scriptFileThe script file to execute from the "scripts" directory.
argsA list of arguments to be passed to the script function.

project.executeFromCLI(exit) ⇒

Kind: instance method of Project
Returns: Result of script

ParamDescription
exitWhether to exit from process.

project.execute(...executables) ⇒ IScriptResult

Kind: instance method of Project
Returns: IScriptResult -

ParamTypeDescription
...executablesExecutableExecutable or executables.

Example

// Execute some commands serially and concurrently. Commands in the object are executed concurrently.
// 1. `serial-command-1` is executed first.
// 2. `serial-command-2` is executed second.
// 3. Based on a condition, `serial-command-3` might be executed.
// 4. `build doc command`, `some-other-command`, and `tsc` are executed in parallel. (object keys are names used in logs)
// 5. Lastly, `other-serial-command` is executed.
const result = project.execute(
  ["serial-command-1", ["arg"]],
  "serial-command-2",
  someCondition ? "serial-command-3" : null,
  {
    my-parallel-job: ["build-doc-command", ["arg"],
    my-parallel-task: "some-other-command"
    builder: ["tsc", ["arg"]],
  },
  ["other-serial-command", ["arg"]],
);

printHelp(scriptNames)

Kind: global function

ParamDescription
scriptNamesThe list of available scripts.

replaceArgumentName(args, names, newName) ⇒

Kind: global function
Returns: An array with the arguments replaced.

ParamDescription
argsArguments array.
namesParameter names to look for in arguments.
newNameParameter names to look for in arguments.

Example

const arguments = ["--a", "--b"];
replaceArgumentName(arguments, ["--a"], "--c"); // -> ["--c", "--b"]