65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 .NET 的 NPM API 包装器

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.71/5 (4投票s)

2023 年 11 月 8 日

CPOL

1分钟阅读

viewsIcon

4745

如何使用名为 NPMWrap 的 NuGet 包

引言

这可以帮助希望使用 NPM Registry API/CLI 的项目,提供一套易于使用和设置的工具。

Using the Code

使用 NPMWrap 的第一步实际上是安装它,你可以使用 CLI 或(如果你使用的是 Visual Studio),管理 NuGet 包并从那里安装它。你可以在 这里 找到 NuGet 包(以及可用的安装命令)。

根据你想要如何使用它,你可以搜索 NPM Registry API 以查找包,它会自动获取 API 并处理数据,使其更易于处理。

使用 Registry API

如果你想搜索可用的包,请使用 NPMWrap.Registry.SearchPackages,要获取包版本,请使用 NPMWrap.Registry.GetPackageVersion

Registry.SearchPackages

这将返回一个 SearchResult 对象,其用法示例是

NPMWrap.Registry.Config Configuration = new NPMWrap.Registry.Config()
{
   UserAgent = "Your user agent" // Example: NPMWrap
};

SearchResult Result = await NPMWrap.Registry.SearchPackages("Express", Configuration);

Registry.GetPackageVersion

这将返回一个 RegistryPackageVersion 对象,其用法示例(类似于 Registry.SearchPackages)是

NPMWrap.Registry.Config Configuration = new NPMWrap.Registry.Config()
{
   UserAgent = "Your user agent" // Example: NPMWrap
}

RegistryPackageVersion PackageVersion = 
    await NPMWrap.Registry.GetPackageVersion("Express", Configuration);

而且,如果你想要获取特定版本而不是最新版本,请在第一个字符串和 Configuration 之间插入一些版本代码。

基本上就是 Registry 部分了。

Commands

在执行任何命令之前,需要进行一些配置,例如

NPMWrap.Commands.Config Configuration = new NPMWrap.Commands.Config()
{
   UseYarn = false, // Depending on if you want to use Yarn or not.
   Directory = "C:/Path/To/Working/Directory", // If you leave this blank,
                                               // it will throw an exception.
   IsDebug = false, // If set to true and a command is run the CMD window will be visible.
   WaitForExit = true // If you wait for it to exit, 
                      // it may clog up the thread until NPM or Yarn has finished.
};

Commands.RunBaseInstall

这将基本上根据给定的配置执行 npm installyarn,例如

await RunBaseInstall(Configuration);

Commands.RunInstall

用于添加包的安装命令,例如

await RunBaseInstall("Express", Configuration);

这也可以通过在第一个字符串之后插入版本代码作为字符串来安装特定版本的包。

Commands.RunUninstall

卸载命令用于删除包,例如

await RunBaseUninstall("React", Configuration);

历史

  • 2023 年 11 月 8 日:初始版本
© . All rights reserved.