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

Posted ThinkerWing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 并返回其输出结果的字符串表示形式。

以上是关于upload-tools发布一个 TypeScript 编写的 npm 包 (自动执行yarn build 然后上传至服务器指定目录)的主要内容,如果未能解决你的问题,请参考以下文章

Grpc快速实践

Grpc快速实践

XMLSerivce 的安装配置简介

为啥函数重载不提示错误?

vue3

Sequelize-typescript 'HasManyCreateAssociationMixin' 不是函数