
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
@handy-common-utils/dev-utils
Advanced tools
Tool chain utilities for the convenience of developers.
Normally you don't use this package directly (although it is perfectly fine to use this package alone).
Instead, you add @handy-common-utils/dev-dependenciesas a dev dependency (which in turn depends on this package):
npm install -D @handy-common-utils/dev-dependencies
After installation, the command line generate-api-docs-and-update-readme will be available in your project.
This command line can update the <!-- API start -->...<!-- API end --> section
in your README.md with generated API documentation.
You can use optional command line arguments to customise the behaviour of generate-api-docs-and-update-readme:
README.md.,). Default: ./src.api-docs.These arguments must be specified in the order as shown above.
const info = await DevUtils.getGitInfo(); is how you can get Git repository related information,
such as repository, branch, commit id, tags, etc.
It relies on the Git command line tool ('git') to have already been installed.
The function accepts two optional arguments. The first one allows you to specify which properties/info to return. The second one allows you to control whether environment variables should be checked. By default all properties/info would be returned and environment variables would be checked before falling back to checking local Git repository.
Checking environment variables is handy when you use it in a build/CI/CD pipeline.
Below is an example output:
{
message: 'user and email could be undefined in CI',
email: 'james.hu.ustc@hotmail.com',
user: 'James Hu',
describeLight: 'v1.0.26-3-g6cfaeb8',
describe: 'v1.0.26-3-g6cfaeb8',
isDirty: true,
branch: 'feature/git-info',
commitIdLong: '6cfaeb8dade5c374c5dcc1e440795aeab3951fa9',
commitIdShort: '6cfaeb8',
repository: 'dev-utils',
messageBody: '',
messageSubject: 'user and email could be undefined in CI',
tag: '6cfaeb8',
tags: [ '6cfaeb8' ]
}
Details of the properties in this structure can be found in the documentation of GitInfo interface.
The functionality depends on serverless-plugin-git-variables. Check it out if you are interested.
The function loadConfiguration(...) / DevUtils.loadConfiguration(...) would be handy if you need to read configuration from YAML and/or JSON files.
If you have multiple variants of the configurations, such like settings.default.yml, settings.staging.yml, settings.production.yml,
you can use loadConfigurationWithVariant(...) or DevUtils.loadConfigurationWithVariant(...) instead.
loadConfiguration(...) / DevUtils.loadConfiguration(...) has these features:
.yaml, .yml and .json files,
but you can customise file extensions and the order they override each other. You can also customise it to pick up different versions of
configuration files, such like: {'-v3.yml': 'yaml', '-v2.yml': 'yaml', '.json': 'json'}options.shouldCheckAncestorDir for customising this behaviour. If configuration files exit in both parent and child directories, the configuration
in the child directory overrides that in the parent directory.options.merge.undefined.loadConfigurationWithVariant(...) / DevUtils.loadConfigurationWithVariant(...) has these additional features:
Code examples:
// Pick up and merge (those to the left overrides those to the right) configurations from: ./my-config.yaml, ./my-config.yml, ./my-config.json
const config = DevUtils.loadConfiguration('my-config');
// Pick up and merge ./my-config.default.{yaml,yml,json} with ./my-config.prod.{yaml,yml,json}
const config = DevUtils.loadConfigurationWithVariant('my-config', '.prod');
// Pick up and merge ./my-config-production.{yaml,yml,json} with ./my-config.{yaml,yml,json}
const config = DevUtils.loadConfigurationWithVariant('my-config', '-production', '');
// Let .json override .yml and don't try to pick up .yaml
const config = DevUtils.loadConfiguration('my-config', { extensions: {
'.json': 'json',
'.yml': 'yaml',
} });
// Pickup and merge (v3 overrides v2 overrides legacy) my-config-v3.yml, my-config-v2.yml, my-config.json
const config = DevUtils.loadConfiguration('my-config', { extensions: {
'-v3.yml': 'yaml',
'-v2.yml': 'yaml',
'.json': 'json',
} });
// Search in parent, grand parent, and great grand parent directories as well
const config = DevUtils.loadConfiguration(
'my-config',
{
dir: 'test/fixtures/dir_L1/dir_L2/dir_L3/dir_L4',
shouldCheckAncestorDir: (level, _dirName, _dirAbsolutePath) => level <= 3,
},
);
Please note that for avoiding peer dependency serverless to be included,
bundled dependency serverless-plugin-git-variables was installed with additional option:
npm i serverless-plugin-git-variables --legacy-peer-deps
| Class | Description |
|---|---|
| DevUtils | - |
| Interface | Description |
|---|---|
| GitInfo | Git related information. See https://github.com/jacob-meacham/serverless-plugin-git-variables |
| LoadConfigurationOptions | Options for loadConfiguration(...) function |
| Type Alias | Description |
|---|---|
| GitInfoKey | - |
abstract DevUtilsnew DevUtils():
DevUtils
DevUtils
| Property | Modifier | Type | Description |
|---|---|---|---|
DEFAULT_OPTIONS_FOR_LOAD_CONFIGURATION | readonly | LoadConfigurationOptions | Default options for loadConfiguration(...) |
staticgenerateApiDocsAndUpdateReadme(readmeLocation,entryPoints,apiDocDir,typeDocOptions?):Promise<void>
Generate API documentation and insert it into README.md file.
| Parameter | Type | Default value | Description |
|---|---|---|---|
readmeLocation | string | README_MD_FILE | location of the README.md file |
entryPoints | string[] | ... | Entry points for generating API documentation |
apiDocDir | string | API_DOCS_DIR | temporary directory for storing intermediate documentation files |
typeDocOptions? | Partial<Omit<TypeDocOptions, "entryPoints" | "out">> | undefined | Options for TypeDoc |
Promise<void>
Promise of void
DevUtils.generateApiDocsAndUpdateReadme(readmePath, entryPoints, apiDocDir);
staticgenerateApiDocsMd(entryPoints,apiDocDir,options?):Promise<void>
| Parameter | Type | Default value |
|---|---|---|
entryPoints | string[] | ... |
apiDocDir | string | API_DOCS_DIR |
options? | Partial<Omit<TypeDocOptions, "entryPoints" | "out">> | undefined |
Promise<void>
staticgetGitInfo(whitelistKeys?,checkEnvironmentVariables?,reportErrors?):Promise<Partial<GitInfo>>
Get Git related information. This function relies on the existence of Git command line.
By default all possible information will be returned, but this can be overridden by
specifying whitelistKeys argument.
If checkEnvironmentVariables argument is true, then environment variables GIT_COMMIT and GITHUB_SHA
will be checked before trying to identify the commit ID from local Git repository,
also environment variables GIT_LOCAL_BRANCH, GIT_BRANCH, BRANCH_NAME, GITHUB_REF_NAME
will be checked before trying to identify the branch name from local Git repository.
This function never throws Error. For example, if user and email have not been configured, they would be undefined in the returned object.
| Parameter | Type | Default value | Description |
|---|---|---|---|
whitelistKeys? | keyof GitInfo[] | undefined | keys (property names) in the returned object that values need to be populated |
checkEnvironmentVariables? | boolean | true | true (default value) if environment variables should be checked |
reportErrors? | boolean | false | true if errors should be reported in the errors: any[] property of the returned object |
Promise<Partial<GitInfo>>
Git related information
staticloadConfiguration<T>(fileNameBase,overrideOptions?):undefined|T
Load configuration from YAML and/or JSON files. This function is capable of reading multiple configuration files from the same directory and optionally its ancestor directories, and combine the configurations.
Internal logic of this function is: \
${dir}${options.extensions.<key>} as type options.extensions.<value> options.extensions overrides those at the bottom options.extensions is: ".yaml" as YAML, ".yml" as YAML, ".json" as JSON. You can override it. \options.shouldCheckAncestorDir function to decide if parent directory should be checked. options.shouldCheckAncestorDir always returns false. You can override it. Other options:
encoding: encoding used when reading the file, default is 'utf8'
merge: the function for merging configurations, the default implementation uses lodash/merge \
| Type Parameter | Default type |
|---|---|
T | any |
| Parameter | Type | Description |
|---|---|---|
fileNameBase | string | Base part of the file name, usually this is the file name without extension, but you can also be creative. |
overrideOptions? | Partial<LoadConfigurationOptions<T>> | Options that would be combined with default options. |
undefined | T
The combined configuration, or undefined if no configuration file can be found/read.
// Pick up and merge (those to the left overrides those to the right) configurations from: ./my-config.yaml, ./my-config.yml, ./my-config.json
const config = DevUtils.loadConfiguration('my-config');
// Let .json override .yml and don't try to pick up .yaml
const config = DevUtils.loadConfiguration('my-config', { extensions: {
'.json': 'json',
'.yml': 'yaml',
} });
// Search in parent, grand parent, and great grand parent directories as well
const config = DevUtils.loadConfiguration(
'my-config',
{
dir: 'test/fixtures/dir_L1/dir_L2/dir_L3/dir_L4',
shouldCheckAncestorDir: (level, _dirName, _dirAbsolutePath) => level <= 3,
},
);
staticloadConfigurationWithVariant<T>(fileNameBase,variant,baseVariant,overrideOptions?):undefined|T
Load configuration from YAML and/or JSON files with variant suffix. This function is capable of reading multiple configuration files from the same directory and optionally its ancestor directories, and combine the configurations. This function is based on loadConfiguration. It picks up the specified variant configuration and the base variant configuration from a directory and optionally its ancestor directories, then merge them by overriding the base variant configuration with the specified variant configuration.
| Type Parameter | Default type |
|---|---|
T | any |
| Parameter | Type | Default value | Description |
|---|---|---|---|
fileNameBase | string | undefined | Base part of the file name, for example, my-config, settings. |
variant | string | undefined | Part of the file name that identifies the variant, for example, .staging, -production, _test. When searching for configuration files, it would be inserted between the fileNameBase and the file type suffix. |
baseVariant | string | '.default' | Part of the file name that identifies the base variant. The default value is .default. When searching for configuration files, it would be inserted between the fileNameBase and the file type suffix. |
overrideOptions? | Partial<LoadConfigurationOptions<T>> | undefined | Options that would be combined with default options. |
undefined | T
The combined configuration, or undefined if no configuration file can be found/read.
Git related information. See https://github.com/jacob-meacham/serverless-plugin-git-variables
Options for loadConfiguration(...) function
| Type Parameter | Default type |
|---|---|
T | any |
GitInfoKey = keyof
GitInfo
FAQs
Utilities for the convenience of developers
We found that @handy-common-utils/dev-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.