【upload-tools】发布一个 TypeScript 编写的 npm 包 (自动执行yarn build 然后上传至服务器指定目录)

学习内容:

提示:将多个项目中常用的发布脚本,发布 npm 包

如何发布一个 TypeScript 编写的 npm 包 https://www.cnblogs.com/chuckQu/p/16939993.html


介绍

做一个自动上传服务器的脚本工具 🔧

仓库地址

npm地址

Config 服务器的配置(账号/密码/端口/存放地址)

CommandsType 命令数组或者字符串(['yarn lint:prettier', 'yarn build'] | 'yarn build')

type Config = {
  host: string;
  username: string;
  password: string;
  port: number;
  remotePath: string;
};
type CommandsType = string[] | string;

示例代码

const uploadTools = require('upload-tools');

const config = {
    host: '127.0.0.1', # ip 地址
    username: 'root', # 用户名
    password: '', # 服务器密码
    port: '22',
    remotePath: '/www/wwwroot/xxlb.site'  # 目标地址
  };

const commands = ['yarn lint:prettier', 'yarn build'];

uploadTools({ commands, config });

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tRJ6pbTC-1678696687581)(public/1.png)]

实现效果

thinkerwing@ThinkerdeMacBook-Pro bin % node test.js
start: yarn lint:prettier...
yarn run v1.22.19
$ prettier -c --write "src/**/*" --end-of-line auto
Checking formatting...
All matched files use Prettier code style!
Done in 3.42s.

start: yarn build...
Browserslist: caniuse-lite is outdated. Please run:

...

Done in 7.34s.

/Users/thinkerwing/Desktop/demo/dist
连接服务器成功
上传完成,当前时间: 2023-3-13 16:16:47

使用指南🧭

npm i upload-tools -D

按照示例代码填写配置项

通过 node test.js 或者 在 packeage.json 中配置

 "scripts": {
    "release": "node ./bin/release.js ",
  }

通过 yarn release实现

过程记录

  1. 最重要的设置

"files": ["src/index.ts"]。库的主文件会位于src文件夹下,因此需要这么设置
"target": "es2015" 确保我们的库支持现代平台,并且不会携带不必要的垫片。
"module": "es2015"。我们的模块将是一个标准的ES模块(默认是CommonJS)。ES模式在现代浏览器下没有任何问题;甚至Node从13版本开始就支持ES模式。但是我们这个工具使用的是"type": "commonjs",
"declaration": true- 因为我们想要自动生成d.ts声明文件。我们的TypeScript用户将需要这些声明文件。
其他大部分选项只是各种可选的TypeScript检查,我更喜欢开启这些检查。

  1. npm publish报错: 426 Upgrade Required
    https://juejin.cn/post/7020221794825011208
    还有要重复名字的问题,先去搜索一下。

  2. 代码解析

child_process.execSync(c) 是一个同步函数,用于在子进程中执行 shell 命令 c。
toString("utf8") 则是将输出结果转换成字符串编码格式 utf8。
因此,child_process.execSync(c).toString("utf8") 的作用是执行 shell 命令 c 并返回其输出结果的字符串表示形式。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值