npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

npm-command-call

v0.0.6

Published

use npm cli commands via function calls

Downloads

10

Readme

npm-command-call CircleCI npm version

以函数调用的方式使用npm cli的命令。

Table of Contents

Features

  • 以函数调用的方式执行npm cli command
  • 提供legacy和sso两种登录API
  • 在函数调用时指定登录凭证,避免不同用户之间互相影响

Install

npm i npm-command-call

Usage

Login

根据使用场景在下面三种方式中选择一种:

| 方式 | 使用场景 | | :------ | :------ | | 1、authentication token | 运行时使用一个账户,静态指定 | | 2、使用登录API和默认的登录态管理 | 运行时使用一个账户,动态指定 | | 3、使用登录API并自己管理登录态 | 运行时并发使用多个账户,动态指定 |

Call API

引入API并调用。

示例

const npmCmdCall = require('npm-command-call')

npmCmdCall.runCmd(['whoami']).catch((err) => {
  console.log(err)
})
import * as npmCmdCall from('npm-command-call')

npmCmdCall.runCmdWithCreds(['whoami'], {
  token: '3fd34677-e7a6-4e21-9d84-******'
}).catch((err) => {
  console.log(err)
})

说明

对于前两种登录方式,使用runCmd;对于最后一种登录方式,使用runCmdWithCreds

API

> adduserByLeagcy(userInfo, registry, scope, self) -> Promise<{token}>

使用密码进行登录

参数说明

userInfo指用户信息,属性包括username,password,email,必填;registrynpm-registry,选填;scopenpm-scope,选填;self指是否要自己处理登录态,默认为false,选填。

示例

adduserByLeagcy({
  username: 'u******',
  password: 'p******',
  email: 'e******'
}).catch((err) => {
  console.log(err)
})

> adduserBySso(registry, scope, cb, ssoType, self) -> Promise<{token}>

使用SSO进行登录

参数说明

registrynpm-registry,选填;scopenpm-scope,选填;cb指处理sso链接的函数,默认打开浏览器并访问该链接,选填;ssoType指处理sso的类型,取值包括’oauth‘和’saml‘,默认为’oauth‘,选填;self指是否要自己处理登录态,默认为false,选填。

示例

adduserBySso('https://registry.npmjs.org', undefined, (ssoUrl)) => {
  openUrl(ssoUrl)) // 以某种形式展示ssoUrl,使得用户可以访问
}, 'oauth', true).then(({ token }) => {
  save({ token }) // 以某种形式保存登录态
}).catch((err) => {
  console.log(err)
})

> runCmd(argvArr) -> Promise<string>

运行命令

参数说明

argvArr指命令行参数,必填。

示例

runCommand(['whoami', '--registry=https://registry.npmjs.org']).catch((err) => {
  console.log(err)
})

> runCmdWithCreds(argvArr, creds = {}, registry) -> Promise<string>

指定登录态运行命令

参数说明

argvArr指命令行参数,必填;creds指用户凭证,属性包括token,必填;registrynpm-registry,选填;

示例

runCmdWithCreds(["publish", ".", "--tag=beta"], {
  token: '3fd34677-e7a6-4e21-9d84-******'
}).catch((err) => {
  console.log(err)
})

> adduserBySsoUnsafe(registry, scope, ssoType) -> Promise<{ token: string, sso: string }>

使用SSO进行登录,返回token和sso链接,返回的token需要等点击sso链接进行登录后才会生效。

参数说明

registrynpm-registry,选填;scopenpm-scope,选填;ssoType指处理sso的类型,取值包括’oauth‘和’saml‘,默认为’oauth‘,选填;

示例

adduserBySsoUnsafe('https://registry.npmjs.org').then(({ token, sso }) => {
  openUrl(sso) // 在浏览器上打开链接进行登录
  save({ token }) // 以某种形式保存登录态
}).catch((err) => {
  console.log(err)
})

> adduserBySsoUnsafe(registry, scope, ssoType) -> Promise<{ token: string, sso: string }>

使用SSO进行登录,返回token和sso链接,返回的token需要等点击sso链接进行登录后才会生效。

参数说明

registrynpm-registry,选填;scopenpm-scope,选填;ssoType指处理sso的类型,取值包括’oauth‘和’saml‘,默认为’oauth‘,选填;

示例

adduserBySsoUnsafe('https://registry.npmjs.org').then(({ token, sso }) => {
  openUrl(sso) // 在浏览器上打开链接进行登录
  save({ token }) // 以某种形式保存登录态
}).catch((err) => {
  console.log(err)
})

> checkSsoToken(token, registry, scope) -> Promise<{ token: string, sso: string }>

检测token是否生效,结合adduserBySsoUnsafe使用。

参数说明

tokenadduserBySsoUnsafe返回的授权令牌,必填;registrynpm-registry,选填;scopenpm-scope,选填;

示例

adduserBySsoUnsafe('https://registry.npmjs.org')
.then(({ token, sso }) => {
  openUrl(sso) // 在浏览器上打开链接进行登录
  return checkSsoToken(token, 'https://registry.npmjs.org',)
}).catch((err) => {
  console.log(err)
})

License

MIT