从0到1使用TS实现一个node.js脚手架工具

1.新建一个项目文件夹,然后初始化一下项目文件

npm init -y

2.创建一个src文件夹,里面放index.ts

#!/usr/bin/env node

import prompts from "prompts";
import path from "node:path";
import fs from "node:fs";
const bootstrap = async () => {
    const result =  await prompts([
        {
            type: "text",
            name: "projectName",
            message: "请输入项目名称:"
        },
    ]);
    const targetPath = path.resolve(process.cwd(), result.projectName);
    const sourcePath = path.resolve(__dirname, "../template");
    console.log(targetPath);
    fs.cpSync(sourcePath, targetPath,{
        recursive: true,
    });
    fs.renameSync(
        path.resolve(targetPath, "_gitignore"),
        path.resolve(targetPath, ".gitignore")
    );
    console.log(`
    项目创建成功!!
    cd ${result.projectName}
    npm install
    npm run dev
    `)
};
bootstrap();

3.需要安装的依赖

npm i -D typescript tsup prompts @types/prompts

4.配置ts文件

        根目录下创建tsconfig.json

{
    "include": ["src"],
    "compilerOptions": {
        "target": "ES2022",
        "module": "ES2022",
        "moduleResolution": "Bundler",
        "outDir": "dist",
        "skipLibCheck": true,
        "declaration": false,
        "strict": true,
        "rootDir": "src"
    }
}

5.配置tsup

        根目录下创建tsup.config.ts

import { defineConfig } from 'tsup'

export default defineConfig({
    target:"node18",
    entry:["src/index.ts"],
    clean: true,
    format:["cjs"],
    minify: true,
    platform: "node",
    outDir: "dist",
})

6.配置package.json文件

        名字起create-xxx

{
  "name": "create-cocobin",
  "version": "1.0.0",
  "description": "",
  "bin": {
    "create-cocobin": "dist/index.js"
  },
  "file": [
    "dist",
    "template"
  ],
  "scripts": {
    "dev": "tsup --watch",
    "build": "tsup",
    "typecheck": "tsc --noEmit",
    "release": "release-it"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "@types/prompts": "^2.4.9",
    "prompts": "^2.4.2",
    "release-it": "^17.2.1",
    "tsup": "^8.0.2",
    "typescript": "^5.4.5"
  },
  "release-it": {
    "hooks": {
      "after:bump": "npm run build"
    }
  }
}

7.创建template文件夹,里面放入要传入的项目

        里面把.gitignore文件改成_gitignore        -->        避免影响文件

        不要node_module文件夹

        不要pnpm-lock.yaml文件

8.下载发布工具

npm init release-it

9.添加.gitignore

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

10.发布到npm

npm run dev
git init
npm config set registry https://registry.npmjs.org/
npm login 

提交到github

npm run release

        

相关推荐

  1. 01使用TS实现一个node.js脚手架工具

    2024-05-02 07:34:03       10 阅读
  2. 01实现一个ui组件库

    2024-05-02 07:34:03       9 阅读
  3. 01实现YOLOv3

    2024-05-02 07:34:03       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-02 07:34:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-02 07:34:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-02 07:34:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-02 07:34:03       18 阅读

热门阅读

  1. 微信小程序标题设置

    2024-05-02 07:34:03       13 阅读
  2. MongoDB聚合运算符:$substr

    2024-05-02 07:34:03       11 阅读
  3. Stylus介绍

    2024-05-02 07:34:03       12 阅读
  4. Android学习系列目录

    2024-05-02 07:34:03       17 阅读
  5. OpenCV 开源的计算机视觉和机器学习软件库

    2024-05-02 07:34:03       13 阅读
  6. 【at89s52单片机的冒泡排序使用指针】2022-4-30

    2024-05-02 07:34:03       14 阅读
  7. 机器学习项目部署:从模型到生产环境

    2024-05-02 07:34:03       12 阅读
  8. 【设计模式】之单例模式

    2024-05-02 07:34:03       12 阅读