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 🙏

© 2026 – Pkg Stats / Ryan Hefner

lw-prompts

v1.0.8

Published

轻量、简单可扩展的prompts

Downloads

2

Readme

lw-prompts

🎯 一个轻量级、简单可扩展的命令行交互提示库,专为现代 TypeScript/JavaScript 项目设计。

✨ 特性

  • 🪶 轻量级: 零依赖,体积小巧
  • 🎨 简单易用: 直观的 API 设计,开箱即用
  • 🔧 高度可扩展: 插件化架构,支持自定义提示类型
  • 📦 TypeScript 优先: 完整的类型支持和智能提示
  • 🌈 丰富的提示类型: 支持文本输入、选择器等多种交互方式
  • ⚡️ 现代化: 基于 ES Module,支持最新的 JavaScript 特性

📦 安装

npm install lw-prompts

# 或使用 yarn
yarn add lw-prompts

# 或使用 pnpm
pnpm add lw-prompts

🚀 快速开始

链式调用

import { prompt } from "lw-prompts";

const answers = await prompt([
  {
    type: "text",
    name: "name",
    message: "项目名称:",
    validate: (input) => input.length > 0 || "项目名称不能为空",
  },
  {
    type: "select",
    name: "template",
    message: "选择模板:",
    options: [
      { label: "React + TypeScript", value: "react-ts" },
      { label: "Vue + TypeScript", value: "vue-ts" },
    ],
  },
]);

console.log("项目配置:", answers);

🎯 使用场景

CLI 工具开发

#!/usr/bin/env node
import { prompt } from "lw-prompts";

async function createProject() {
  const config = await prompt([
    {
      type: "text",
      name: "name",
      message: "项目名称:",
    },
    {
      type: "select",
      name: "framework",
      message: "选择框架:",
      options: [
        { label: "React", value: "react" },
        { label: "Vue", value: "vue" },
      ],
    },
  ]);

  console.log("创建项目:", config);
}

createProject();

🏗️ 项目结构

lw-prompts/
├── src/
│   ├── index.ts           # 主入口文件
│   ├── prompt.ts          # 核心提示逻辑
│   ├── textPrompt.ts      # 文本输入提示
│   └── selectPrompt.ts    # 选择器提示
├── dist/                  # 编译输出
├── package.json
├── tsconfig.json
└── README.md

🛠️ 开发

本地开发

# 克隆项目
git clone <repository-url> // 未提交到代码库,可以全局安装再将项目拷贝出来
cd lw-prompts

# 安装依赖
npm install

# 开发模式
npx tsc -w // 将ts文件实时转化成js文件

🔧 扩展开发

自定义提示类型

// 创建自定义 password 提示
export interface PasswordPromptOptions {
  message: string;
  mask?: string;
}

export async function passwordPrompt(options: PasswordPromptOptions) {
  // 实现密码输入逻辑
  // ...
}

🎨 最佳实践

1. 输入验证

const email = await textPrompt({
  message: "邮箱地址:",
  validate: (input) => {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(input) || "请输入有效的邮箱地址";
  },
});

2. 条件性提示

const useTypeScript = await selectPrompt({
  message: "是否使用 TypeScript?",
  options: [
    { label: "是", value: true },
    { label: "否", value: false },
  ],
});

const questions = [
  {
    type: "text",
    name: "name",
    message: "项目名称:",
  },
];

if (useTypeScript) {
  questions.push({
    type: "select",
    name: "tsconfig",
    message: "选择 TypeScript 配置:",
    options: [
      { label: "严格模式", value: "strict" },
      { label: "宽松模式", value: "loose" },
    ],
  });
}

const answers = await prompt(questions);

3. 错误处理

try {
  const result = await textPrompt({
    message: "输入内容:",
    validate: (input) => {
      if (!input.trim()) {
        throw new Error("输入不能为空");
      }
      return true;
    },
  });
} catch (error) {
  console.error("输入错误:", error.message);
  process.exit(1);
}

📊 与其他库对比

| 特性 | lw-prompts | inquirer | prompts | enquirer | | ---------- | ---------- | --------- | ------- | -------- | | 包大小 | 🟢 小 | 🔴 大 | 🟡 中 | 🟡 中 | | TypeScript | ✅ 原生 | ⚠️ 社区 | ✅ 支持 | ✅ 支持 | | ES Module | ✅ 支持 | ❌ 不支持 | ✅ 支持 | ✅ 支持 | | 可扩展性 | 🟢 高 | 🟡 中 | 🟡 中 | 🟢 高 | | 学习成本 | 🟢 低 | 🟡 中 | 🟢 低 | 🟡 中 |

🔍 故障排除

常见问题

1. TypeScript 类型错误

# 确保安装了类型定义
npm install --save-dev @types/node

2. ES Module 导入错误

// package.json
{
  "type": "module"
}

3. 终端兼容性问题

// 检查终端是否支持交互
if (!process.stdin.isTTY) {
  console.error("此程序需要在交互式终端中运行");
  process.exit(1);
}

📈 性能特点

  • ⚡️ 启动速度: < 10ms
  • 💾 内存占用: < 5MB
  • 📦 包体积: < 50KB (压缩后)
  • 🔄 响应延迟: < 1ms

📄 许可证

ISC

👨‍💻 作者

785184273

🔗 相关