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

lj-generator-cli

v1.0.9

Published

自定义脚手架

Downloads

14

Readme

自定义脚手架

安装npm lj-generator-cli -g

查看模板lj-generator-cli list

查看版本lj-generator-cli -v

安装模板lj-generator-cli create [projectName]

脚手架配置

//入口js文件
#! /usr/bin/env noe
//指定node运行该脚本

package.json文件

//bin属性 脚本名称为 "lj-cli" ,执行脚本文件 "./index.js"
 "bin": {
    "lj-cli":"./index.js"
  },
具体步骤
  1. 书写脚本文件
  2. 编辑package.json文件
  3. 软链接npm link 删除软链接npm rm lj-generator-cli -g 全局命令 lj-cli 会执行脚本
  4. 使用 commander插件 读取命令行参数 option可配置
import { program } from 'commander';
program
    .command("create <app-name>",)
    .description("创建一个新的项目")
    .option('-t --template [template]', '输入的模板名称')//-t 配置 模板
    .option('-f --force [force]', '强制覆盖本地同名目录')//-f 配置 模板
    .option('-i --ignore [ignore]', '忽略描述')//-i 配置 模板
    .action((name, option) => {
        logTitle();
        //name是create输入的名字  
        // option { template: 'asd', force: '1232', ignore: true }
    })
program.parse(process.argv);//解析用户输入的参数
  1. 读取package.json文件查看版本号
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
const __dirname = dirname(fileURLToPath(import.meta.url));//当前文件夹
const pkg = JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf8'));
program.version(pkg.version, "-v, -version");//参数查看版本号
  1. 已有文件删除文件夹
function deleteFolder(folderPath) {
    if (fs.existsSync(folderPath)) {
        fs.readdirSync(folderPath).forEach((file) => {
            const curPath = path.join(folderPath, file);
            if (fs.lstatSync(curPath).isDirectory()) {
                deleteFolder(curPath); // 递归调用删除子文件夹
            } else {
                fs.unlinkSync(curPath); // 直接删除文件
            }
        });
        fs.rmdirSync(folderPath); // 删除空文件夹
    }
}
await fs.rmdirSync(file);//删除文件夹 此操作只能删除空文件夹
  1. 交互命令行
import inquirer from 'inquirer';
export const inquirerConfirmHandle = async (message) => {
    return await inquirer
        .prompt({type: 'confirm', name: 'confirm',message, })
}
export const inquirerListHandle = async (message, choices) => {
    return await inquirer
        .prompt({ type: 'list', name: 'choices',  message,choices,  })
}   
export const inquirerPackageHandle = async (message) => {
    return await inquirer
        .prompt({ type: 'input',name: 'input',  message, })
}   
  1. 更改 package.json 文件信息
await changePackage(process.cwd() + '/' + name + '/package.json', pkgMessage, name);
const pkg = JSON.parse(fs.readFileSync(path, 'utf8'));  //读
await fs.writeFileSync(path, JSON.stringify(pkg, null, "\t"), { spaces: 2 }); //写 
  1. 安装依赖
await npmInstall(process.cwd() + '/' + name)
export const npmInstall = async (path) => {
const spinner = ora(chalk.blueBright(`正在安装依赖`)).start();
try {
    const res = shell.exec(`cd ${path} && npm install --force -d`);
    if (res?.code === 0) {
        spinner.succeed(chalk.blueBright((`~~~安装依赖成功!~~~`)));
        spinner.succeed(chalk.blueBright((`~~~项目安装成功!~~~`)));
    } else {
        spinner.fail(chalk.redBright((`安装依赖失败!请手动安装依赖!`)));
        shell.exec(1);
    }
} catch (error) {
    return false;
}
}
使用插件
  1. figlet(大标题)
  2. 进度条 ora
  3. chalk 字体美化
  4. shell 脚本控制
  5. table 命令行表格
  6. inquirer 命令行交互
  7. download-git-repo 下载包插件
  8. fs-extra 修改文件