project-helper
Helper for node projects
- Description
- Synopsis
- API
- Classes
- Typedefs
- Project
- new Project([moduleName], options, debug)
- project.package :
TrackedObject
- project.name :
string
- project.root :
string
- project.moduleName :
string
- project.moduleRoot :
string
- project.track :
boolean
- project.config :
object
- project.load() ⇒
void
- project.save() ⇒
Promise.<void>
- project.resolveModule() ⇒
Promise.<string>
- project.resolveBin(modName, [options], [executable], [cwd]) ⇒
Promise.<string>
- project.fromRoot(...part) ⇒
string
- project.fromModuleRoot(...part) ⇒
string
- project.hasFile(files, [t], [f]) ⇒
Promise.<*>
- project.hasAnyDep(deps, [t], [f]) ⇒
*
- project.envIsSet(name) ⇒
boolean
- project.parseEnv(name, defaultValue) ⇒
*
- project.getConcurrentlyArgs(scripts, [options], [killOthers]) ⇒
Array.<string>
- project.isOptedOut(key, [t], [f]) ⇒
Promise.<*>
- project.isOptedIn(key, [t], [f]) ⇒
Promise.<*>
- project.getFileDetails(projectFile, options) ⇒
Promise.<FileDetail>
- project.createSymLink(moduleFile, projectFile, [options]) ⇒
Promise.<void>
- project.getFileHash(projectFile) ⇒
string
- project.getDataFile(projectFile, [options]) ⇒
Promise.<void>
- project.readFile(projectFile, [options]) ⇒
Promise.<*>
- project.writeFile(projectFile, data, [options]) ⇒
Promise.<void>
- project.copyFile(moduleFile, projectFile, [options]) ⇒
Promise.<void>
- Project~FileDetail :
Object
- DataObject
- TrackedObject
- new TrackedObject([data], [registry], [options])
- trackedObject.registry :
Object
- trackedObject.isChanged :
boolean
- trackedObject.data :
Object
- trackedObject.set(key, value, [options]) ⇒
this
- trackedObject.setObject(data, [options]) ⇒
this
- trackedObject.remove(key, [options]) ⇒
this
- trackedObject.has(props, [t], [f]) ⇒
*
- trackedObject.hasSubProp(prop, subProps, [t], [f]) ⇒
*
- trackedObject.get(key) ⇒
*
- Format :
"json"
|"yaml"
|"auto"
Description
Synopsis
API
Classes
- Project
Project class
- DataObject
This class is used for modifications of the given object.
- TrackedObject
This class is used for controlled modifications of the given object. It uses a data object called registry, which stores modifications made by this library. Modifies only data not created or altered by user. Optionally changes may be forced.
Typedefs
- Format :
"json"
|"yaml"
|"auto"
Project
Project class
Kind: global class
- Project
- new Project([moduleName], options, debug)
- instance
- .package :
TrackedObject
- .name :
string
- .root :
string
- .moduleName :
string
- .moduleRoot :
string
- .track :
boolean
- .config :
object
- .load() ⇒
void
- .save() ⇒
Promise.<void>
- .resolveModule() ⇒
Promise.<string>
- .resolveBin(modName, [options], [executable], [cwd]) ⇒
Promise.<string>
- .fromRoot(...part) ⇒
string
- .fromModuleRoot(...part) ⇒
string
- .hasFile(files, [t], [f]) ⇒
Promise.<*>
- .hasAnyDep(deps, [t], [f]) ⇒
*
- .envIsSet(name) ⇒
boolean
- .parseEnv(name, defaultValue) ⇒
*
- .getConcurrentlyArgs(scripts, [options], [killOthers]) ⇒
Array.<string>
- .isOptedOut(key, [t], [f]) ⇒
Promise.<*>
- .isOptedIn(key, [t], [f]) ⇒
Promise.<*>
- .getFileDetails(projectFile, options) ⇒
Promise.<FileDetail>
- .createSymLink(moduleFile, projectFile, [options]) ⇒
Promise.<void>
- .getFileHash(projectFile) ⇒
string
- .getDataFile(projectFile, [options]) ⇒
Promise.<void>
- .readFile(projectFile, [options]) ⇒
Promise.<*>
- .writeFile(projectFile, data, [options]) ⇒
Promise.<void>
- .copyFile(moduleFile, projectFile, [options]) ⇒
Promise.<void>
- .package :
- inner
- ~FileDetail :
Object
- ~FileDetail :
new Project([moduleName], options, debug)
Param | Type | Default | Description |
---|---|---|---|
[moduleName] | string |
"[dir of module]" |
Module name which provides configuration services to project using this library. Defaults to directory name. |
options | Object |
Options |
|
debug | boolean |
Adds debug data to instance. |
TrackedObject
project.package : Object which contains data of package.json of project.
Kind: instance property of Project
string
project.name : Module name which provides configuration services to project using this library.
Kind: instance property of Project
Read only: true
string
project.root : Root directory path of project.
Kind: instance property of Project
Read only: true
string
project.moduleName : Module name which provides configuration services to project using this library.
Kind: instance property of Project
Read only: true
string
project.moduleRoot : Root directory path of module which provides configuration services to project using this library.
Kind: instance property of Project
Read only: true
boolean
project.track : Whteher files of the project are tracked to determine if they are created by the module.
Kind: instance property of Project
Read only: true
object
project.config : Configuration for module.
Kind: instance property of Project
Read only: true
void
project.load() ⇒ Finds project path, loads necessary files (i.e. configuration, registry etc.)
Kind: instance method of Project
Throws:
VError
Promise.<void>
project.save() ⇒ Saves any changes made to disk.
Kind: instance method of Project
Returns: Promise.<void>
-
VError
Promise.<string>
project.resolveModule() ⇒ Finds and returns path to module's binary. (Module which requires this library)
Kind: instance method of Project
Returns: Promise.<string>
-
Promise.<string>
project.resolveBin(modName, [options], [executable], [cwd]) ⇒ Finds and returns path of given command.
Kind: instance method of Project
Returns: Promise.<string>
-
Error
Param | Type | Default | Description |
---|---|---|---|
modName | string |
Module name to find executable from. |
|
[options] | Object |
Options. |
|
[executable] | string |
"param.modName" |
Executable name. (Defaults to module name) |
[cwd] | string |
"process.cwd()" |
Current working directory |
string
project.fromRoot(...part) ⇒ Joins parts to form a path using path.join
. Returns path in project by adding project root at the beginning of path.
Kind: instance method of Project
Returns: string
-
Param | Type | Description |
---|---|---|
...part | Array.<string> |
Path parts to get path relative to project root. |
string
project.fromModuleRoot(...part) ⇒ Joins parts to form a path using path.join
. Returns path in project by adding project root at the beginning of path.
Kind: instance method of Project
Returns: string
-
Param | Type | Description |
---|---|---|
...part | Array.<string> |
Path parts to get path relative to project root. |
Promise.<*>
project.hasFile(files, [t], [f]) ⇒ Returns one of the given values based on existence of given file path or file paths in project root. Normalizes file path separators based on operating system.
Kind: instance method of Project
Returns: Promise.<*>
-
t
or f
value based on existence of files in project.Param | Type | Default | Description |
---|---|---|---|
files | string | Array.<string> |
File or list of files to look in project. |
|
[t] | * |
true |
Value to return if some of the files exists in project. |
[f] | * |
false |
Value to return if none of the files exists in project. |
*
project.hasAnyDep(deps, [t], [f]) ⇒ Returns one of the given values based on whether project has any of the given dependencies in dependencies
, devDependencies
, peerDependencies
.
Kind: instance method of Project
Returns: *
-
t
or f
value based on existence of sub property.Param | Type | Default | Description |
---|---|---|---|
deps | string | Array.<string> |
Dependency or dependencies to check. |
|
[t] | * |
true |
Value to return if any of dependencies exists. |
[f] | * |
false |
Value to return if none of dependencies exists.ts. |
boolean
project.envIsSet(name) ⇒ Returns whether given environment variable is set and not empty.
Kind: instance method of Project
Returns: boolean
-
Param | Type | Description |
---|---|---|
name | string |
Name of the environment variable to look for. |
*
project.parseEnv(name, defaultValue) ⇒ Returns environment variable if it exists and is not empty. Returns given default value otherwise.
Kind: instance method of Project
Returns: *
-
Param | Type | Description |
---|---|---|
name | string |
Name of the environment variable |
defaultValue | * |
Default value to return if no environment variable is set or is empty. |
Array.<string>
project.getConcurrentlyArgs(scripts, [options], [killOthers]) ⇒ Given an object containing keys as script names, values as commands, returns parameters to feed to concurrently.
Kind: instance method of Project
Returns: Array.<string>
-
Param | Type | Default | Description |
---|---|---|---|
scripts | Object.<string, string> |
Object with script names as keys, commands as values. |
|
[options] | Object |
Options |
|
[killOthers] | boolean |
true |
Whether -kill-others-on-fail should added. |
Promise.<*>
project.isOptedOut(key, [t], [f]) ⇒ Returns whether given parameter is opted out.
Kind: instance method of Project
Returns: Promise.<*>
-
t
or f
value based on existence of sub property.Param | Type | Default | Description |
---|---|---|---|
key | string |
Paremeter to look for. |
|
[t] | * |
true |
Value to return if it is opted out. |
[f] | * |
false |
Value to return if it is not opted out. |
Promise.<*>
project.isOptedIn(key, [t], [f]) ⇒ Returns whether given parameter is opted in.
Kind: instance method of Project
Returns: Promise.<*>
-
t
or f
value based on existence of sub property.Param | Type | Default | Description |
---|---|---|---|
key | string |
Paremeter to look for. |
|
[t] | * |
true |
Value to return if it is opted in. |
[f] | * |
false |
Value to return if it is not opted in. |
Promise.<FileDetail>
project.getFileDetails(projectFile, options) ⇒ Returns file details related to given file path relative to project root.
Kind: instance method of Project
Returns: Promise.<FileDetail>
-
VError
Param | Type | Description |
---|---|---|
projectFile | string |
Project file to get detail for. |
options | Object |
Options |
options.force | boolean |
Assume safe to operate on file even it is not auto created. |
options.track | boolean |
Whether to track file if it is created by module. |
Promise.<void>
project.createSymLink(moduleFile, projectFile, [options]) ⇒ Creates a symbolic link in project which points to a file in module. Removes previously created symbolic link created by this library.
Kind: instance method of Project
Returns: Promise.<void>
-
VError
Param | Type | Default | Description |
---|---|---|---|
moduleFile | string |
Target file which link points to. Should be given relative to module root. |
|
projectFile | string |
Link file. Should be given relative to project root. |
|
[options] | Object |
Options |
|
[options.force] | boolean |
false |
Writes file even it exists and not auto created. |
[options.track] | boolean |
[this.track] |
Whether to track symlink if it is created by module. |
Example
// Creates tsconfig.json symbolic link file in project root, pointing to a file from toolkit.;
string
project.getFileHash(projectFile) ⇒ Calculates and returns hash for given file relative to project root.
Kind: instance method of Project
Returns: string
-
Param | Type | Description |
---|---|---|
projectFile | string |
File path of the file relative to project root to calculate hash for. |
Promise.<void>
project.getDataFile(projectFile, [options]) ⇒ Kind: instance method of Project
Throws:
VError
Param | Type | Default | Description |
---|---|---|---|
projectFile | string |
File path to read relative to project root. |
|
[options] | Object |
Options |
|
[options.format] | boolean |
[file extension] |
Format to serialize data in given format. ( |
[options.create] | boolean |
Whether to create file if it does not exist. |
|
[options.createFormat] | boolean |
Format to serialize data in given format. ( |
|
[options.force] | boolean |
false |
Whether to force write file even it exist. |
Promise.<*>
project.readFile(projectFile, [options]) ⇒ Reads and returns content of the given file relative to project root. Optionally can create file if it does not exist.
Kind: instance method of Project
Returns: Promise.<*>
-
VError
Param | Type | Default | Description |
---|---|---|---|
projectFile | string |
File to read relative to project root. |
|
[options] | Object |
Options |
|
[options.create] | boolean |
Whether to create file if it does not exist. |
|
[options.track] | boolean |
[this.track] |
Whether to track file in registry if it is created by module. |
[options.force] | boolean |
false |
Whether to force create even it is deleted by user. |
[options.defaultContent] | * |
Default content to write if file does not exist. |
|
[options.errorNotExists] | boolean |
true |
Throw error if file does not exist. |
[options.parse] | boolean |
false |
Whether to parse file content to create a js object. |
[options.format] | Format |
[file extension] |
Format to use parsing data. |
[options.createFormat] | Format |
json |
Format to be used while creating nonexisting file if no format is provided. |
Promise.<void>
project.writeFile(projectFile, data, [options]) ⇒ Creates and writes given data to a file in project.
Kind: instance method of Project
Returns: Promise.<void>
-
VError
Param | Type | Default | Description |
---|---|---|---|
projectFile | string |
File path to relative to project root. |
|
data | string |
Data to write |
|
[options] | Object |
Options |
|
[options.force] | boolean |
false |
Writes file even it exists and not auto created. |
[options.track] | boolean |
[this.track] |
Whether to track file in registry if it is created by module. |
[options.serialize] | boolean |
false |
Whether to serialize object before written to file. |
[options.format] | Format |
[file extension] |
Format to use serializing data. |
Example
project; // Writes given data to some-config.json in project root.
Promise.<void>
project.copyFile(moduleFile, projectFile, [options]) ⇒ Copies a file from module to project. Paths should be given relative to module root and project root.
Kind: instance method of Project
Returns: Promise.<void>
-
VError
Param | Type | Default | Description |
---|---|---|---|
moduleFile | string |
Source file path relative to module root. |
|
projectFile | string |
Destinantion file path relative to paroject root. |
|
[options] | Object |
Options |
|
[options.force] | boolean |
false |
Overwrites file even it exists and not auto created. |
[options.track] | boolean |
[this.track] |
Whether to track file in registry if it is created by module. |
Example
project; // Copies some-config.json file from module's root to project's root.
Object
Project~FileDetail : Type to store file details.
Kind: inner typedef of Project
Properties
Name | Type | Description |
---|---|---|
isSafe | boolean |
Whether it is safe to delete or change given file. (Is true if file is created automatically by module and is not modified by user.) |
[stats] | fs.Stats |
|
[linkTarget] | string |
If file is a symbolic link, target path relative to module root. |
DataObject
This class is used for modifications of the given object.
Kind: global class
- DataObject
- new DataObject([data], [options])
- .isChanged :
boolean
- .data :
Object
- .has(props, [t], [f]) ⇒
*
- .hasSubProp(prop, subProps, [t], [f]) ⇒
*
- .get(key) ⇒
*
- .set(key, value) ⇒
this
- .setObject(data) ⇒
this
- .remove(key) ⇒
this
new DataObject([data], [options])
Creates an instance of DataObject.
Param | Type | Default | Description |
---|---|---|---|
[data] | Object |
{} |
Data to be modified. |
[options] | Object |
Options |
|
[options.file] | string |
Path of the file to be used in logs. |
|
[options.logger] | Logger |
A looger instance such as winston. Must implement |
boolean
dataObject.isChanged : Whether data is changed.
Kind: instance property of DataObject
Read only: true
Object
dataObject.data : Stored data.
Kind: instance property of DataObject
Read only: true
*
dataObject.has(props, [t], [f]) ⇒ Returns one of the given values based on whether some of given property or properties exists in given object.
Property names may be given as chained such as key
or key.subkey
.
Kind: instance method of DataObject
Returns: *
-
t
or f
value based on existence of property.Param | Type | Default | Description |
---|---|---|---|
props | string | Array.<string> |
Property or properties to look in package.json |
|
[t] | * |
true |
Value to return if some of the properties exists in project. |
[f] | * |
false |
Value to return if none of the properties exists in project. |
Example
const result = project;
*
dataObject.hasSubProp(prop, subProps, [t], [f]) ⇒ Returns one of the given values based on whether some of given property path or property paths exists in object's target property path.
Property names may be given as chained such as key
or key.subkey
.
Kind: instance method of DataObject
Returns: *
-
t
or f
value based on existence of sub property.Param | Type | Default | Description |
---|---|---|---|
prop | string |
Property or properties to look in package.json |
|
subProps | string | Array.<string> |
Property or properties to look in package.json |
|
[t] | * |
true |
Value to return if some of the properties exists. |
[f] | * |
false |
Value to return if none of the properties exists. |
Example
const result = project;const result2 = project;
*
dataObject.get(key) ⇒ Returns data in given key or path. Path may be given as chained. (i.e "scripts.compile")
Kind: instance method of DataObject
Returns: *
-
Param | Type | Description |
---|---|---|
key | string |
Key to get data from |
this
dataObject.set(key, value) ⇒ Stores given data at given key or path. Based on force option, does not change value if it is not created automatically by this library by looking registry. Path may be given as chained. (i.e "scripts.compile")
Kind: instance method of DataObject
Returns: this
-
Param | Type | Description |
---|---|---|
key | string |
Key to store data at. |
value | * |
Value to store at given key. |
this
dataObject.setObject(data) ⇒ Stores each key and its value in the object. Key's may be given as chained paths such as scripts.compile
.
Kind: instance method of DataObject
Returns: this
-
Param | Type | Description |
---|---|---|
data | Object |
Data to store at object. |
Example
data;
this
dataObject.remove(key) ⇒ Removes given path or paths from object . Based on force option, does not remove value if it is not created automatically by this library by looking registry. Path may be given as chained. (i.e "scripts.compile")
Kind: instance method of DataObject
Returns: this
-
Param | Type | Description |
---|---|---|
key | string | Array.<string> |
Key or list of keys to remove |
TrackedObject
This class is used for controlled modifications of the given object. It uses a data object called registry, which stores modifications made by this library. Modifies only data not created or altered by user. Optionally changes may be forced.
Kind: global class
- TrackedObject
- new TrackedObject([data], [registry], [options])
- .registry :
Object
- .isChanged :
boolean
- .data :
Object
- .set(key, value, [options]) ⇒
this
- .setObject(data, [options]) ⇒
this
- .remove(key, [options]) ⇒
this
- .has(props, [t], [f]) ⇒
*
- .hasSubProp(prop, subProps, [t], [f]) ⇒
*
- .get(key) ⇒
*
new TrackedObject([data], [registry], [options])
Creates an instance of TrackedObject.
Param | Type | Default | Description |
---|---|---|---|
[data] | Object |
{} |
Data to be modified. |
[registry] | Object |
{} |
Registry object to track changes made by this library. This object stores copies of the data modified by this library. |
[options] | Object |
Options |
|
[options.file] | string |
Path of the file to be used in logs. |
|
[options.logger] | Logger |
A looger instance such as winston. Must implement |
Object
trackedObject.registry : Registry object
Kind: instance property of TrackedObject
Read only: true
boolean
trackedObject.isChanged : Whether data is changed.
Kind: instance property of TrackedObject
Read only: true
Object
trackedObject.data : Stored data.
Kind: instance property of TrackedObject
Read only: true
this
trackedObject.set(key, value, [options]) ⇒ Stores given data at given key or path. Based on force option, does not change value if it is not created automatically by this library by looking registry. Path may be given as chained. (i.e "scripts.compile")
Kind: instance method of TrackedObject
Overrides: set
Returns: this
-
Param | Type | Default | Description |
---|---|---|---|
key | string |
Key to store data at. |
|
value | * |
Value to store at given key. |
|
[options] | Object |
Options |
|
[options.force] | boolean |
false |
Whether to force change even value is altered by user manually. |
this
trackedObject.setObject(data, [options]) ⇒ Stores each key and its value in the object. Key's may be given as chained paths such as scripts.compile
.
Kind: instance method of TrackedObject
Overrides: setObject
Returns: this
-
Param | Type | Default | Description |
---|---|---|---|
data | Object |
Data to store at object. |
|
[options] | Object |
Options |
|
[options.force] | boolean |
false |
Whether to force change even value is altered by user manually. |
Example
data;
this
trackedObject.remove(key, [options]) ⇒ Removes given path or paths from object . Based on force option, does not remove value if it is not created automatically by this library by looking registry. Path may be given as chained. (i.e "scripts.compile")
Kind: instance method of TrackedObject
Overrides: remove
Returns: this
-
Param | Type | Default | Description |
---|---|---|---|
key | string | Array.<string> |
Key or list of keys to remove |
|
[options] | object |
Options |
|
[options.force] | boolean |
false |
Whether to force removal even value is altered by user manually. |
*
trackedObject.has(props, [t], [f]) ⇒ Returns one of the given values based on whether some of given property or properties exists in given object.
Property names may be given as chained such as key
or key.subkey
.
Kind: instance method of TrackedObject
Returns: *
-
t
or f
value based on existence of property.Param | Type | Default | Description |
---|---|---|---|
props | string | Array.<string> |
Property or properties to look in package.json |
|
[t] | * |
true |
Value to return if some of the properties exists in project. |
[f] | * |
false |
Value to return if none of the properties exists in project. |
Example
const result = project;
*
trackedObject.hasSubProp(prop, subProps, [t], [f]) ⇒ Returns one of the given values based on whether some of given property path or property paths exists in object's target property path.
Property names may be given as chained such as key
or key.subkey
.
Kind: instance method of TrackedObject
Returns: *
-
t
or f
value based on existence of sub property.Param | Type | Default | Description |
---|---|---|---|
prop | string |
Property or properties to look in package.json |
|
subProps | string | Array.<string> |
Property or properties to look in package.json |
|
[t] | * |
true |
Value to return if some of the properties exists. |
[f] | * |
false |
Value to return if none of the properties exists. |
Example
const result = project;const result2 = project;
*
trackedObject.get(key) ⇒ Returns data in given key or path. Path may be given as chained. (i.e "scripts.compile")
Kind: instance method of TrackedObject
Returns: *
-
Param | Type | Description |
---|---|---|
key | string |
Key to get data from |
"json"
| "yaml"
| "auto"
Format : Kind: global typedef