当前位置:   article > 正文

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

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

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

npm init -y

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

  1. #!/usr/bin/env node
  2. import prompts from "prompts";
  3. import path from "node:path";
  4. import fs from "node:fs";
  5. const bootstrap = async () => {
  6. const result = await prompts([
  7. {
  8. type: "text",
  9. name: "projectName",
  10. message: "请输入项目名称:"
  11. },
  12. ]);
  13. const targetPath = path.resolve(process.cwd(), result.projectName);
  14. const sourcePath = path.resolve(__dirname, "../template");
  15. console.log(targetPath);
  16. fs.cpSync(sourcePath, targetPath,{
  17. recursive: true,
  18. });
  19. fs.renameSync(
  20. path.resolve(targetPath, "_gitignore"),
  21. path.resolve(targetPath, ".gitignore")
  22. );
  23. console.log(`
  24. 项目创建成功!!
  25. cd ${result.projectName}
  26. npm install
  27. npm run dev
  28. `)
  29. };
  30. bootstrap();

3.需要安装的依赖

npm i -D typescript tsup prompts @types/prompts

4.配置ts文件

        根目录下创建tsconfig.json

  1. {
  2. "include": ["src"],
  3. "compilerOptions": {
  4. "target": "ES2022",
  5. "module": "ES2022",
  6. "moduleResolution": "Bundler",
  7. "outDir": "dist",
  8. "skipLibCheck": true,
  9. "declaration": false,
  10. "strict": true,
  11. "rootDir": "src"
  12. }
  13. }

5.配置tsup

        根目录下创建tsup.config.ts

  1. import { defineConfig } from 'tsup'
  2. export default defineConfig({
  3. target:"node18",
  4. entry:["src/index.ts"],
  5. clean: true,
  6. format:["cjs"],
  7. minify: true,
  8. platform: "node",
  9. outDir: "dist",
  10. })

6.配置package.json文件

        名字起create-xxx

  1. {
  2. "name": "create-cocobin",
  3. "version": "1.0.0",
  4. "description": "",
  5. "bin": {
  6. "create-cocobin": "dist/index.js"
  7. },
  8. "file": [
  9. "dist",
  10. "template"
  11. ],
  12. "scripts": {
  13. "dev": "tsup --watch",
  14. "build": "tsup",
  15. "typecheck": "tsc --noEmit",
  16. "release": "release-it"
  17. },
  18. "keywords": [],
  19. "author": "",
  20. "license": "MIT",
  21. "devDependencies": {
  22. "@types/prompts": "^2.4.9",
  23. "prompts": "^2.4.2",
  24. "release-it": "^17.2.1",
  25. "tsup": "^8.0.2",
  26. "typescript": "^5.4.5"
  27. },
  28. "release-it": {
  29. "hooks": {
  30. "after:bump": "npm run build"
  31. }
  32. }
  33. }

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

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

        不要node_module文件夹

        不要pnpm-lock.yaml文件

8.下载发布工具

npm init release-it

9.添加.gitignore

  1. # Logs
  2. logs
  3. *.log
  4. npm-debug.log*
  5. yarn-debug.log*
  6. yarn-error.log*
  7. pnpm-debug.log*
  8. lerna-debug.log*
  9. node_modules
  10. dist
  11. dist-ssr
  12. *.local
  13. # Editor directories and files
  14. .vscode/*
  15. !.vscode/extensions.json
  16. .idea
  17. .DS_Store
  18. *.suo
  19. *.ntvs*
  20. *.njsproj
  21. *.sln
  22. *.sw?

10.发布到npm

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

提交到github

npm run release

        

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/523085?site
推荐阅读
相关标签
  

闽ICP备14008679号