Run the following command to start using node-scripts in your projects:
npm i @alessiofrittoli/node-scriptsor using pnpm
pnpm i @alessiofrittoli/node-scriptsRun the following to start development
nvm use
pnpm iRun the following
pnpm devThis will generate unminified output code, sourcemaps and will enable source file watcher.
Run the following to create a local production build
pnpm buildThis will generate minified output code without sourcemaps.
Run all the defined test suites by running the following:
# Run tests and watch file changes.
pnpm test:watch
# Run tests in a CI environment.
pnpm test:ciAn HTTP server is then started to serve coverage files from ./coverage folder.
pnpm test:coverage:serveContributions are truly welcome!
Please refer to the Contributing Doc for more information on how to start contributing to this project.
Help keep this project up to date with GitHub Sponsor.
If you believe you have found a security vulnerability, we encourage you to responsibly disclose this and NOT open a public issue. We will investigate all legitimate reports. Email security@alessiofrittoli.it to disclose any security vulnerabilities.
The addTypesReference function allows you to create and manage TypeScript reference files and update the related tsconfig.json file for a project installing your node module.
Below are the detailed descriptions of the interfaces and functions included.
Properties
| Property | Type | Description |
|---|---|---|
root |
string |
The root directory of the project which is installing your node module. |
name |
string |
The name of your node module. |
outputFile |
string |
The output file name. |
Properties
| Property | Type | Default | Description |
|---|---|---|---|
name |
string |
- | The project name currently executing the script. |
scopes |
string[] |
- | An array of scopes references used over package name. |
outputFile |
string |
'globals.d.ts' | The *.d.ts output file name. |
Creates or updates a reference file with type definitions for a project.
Parameters
| Parameter | Type | Description |
|---|---|---|
options |
CommonOptions |
Common options for the reference file creation. |
- See CommonOptions interface.
Throws
Error - Throws an error if there is an issue creating or updating the file.
Updates the tsconfig.json file by adding the specified output file to the include array.
Parameters
| Parameter | Type | Description |
|---|---|---|
options |
CommonOptions |
Common options for the reference file creation. |
- See CommonOptions interface.
Throws
Error- Throws an error if the tsconfig.json file cannot be read or updated.
Adds a TypeScript reference file and updates the tsconfig.json for the project installing your node module.
If the options.outputFile already exists, it will be updated with the new package reference if not already in there.
Parameters
| Parameter | Type | Description |
|---|---|---|
options |
AddTypesReferenceOptions |
The options for adding the types reference. |
- See AddTypesReferenceOptions interface.
Error
Exit the process with code 1 on failure.
Add Types Reference Example usage
Add the postinstall script in your package.json file which will execute the script once your package get installed in an external project.
{
// ...
"files": [
// ...,
"my-scripts" // ensure folder is published to `npm`
],
"scripts": {
// ...
"postinstall": "node my-scripts/ts-setup.ts"
}
}Then in your ts-setup.ts file simply import the script and execute it with a few options.
// my-scripts/ts-setup.ts
import { addTypesReference } from '@alessiofrittoli/node-scripts/postinstall'
import packageJson from '../package.json'
addTypesReference({
name: packageJson.name,
outputFile: `${packageJson.name}.d.ts`, // optional
})Or you can statically pass a outputFile to add all your scoped packages in a single file.
// my-scripts/ts-setup.ts
import { addTypesReference } from '@alessiofrittoli/node-scripts/postinstall'
import packageJson from '../package.json'
addTypesReference({
name: packageJson.name,
outputFile: 'my-package-scope-env.d.ts',
})Add custom types references
// my-scripts/ts-setup.ts
import { addTypesReference } from '@alessiofrittoli/node-scripts/postinstall'
import packageJson from '../package.json'
addTypesReference({
name: packageJson.name,
types: [`${packageJson.name}/globals`, `${packageJson.name}/globals-2`],
outputFile: `${packageJson.name}.d.ts`, // optional
})The release function automates the process of building, tagging, and optionally releasing a project to npm.
This function either works with process options (passed via CLI) or function arguments (function arguments takes precedence over process options).
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
version |
string |
--version process option or package.json. |
The version to release. |
Retrieved from --version process option or package.json if omitted. |
|||
build |
string |
'build' | A custom build command that will build your project before publish. |
Retrieved from --build process option or fallback to build if omitted. |
|||
verbose |
boolean |
false | Enables detailed logging. |
Retrieved from --verbose process option or fallback to false if omitted. |
|||
origin |
string |
'origin' | The Git origin name used for pushing version tags. |
Retrieved from --origin or --o process option or fallback to origin if omitted. |
|||
npm |
boolean |
false | Indicates whether to publish the package to npm. |
Retrieved from --npm process option or fallback to false if omitted. |
|||
access |
public|restricted |
'public' | Sets npm package access level. |
Retrieved from --access process option or fallback to public if omitted. |
Process Options - CLI
| Option | Type | Default | Description |
|---|---|---|---|
--version |
string |
Value from package.json | The version to release. Retrieved from package.json if omitted. |
| Retrieved from package.json if omitted. | |||
--build |
string |
build |
A custom build command that will build your project before publish. |
--verbose |
boolean |
false |
Enables detailed logging. |
--origin, -o |
string |
'origin' | The Git origin name used for pushing version tags. |
--npm |
boolean |
false |
Indicates whether to publish the package to npm. |
--access |
public|restricted |
'public' | Sets npm package access level. |
Performed steps
-
Retrieve package.json:
- Attempts to load and parse the
package.jsonfile. - Exits the process with code "1" if the file is unavailable or invalid.
- Retrieve the version to use as fallback if no
--versionoption has been provided.
- Attempts to load and parse the
-
Parse Options:
- Retrieves CLI options using
getProcessOptions(). - Validates critical parameters such as
versionandaccess.
- Retrieves CLI options using
-
Prepare Git and Build:
- Stashes any uncommitted changes with a stash name (
pre-release). - Executes the
npm run buildorpnpm buildcommand (ifpnpmis globally installed). - Create the Git Tag as
v{version} - Push the Git Tag the the specified
originor to the default Git Repository Remote.
- Stashes any uncommitted changes with a stash name (
-
Publish to npm (Optional):
- Publishes the package using
npm publishif the--npmflag is set.
- Publishes the package using
-
Restore Stash:
- Restores the stashed changes if any were saved during the process.
-
Verbose Logging:
- Logs details of the release process if the
--verboseflag is set.
- Logs details of the release process if the
Example usage
Add the release script in your package.json file so you can easly run from your terminal.
{
// ...
"scripts": {
// ...
"release": "node my-scripts/release.ts"
}
}Then in your release.ts file simply import the script and execute it.
.npmignore so it won't be published within you package.
// my-scripts/release.ts
import { release } from '@alessiofrittoli/node-scripts/release'
release({
verbose: true,
npm: true,
access: 'restricted',
})Add the release script in your package.json file so you can easly run from your terminal.
{
// ...
"scripts": {
// ...
"release": "node my-scripts/release.ts --verbose --npm --access restricted"
}
}Then in your release.ts file simply import the script and execute it.
.npmignore so it won't be published within you package.
// my-scripts/release.ts
import { release } from '@alessiofrittoli/node-scripts/release'
release()|
|
|