A guide to using the NuGet packaged named NPMWrap (an API Wrapper for NPM for .NET)
Introduction
This could help with projects wanting to do things with the NPM Registry API/CLI, providing a set of easy to use and setup tools.
Using the Code
The first step to using NPMWrap
is actually installing it, you can either use the CLI or (if you're using Visual Studio), Manage NuGet packages and install it from there. You can see the NuGet package (and the available commands to install it here).
Depending on how you want to use it, you can search the NPM Registry API for packages, it will automatically fetch the API and do things with the data to make it easier to handle.
Using the Registry API
If you want to search through the available packages, use the NPMWrap.Registry.SearchPackages
, and to get a package version, use NPMWrap.Registry.GetPackageVersion
.
Registry.SearchPackages
This returns a SearchResult
object, and an example of its usage is:
NPMWrap.Registry.Config Configuration = new NPMWrap.Registry.Config()
{
UserAgent = "Your user agent"
};
SearchResult Result = await NPMWrap.Registry.SearchPackages("Express", Configuration);
Registry.GetPackageVersion
This returns a RegistryPackageVersion
object, and an example of its usage (similar to Registry.SearchPackages
) is:
NPMWrap.Registry.Config Configuration = new NPMWrap.Registry.Config()
{
UserAgent = "Your user agent"
}
RegistryPackageVersion PackageVersion =
await NPMWrap.Registry.GetPackageVersion("Express", Configuration);
And, if you want to get a specific version and not just the latest, put a little version code in between the first string and the Configuration.
And that's basically the Registry part.
Commands
Before you do anything with commands, there's a little configuration that needs to be setup, an example of this is:
NPMWrap.Commands.Config Configuration = new NPMWrap.Commands.Config()
{
UseYarn = false,
Directory = "C:/Path/To/Working/Directory",
IsDebug = false,
WaitForExit = true
};
Commands.RunBaseInstall
This will basically just either do npm install
or yarn
based on the given configuration, for example:
await RunBaseInstall(Configuration);
Commands.RunInstall
The installation command for adding a package, for example:
await RunBaseInstall("Express", Configuration);
This also can be used to install specific versions of the package by just inserting the version code as a string after the first string.
Commands.RunUninstall
The uninstallation command removes a package, for example:
await RunBaseUninstall("React", Configuration);
History
- 8th November, 2023: Initial version