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

jm_koa_cil

v1.0.2

Published

#### Description 开发一个koa-cli node工具,学习使用node开发

Downloads

11

Readme

jm_koa_cil

介绍

开发一个 koa-cli node 工具,学习使用 node 开发

目录介绍

  • question 前置安装配置问题
  • template 项目模版
  • utils 项目工具
  • config 所有的配置项
  • create-mkdir 目录生成
  • index 执行文件

node 包介绍

inquirer 交互命令行工具

inquirer.prompt(questions) -> promise 启动提示界面并返回 promise

传入的是一个配置化的数组,数组里面每一个都是一个对象

const questions = [
  {
    type: "string",
    name: "name",
    message: "请输入你的名字",
  },
];

inquirer.prompt(questions);

// type: 'input' 表示可以输入

// type: 'checkbox' 表示多选,用于去安装项目依赖

chalk 一个命令行的样式工具

https://segmentfault.com/a/1190000012666718

可以在命令行打印出好看的色彩颜色

console.log(chalk.blue("...正式安装之前的准备工作 开始 ..."));

execa 子进程管理工具

execa 是更好的子进程管理工具(A better child_process)。本质上就是衍生一个 shell,传入的 command 字符串在该 shell 中直接处理。

https://github.com/sindresorhus/execa

const execa = require("execa");
execa("cd ../");

package.json

  1. type 设置 module 用于可以使用 使用 esm 规范 去导入文件, 后面必须携带.js 后缀名 import packageName from './package-name.js'

扩展知识

#!/usr/bin/env node https://blog.csdn.net/liangtaox8/article/details/100039274

配置#!/usr/bin/env node, 就是解决了不同的用户 node 路径不同的问题,可以让系统动态的去查找 node 来执行你的脚本文件。

编码过程

1. 命令行输入,得到用户自定义配置

// question
import inquirer from "inquirer";
import packageName from "./package-name.js";
import middleware from "./middleware.js";

export default () => {
  return inquirer.prompt([packageName(), middleware()]);
};
// index.js
const answer = await question();

// 将命令窗口交互答案放置config 中
const config = createConfig(answer);

2. 根据项目名生成 项目目录

  1. 目录下项目名存在,创建 dist 目录下的工作台目录,否则当前目录创建工作台目录
// index.js

const rootFolder = path.resolve(process.cwd(), packageName);

const exitFlag = fsExistsSync(rootFolder);

mkdir(exitFlag, packageName, config, rootFolder);
// createTemplate.js
import { mkdirSync, writeFileSync } from "fs";
import { resolve } from "path";
import { createPath } from "./utils/commonUtils.js";
import {
  createIndexTemplate,
  createPackageTemplate,
} from "./utils/createTemplate.js";

export default (exitFlag, packageName, config, rootFolder) => {
  // 目录不存在
  if (!exitFlag) {
    commonMkdir(rootFolder, config);
  } else {
    const dist = resolve(rootFolder, "dist");
    mkdirSync(dist);
    commonMkdir(resolve(dist, packageName), config);
  }
};

function commonMkdir(rootFolder, config) {
  // 创建目录
  mkdirSync(rootFolder); // 创建目录

  // 创建入口文件
  writeFileSync(
    createPath(rootFolder, "index.js"), // 创建index.js
    createIndexTemplate(config) // 模版下写入数据
  );

  // 创建 package.json
  writeFileSync(
    createPath(rootFolder, "package.json"),
    createPackageTemplate(config)
  );
}

3. 安装依赖

// 安装依赖
// 下面代码 使用yarn安装依赖 win 下执行有问题
// 判断 如果是win系统的话 改成 npm i 来安装依赖 就没有问题了
async function installDep() {
  if (isWinOS()) {
    // 是win系统
    log(chalk.blue("安装依赖 -> npm i"));
    // npm install
    await execa("npm i", {
      cwd: exitFlag ? rootDistPath : rootFolder,
      stdio: [2, 2, 2],
    });
  } else {
    // 是其他系统
    log(chalk.blue("安装依赖 -> yarn"));
    // yarn install
    await execa("yarn", {
      cwd: exitFlag ? rootDistPath : rootFolder,
      stdio: [2, 2, 2],
    });
  }
}
installDep();

api 总结

path.resolve(); // 返回一个由当前文件所处根盘目录下到到当前文件下到绝对路径,
// 参数 resolve(...pathSegments: string[]): string;

process.cwd(); // 返回路径

fs.accessSync(path, fs.F_OK); // 判断文件或文件夹是否存在

process.platform; // 返回当前操作系统 分别为 win32 darwin linux

fs.mkdirSync(); // 同步创建文件夹,
// 参数:export function mkdirSync(path: PathLike, options?: Mode | (MakeDirectoryOptions & { recursive?: false | undefined; }) | null): void;

fs.writeFileSync(); // 同步创建文件并写入
// 参数:export function writeFileSync(path: PathOrFileDescriptor, data: string | NodeJS.ArrayBufferView, options?: WriteFileOptions): void;