yj-deploy 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -0,0 +1,218 @@
1
+ # yj-deploy
2
+
3
+ > `yj-deploy` 是为解决公司内部项目部署到镜像服务器开发的工具,可解决每次开发完需通过繁琐的步骤才能获得镜像地址的问题。
4
+
5
+ <br />
6
+ <br />
7
+
8
+ ## 目前已实现如下功能
9
+ - &#x2714; 项目打包完成后自动上传到跳板机
10
+ - &#x2714; 自动生成目录,自动创建dockerfile,upload.sh脚本文件
11
+ - &#x2714; 自动运行脚本,自动打包镜像,自动推送到镜像服务器(全程不用手动操作)
12
+ - &#x2714; 直接集成到vite,webpack项目
13
+ - &#x2714; 支持手动上传其他任何类型项目
14
+ - &#x2714; 支持灵活配置dockerfile,upload.sh等脚本内容
15
+ - &#x2714; 支持多种方式配置 `镜像仓库` `镜像环境`等参数
16
+
17
+ ![预览](./img.png)
18
+
19
+ ## 安装
20
+ ![NPM](https://nodei.co/npm/yj-deploy.png)
21
+ ```sh
22
+ $ yarn add yj-deploy
23
+ $ npm i yj-deploy -D
24
+ ```
25
+
26
+
27
+ ## 简单配置
28
+
29
+ ```javascript
30
+ import path from 'path'
31
+ import yjDeploy from './lib/main.js'
32
+
33
+ const deploy = new yjDeploy({
34
+ host: '', // ip
35
+ port: '', // 端口
36
+ username: '', // 账号
37
+ password: '', // 密码
38
+ namespace: '', // 项目命名空间,等同于镜像地址目录名称
39
+ // 本地文件目录
40
+ fileDir: path.resolve('./dist'),
41
+ })
42
+ ```
43
+
44
+ ## 完整配置(包含默认配置)
45
+ ```json
46
+ {
47
+ host: '', // ip (必填)
48
+ port: '', // 端口 (必填)
49
+ username: '', // 账号 (必填)
50
+ password: '', // 密码 (必填)
51
+ namespace: '', // 项目命名空间,等同于镜像地址目录名称 (必填)
52
+ imageStore: 'dev-images', // 镜像仓库, 默认为 dev-images
53
+ tmpName: 'dev', // 镜像环境, 默认 dev
54
+ delay: 0, // 延迟上传时间 (部分项目构建需要时间,延迟上传可以解决)
55
+ fileDir: '', // 本地文件目录(必填)
56
+ rootDir: '/home/yjweb', // 远程sftp服务根目录 默认为 /home/yjweb
57
+ // dockerfile文件信息
58
+ dockerfile: {
59
+ name: 'Dockerfile',
60
+ content: [
61
+ 'FROM harbor.yunjingtech.cn:30002/yj-base/nginx:latest',
62
+ 'RUN rm -rf /usr/share/nginx/html/\*',
63
+ 'COPY dist /usr/share/nginx/html/'
64
+ ]
65
+ },
66
+
67
+ // upload.sh文件信息(可覆盖,单需要注意 $1 和 $2参数的顺序)
68
+ upload: {
69
+ name: 'upload.sh',
70
+ content: [
71
+ '#!/bin/sh',
72
+ 'tag=`basename \\`pwd\\``:$2-`date +%Y%m%d`',
73
+ 'echo "- 镜像仓库: $1"',
74
+ 'echo "- 镜像环境: $2"',
75
+ 'docker build -t harbor.yunjingtech.cn:30002/$1/$tag .',
76
+ 'docker push harbor.yunjingtech.cn:30002/$1/$tag',
77
+ 'echo - 镜像地址: harbor.yunjingtech.cn:30002/$1/$tag',
78
+ ]
79
+ },
80
+
81
+ // 上传dist目录信息 (除非运维改变,否则不要配置)
82
+ dist: {
83
+ name: 'dist'
84
+ }
85
+ }
86
+ ```
87
+
88
+ # 使用
89
+ ## 配合打包命令使用
90
+ ```javascript
91
+ // webpack 中使用
92
+ //vue.config.js
93
+ const path = require('path')
94
+ const yjDeploy = require('yj-deploy')
95
+ module.exports = {
96
+ configureWebpack: config => {
97
+ return {
98
+ plugins: [
99
+ new yjDeploy({
100
+ host: '',
101
+ port: '',
102
+ username: '',
103
+ password: '',
104
+ namespace: '', // 项目命名空间,等同于镜像地址目录名称
105
+ // 本地文件目录
106
+ fileDir: path.resolve('./dist'),
107
+ })
108
+ ]
109
+ }
110
+ }
111
+ }
112
+
113
+ // package.json
114
+ "scripts": {
115
+ "deploy": "vue-cli-service build --mode development --deploy"
116
+ }
117
+ // 使用 yarn deploy 或 npm run deploy
118
+ ```
119
+
120
+ ```javascript
121
+ // vite 项目中使用
122
+ //vite.config.js
123
+ import path from 'path'
124
+ import yjDeploy from 'yj-deploy'
125
+
126
+ export default defineConfig({
127
+ plugins: [
128
+ new yjDeploy({
129
+ host: '',
130
+ port: '',
131
+ username: '',
132
+ password: '',
133
+ namespace: '', // 项目命名空间,等同于镜像地址目录名称
134
+ // 本地文件目录
135
+ fileDir: path.resolve('./dist'),
136
+ })
137
+ ]
138
+ })
139
+
140
+ // package.json
141
+ "scripts": {
142
+ "deploy": "vue-cli-service build --mode development -- --deploy"
143
+ }
144
+ // 使用 yarn deploy 或 npm run deploy
145
+ ```
146
+
147
+ ## 动态配置项
148
+ 由于部分项目可能需要区分环境,所以需要动态配置项,目前支持动态配置项如下:
149
+ <br />
150
+
151
+ `--deploy` :是否启用自动部署功能,如果启用,在命令后跟上 --deploy即可
152
+ ```javascript
153
+ // vite项目 package.json
154
+ "scripts": {
155
+ "deploy": "vue-cli-service build --mode development -- --deploy"
156
+ }
157
+
158
+ // webpack项目 package.json
159
+ "scripts": {
160
+ "deploy": "vue-cli-service build --mode development --deploy"
161
+ }
162
+ ```
163
+ ::: warning
164
+ 由于vite的自定义命令需要在vite专属配置后加 -- 分割,所以vite项目的命令中间会多一个 -- 分割,这就是vite和webpack执行命令的差异,后续不再提及
165
+ :::
166
+
167
+ <br />
168
+
169
+ `--reset` :是否重置项目,如果配置了此选项,则每次执行命令时会删除`dockerfile``upload.sh`配置文件,然后重新初始化项目,默认为不重置
170
+
171
+ <br />
172
+
173
+ `--imageStore` :镜像仓库名称(默认为dev-images),在测试或者正式环境可能需要配置,需要推送到指定的镜像仓库时配置,增加此动态配置可以使用更灵活,同一套主配置可以满足多个环境推送到不同的镜像仓库,如:
174
+ ```javascript
175
+ "scripts": {
176
+ // 开发环境
177
+ "deploy": "vue-cli-service build --mode development -- --deploy"
178
+ // 测试环境
179
+ "deploy-test": "vue-cli-service build --mode development -- --deploy --imageStore=sot-admin"
180
+ }
181
+ ```
182
+
183
+ <br />
184
+
185
+ `--tmpName` :镜像环境名称(默认为 dev),在测试或者正式环境可能需要配置,需要推送到指定的镜像环境时配置,防止镜像名重复导致覆盖,同一套主配置可以满足多个环境推送到不同的镜像地址,如:
186
+ ```javascript
187
+ "scripts": {
188
+ // 开发环境
189
+ "deploy": "vue-cli-service build --mode development -- --deploy --tmpName=dev"
190
+ // 测试环境
191
+ "deploy-test": "vue-cli-service build --mode development -- --deploy --tmpName=test"
192
+ // 测试环境
193
+ "deploy-prod": "vue-cli-service build --mode development -- --deploy --tmpName=prod"
194
+ }
195
+ ```
196
+
197
+
198
+ ## 上传任意项目
199
+ ```javascript
200
+ // 1、在项目中创建uploader.js
201
+ // 2、配置和webpack插件模式相同
202
+ const deploy = new yjDeploy({
203
+ host: '',
204
+ port: '',
205
+ username: '',
206
+ password: '',
207
+ namespace: '', // 项目命名空间,等同于镜像地址目录名称
208
+ // 本地文件目录
209
+ fileDir: path.resolve('./dist'),
210
+ })
211
+
212
+ deploy.upload() // 开始上传
213
+ // 然后在项目根目录终端下运行如下命令(node直接上传不需要加--deploy动态指令)
214
+ node uploader.js
215
+
216
+ // 增加动态配置
217
+ node uploader.js --tmpName=test
218
+ ```
@@ -0,0 +1,269 @@
1
+ var S = Object.defineProperty;
2
+ var C = (h, o, e) => o in h ? S(h, o, { enumerable: !0, configurable: !0, writable: !0, value: e }) : h[o] = e;
3
+ var g = (h, o, e) => (C(h, typeof o != "symbol" ? o + "" : o, e), e);
4
+ const { stdout: j } = require("single-line-log"), D = require("path"), m = require("fs"), { Client: v } = require("ssh2");
5
+ class F {
6
+ constructor(o) {
7
+ g(this, "config", {
8
+ host: "",
9
+ port: "",
10
+ username: "",
11
+ password: "",
12
+ namespace: "",
13
+ // 项目命名空间,等同于镜像地址目录名称
14
+ imageStore: "dev-images",
15
+ // 镜像仓库
16
+ tmpName: "dev",
17
+ // 镜像环境
18
+ delay: 0,
19
+ // 延迟上传时间
20
+ // 本地文件目录(必填)
21
+ fileDir: "",
22
+ // 远程sftp服务根目录
23
+ rootDir: "/home/yjweb",
24
+ // dockerfile文件信息
25
+ dockerfile: {
26
+ name: "Dockerfile",
27
+ content: [
28
+ "FROM harbor.yunjingtech.cn:30002/yj-base/nginx:latest",
29
+ "RUN rm -rf /usr/share/nginx/html/*",
30
+ "COPY dist /usr/share/nginx/html/"
31
+ ]
32
+ },
33
+ // upload.sh文件信息
34
+ upload: {
35
+ name: "upload.sh",
36
+ content: [
37
+ "#!/bin/sh",
38
+ "tag=`basename \\`pwd\\``:$2-`date +%Y%m%d`",
39
+ 'echo "- 镜像仓库: $1"',
40
+ 'echo "- 镜像环境: $2"',
41
+ "docker build -t harbor.yunjingtech.cn:30002/$1/$tag .",
42
+ "docker push harbor.yunjingtech.cn:30002/$1/$tag",
43
+ "echo - 镜像地址: harbor.yunjingtech.cn:30002/$1/$tag"
44
+ ]
45
+ },
46
+ // 上传dist目录信息
47
+ dist: {
48
+ name: "dist"
49
+ }
50
+ });
51
+ g(this, "ssh2Conn", null);
52
+ // 上传状态
53
+ g(this, "uploading", !1);
54
+ // 定时器
55
+ g(this, "trim", null);
56
+ return this.config = Object.assign(this.config, o), {
57
+ name: "yj-deploy",
58
+ isPut: this.isPut.bind(this),
59
+ upload: this.upload.bind(this),
60
+ // webpack钩子
61
+ apply: this.apply.bind(this),
62
+ // vite上传钩子
63
+ closeBundle: this.isPut.bind(this)
64
+ };
65
+ }
66
+ // webpack钩子
67
+ apply(o) {
68
+ return o && o.hooks && o.hooks.done && o.hooks.done.tap("yj-deploy", () => {
69
+ this.isPut();
70
+ }), "build";
71
+ }
72
+ /**
73
+ * 判断是否需要上传,
74
+ * vite和 webpack环境在每次编译完都会触发当前方法
75
+ * 需要通过是否有命令行参数 --deploy
76
+ */
77
+ isPut() {
78
+ this.getArgv("--deploy") != null && (clearTimeout(this.trim), this.trim = setTimeout(() => {
79
+ this.upload();
80
+ }, this.config.delay || 0));
81
+ }
82
+ /**
83
+ * 连接跳板机并开始上传
84
+ */
85
+ upload() {
86
+ if (console.log("-------------- deploy-start --------------"), !this.config.host) {
87
+ console.log("- 请配置跳板机地址 host");
88
+ return;
89
+ }
90
+ if (!this.config.port) {
91
+ console.log("- 请配置跳板机端口 port");
92
+ return;
93
+ }
94
+ if (!this.config.username) {
95
+ console.log("- 请配置跳板机账号 username");
96
+ return;
97
+ }
98
+ if (!this.config.password) {
99
+ console.log("- 请配置跳板机密码 password");
100
+ return;
101
+ }
102
+ this.uploading || (this.ssh2Conn = new v(), this.ssh2Conn.on("ready", () => {
103
+ console.log("- 跳板机连接成功!"), this.ssh2Conn.sftp(async (o, e) => {
104
+ o && (console.log("- sftp连接失败"), this.breakConnect()), this.config.namespace || (console.log("- 请配置项目命名空间 namespace"), this.breakConnect()), this.uploading = !0, this.getArgv("--reset") !== void 0 && (console.log("- 重置项目"), await this.onShell(`rm -r ${this.config.rootDir}/${this.config.namespace}`)), this.upLoadProject(e, `${this.config.rootDir}/${this.config.namespace}`);
105
+ });
106
+ }).connect({
107
+ host: this.config.host,
108
+ port: this.config.port,
109
+ username: this.config.username,
110
+ password: this.config.password
111
+ }), this.ssh2Conn.on("error", (o) => {
112
+ console.log(`- 连接失败: ${o}`), this.breakConnect();
113
+ }), this.ssh2Conn.on("end", () => {
114
+ this.uploading = !1;
115
+ }));
116
+ }
117
+ breakConnect() {
118
+ this.uploading = !1, console.log("- 已断开连接"), console.log("-------------- deploy-end --------------"), this.ssh2Conn.end();
119
+ }
120
+ /**
121
+ * 运行shell命令
122
+ * @param {*} shell
123
+ * @returns Promise
124
+ */
125
+ onShell(o) {
126
+ return new Promise((e, r) => {
127
+ this.ssh2Conn.shell((i, s) => {
128
+ if (i) {
129
+ this.breakConnect();
130
+ return;
131
+ }
132
+ let l = [];
133
+ s.on("close", () => {
134
+ e(l.toString());
135
+ }).on("data", (t) => {
136
+ l.push(`
137
+ ` + t);
138
+ }).stderr.on("data", (t) => {
139
+ console.log(`- 远程命令错误:
140
+ ` + t), this.ssh2Conn.end(), r(t);
141
+ }), s.end(o + `
142
+ exit
143
+ `);
144
+ });
145
+ });
146
+ }
147
+ /**
148
+ * 初始化项目
149
+ * @param {*} sftp
150
+ * @param {*} dir
151
+ */
152
+ projectInit(o, e) {
153
+ return new Promise(async (r, i) => {
154
+ o.mkdir(e, async (s) => {
155
+ if (s)
156
+ console.log(s), i();
157
+ else {
158
+ console.log("- 正在创建dockerfile");
159
+ const l = `${e}/${this.config.dockerfile.name}`;
160
+ await this.onShell(`touch ${l}`), await this.onShell(`echo '${this.config.dockerfile.content.join(`
161
+ `)}' > ${l}`), console.log("- 正在创建upload.sh");
162
+ const t = `${e}/${this.config.upload.name}`;
163
+ await this.onShell(`touch ${t}`), await this.onShell(`echo '${this.config.upload.content.join(`
164
+ `)}' > ${t}`), r();
165
+ }
166
+ });
167
+ });
168
+ }
169
+ /**
170
+ * 上传项目
171
+ * @param {*} sftp
172
+ * @param {*} dir
173
+ */
174
+ upLoadProject(o, e) {
175
+ o.readdir(e, async (r) => {
176
+ if (r) {
177
+ console.log("- 跳板机不存在项目, 开始创建项目"), this.projectInit(o, e).then(() => {
178
+ console.log(`- ${this.config.namespace}项目创建成功`), this.upLoadProject(o, e);
179
+ }).catch(() => {
180
+ console.log("- 创建项目失败"), this.breakConnect();
181
+ });
182
+ return;
183
+ }
184
+ console.log("- 开始上传文件");
185
+ const i = `${e}/${this.config.dist.name}`;
186
+ await this.onShell(`rm -rf ${i}`), await this.onShell(`mkdir ${i}`), this.config.fileDir || (console.log("- 请配置本地文件目录 fileDir"), this.breakConnect());
187
+ const s = await this.getFilesInDirectory(this.config.fileDir);
188
+ s.length === 0 && (console.log("- 待上传目录为空,请检查"), this.breakConnect());
189
+ let l = 0;
190
+ const t = (n, k = 0) => n.isDirectory ? this.onShell(`mkdir ${i}${n.remotePath}`) : new Promise((P, y) => {
191
+ try {
192
+ const f = m.createReadStream(n.localPath), p = o.createWriteStream(`${e}/dist${n.remotePath}`);
193
+ p.on("close", () => {
194
+ this.progressBar(++l, k), P();
195
+ }), p.on("error", (w) => {
196
+ console.log(`- 文件 ${n.remotePath} 上传失败:${w}`), y(w), this.breakConnect();
197
+ }), f.pipe(p);
198
+ } catch (f) {
199
+ y(f);
200
+ }
201
+ }), d = s.filter((n) => n.isDirectory);
202
+ await Promise.all(d.map((n) => t(n))), await new Promise((n) => {
203
+ setTimeout(() => {
204
+ n();
205
+ }, 500);
206
+ });
207
+ const a = s.filter((n) => !n.isDirectory);
208
+ await Promise.all(a.map((n) => t(n, a.length))), console.log("- 文件上传成功"), console.log("- 开始推送镜像");
209
+ let c = `cd ${e} && sh ${this.config.upload.name}`;
210
+ const $ = this.getArgv("--imageStore") || this.config.imageStore;
211
+ c += ` ${$}`;
212
+ const u = this.getArgv("--tmpName") || this.config.tmpName;
213
+ c += ` ${u}`;
214
+ const b = await this.onShell(c);
215
+ console.log(b), console.log("- 镜像推送完成"), this.ssh2Conn.end();
216
+ });
217
+ }
218
+ /**
219
+ * 进度条
220
+ * @param description 命令行开头的文字信息
221
+ * @param bar_length 进度条的长度(单位:字符),默认设为 25
222
+ */
223
+ progressBar(o, e, r = 25) {
224
+ let i = (o / e).toFixed(4), s = Math.floor(i * r), l = "";
225
+ for (let a = 0; a < s; a++)
226
+ l += "█";
227
+ let t = "";
228
+ for (let a = 0; a < r - s; a++)
229
+ t += "░";
230
+ let d = `- 上传进度: ${l}${t} ${(100 * i).toFixed(2)}% (${o}/${e})`;
231
+ j(d), o == e && console.log("");
232
+ }
233
+ /**
234
+ * 获取目录下所有文件
235
+ * @param {*} dir
236
+ */
237
+ getFilesInDirectory(o) {
238
+ const e = [];
239
+ return new Promise((r, i) => {
240
+ const s = (l, t) => {
241
+ m.readdirSync(l).forEach((a) => {
242
+ const c = D.join(l, a), u = m.statSync(c).isDirectory();
243
+ e.push({
244
+ isDirectory: u,
245
+ localPath: c,
246
+ // 本地路径
247
+ remotePath: `${t}${a}`
248
+ // 远程路径
249
+ }), u && s(c, `${t}${a}/`);
250
+ });
251
+ };
252
+ s(o, "/"), r(e);
253
+ });
254
+ }
255
+ /**
256
+ * 获取命令行参数
257
+ * @param { string } name
258
+ */
259
+ getArgv(o) {
260
+ const e = process.argv;
261
+ let r;
262
+ return e.forEach((i) => {
263
+ i.indexOf(o) > -1 && (r = i.split("=")[1] || "");
264
+ }), r;
265
+ }
266
+ }
267
+ export {
268
+ F as default
269
+ };
@@ -0,0 +1,7 @@
1
+ (function(h,r){typeof exports=="object"&&typeof module<"u"?module.exports=r():typeof define=="function"&&define.amd?define(r):(h=typeof globalThis<"u"?globalThis:h||self,h["yj-deploy"]=r())})(this,function(){"use strict";var D=Object.defineProperty;var v=(h,r,g)=>r in h?D(h,r,{enumerable:!0,configurable:!0,writable:!0,value:g}):h[r]=g;var u=(h,r,g)=>(v(h,typeof r!="symbol"?r+"":r,g),g);const{stdout:h}=require("single-line-log"),r=require("path"),g=require("fs"),{Client:k}=require("ssh2");class P{constructor(e){u(this,"config",{host:"",port:"",username:"",password:"",namespace:"",imageStore:"dev-images",tmpName:"dev",delay:0,fileDir:"",rootDir:"/home/yjweb",dockerfile:{name:"Dockerfile",content:["FROM harbor.yunjingtech.cn:30002/yj-base/nginx:latest","RUN rm -rf /usr/share/nginx/html/*","COPY dist /usr/share/nginx/html/"]},upload:{name:"upload.sh",content:["#!/bin/sh","tag=`basename \\`pwd\\``:$2-`date +%Y%m%d`",'echo "- 镜像仓库: $1"','echo "- 镜像环境: $2"',"docker build -t harbor.yunjingtech.cn:30002/$1/$tag .","docker push harbor.yunjingtech.cn:30002/$1/$tag","echo - 镜像地址: harbor.yunjingtech.cn:30002/$1/$tag"]},dist:{name:"dist"}});u(this,"ssh2Conn",null);u(this,"uploading",!1);u(this,"trim",null);return this.config=Object.assign(this.config,e),{name:"yj-deploy",isPut:this.isPut.bind(this),upload:this.upload.bind(this),apply:this.apply.bind(this),closeBundle:this.isPut.bind(this)}}apply(e){return e&&e.hooks&&e.hooks.done&&e.hooks.done.tap("yj-deploy",()=>{this.isPut()}),"build"}isPut(){this.getArgv("--deploy")!=null&&(clearTimeout(this.trim),this.trim=setTimeout(()=>{this.upload()},this.config.delay||0))}upload(){if(console.log("-------------- deploy-start --------------"),!this.config.host){console.log("- 请配置跳板机地址 host");return}if(!this.config.port){console.log("- 请配置跳板机端口 port");return}if(!this.config.username){console.log("- 请配置跳板机账号 username");return}if(!this.config.password){console.log("- 请配置跳板机密码 password");return}this.uploading||(this.ssh2Conn=new k,this.ssh2Conn.on("ready",()=>{console.log("- 跳板机连接成功!"),this.ssh2Conn.sftp(async(e,o)=>{e&&(console.log("- sftp连接失败"),this.breakConnect()),this.config.namespace||(console.log("- 请配置项目命名空间 namespace"),this.breakConnect()),this.uploading=!0,this.getArgv("--reset")!==void 0&&(console.log("- 重置项目"),await this.onShell(`rm -r ${this.config.rootDir}/${this.config.namespace}`)),this.upLoadProject(o,`${this.config.rootDir}/${this.config.namespace}`)})}).connect({host:this.config.host,port:this.config.port,username:this.config.username,password:this.config.password}),this.ssh2Conn.on("error",e=>{console.log(`- 连接失败: ${e}`),this.breakConnect()}),this.ssh2Conn.on("end",()=>{this.uploading=!1}))}breakConnect(){this.uploading=!1,console.log("- 已断开连接"),console.log("-------------- deploy-end --------------"),this.ssh2Conn.end()}onShell(e){return new Promise((o,l)=>{this.ssh2Conn.shell((i,n)=>{if(i){this.breakConnect();return}let c=[];n.on("close",()=>{o(c.toString())}).on("data",t=>{c.push(`
2
+ `+t)}).stderr.on("data",t=>{console.log(`- 远程命令错误:
3
+ `+t),this.ssh2Conn.end(),l(t)}),n.end(e+`
4
+ exit
5
+ `)})})}projectInit(e,o){return new Promise(async(l,i)=>{e.mkdir(o,async n=>{if(n)console.log(n),i();else{console.log("- 正在创建dockerfile");const c=`${o}/${this.config.dockerfile.name}`;await this.onShell(`touch ${c}`),await this.onShell(`echo '${this.config.dockerfile.content.join(`
6
+ `)}' > ${c}`),console.log("- 正在创建upload.sh");const t=`${o}/${this.config.upload.name}`;await this.onShell(`touch ${t}`),await this.onShell(`echo '${this.config.upload.content.join(`
7
+ `)}' > ${t}`),l()}})})}upLoadProject(e,o){e.readdir(o,async l=>{if(l){console.log("- 跳板机不存在项目, 开始创建项目"),this.projectInit(e,o).then(()=>{console.log(`- ${this.config.namespace}项目创建成功`),this.upLoadProject(e,o)}).catch(()=>{console.log("- 创建项目失败"),this.breakConnect()});return}console.log("- 开始上传文件");const i=`${o}/${this.config.dist.name}`;await this.onShell(`rm -rf ${i}`),await this.onShell(`mkdir ${i}`),this.config.fileDir||(console.log("- 请配置本地文件目录 fileDir"),this.breakConnect());const n=await this.getFilesInDirectory(this.config.fileDir);n.length===0&&(console.log("- 待上传目录为空,请检查"),this.breakConnect());let c=0;const t=(s,C=0)=>s.isDirectory?this.onShell(`mkdir ${i}${s.remotePath}`):new Promise((j,w)=>{try{const m=g.createReadStream(s.localPath),$=e.createWriteStream(`${o}/dist${s.remotePath}`);$.on("close",()=>{this.progressBar(++c,C),j()}),$.on("error",b=>{console.log(`- 文件 ${s.remotePath} 上传失败:${b}`),w(b),this.breakConnect()}),m.pipe($)}catch(m){w(m)}}),f=n.filter(s=>s.isDirectory);await Promise.all(f.map(s=>t(s))),await new Promise(s=>{setTimeout(()=>{s()},500)});const a=n.filter(s=>!s.isDirectory);await Promise.all(a.map(s=>t(s,a.length))),console.log("- 文件上传成功"),console.log("- 开始推送镜像");let d=`cd ${o} && sh ${this.config.upload.name}`;const y=this.getArgv("--imageStore")||this.config.imageStore;d+=` ${y}`;const p=this.getArgv("--tmpName")||this.config.tmpName;d+=` ${p}`;const S=await this.onShell(d);console.log(S),console.log("- 镜像推送完成"),this.ssh2Conn.end()})}progressBar(e,o,l=25){let i=(e/o).toFixed(4),n=Math.floor(i*l),c="";for(let a=0;a<n;a++)c+="█";let t="";for(let a=0;a<l-n;a++)t+="░";let f=`- 上传进度: ${c}${t} ${(100*i).toFixed(2)}% (${e}/${o})`;h(f),e==o&&console.log("")}getFilesInDirectory(e){const o=[];return new Promise((l,i)=>{const n=(c,t)=>{g.readdirSync(c).forEach(a=>{const d=r.join(c,a),p=g.statSync(d).isDirectory();o.push({isDirectory:p,localPath:d,remotePath:`${t}${a}`}),p&&n(d,`${t}${a}/`)})};n(e,"/"),l(o)})}getArgv(e){const o=process.argv;let l;return o.forEach(i=>{i.indexOf(e)>-1&&(l=i.split("=")[1]||"")}),l}}return P});
package/package.json CHANGED
@@ -1,29 +1,30 @@
1
1
  {
2
2
  "name": "yj-deploy",
3
- "version": "0.0.1",
4
- "description": "ssh",
3
+ "version": "0.0.2",
4
+ "description": "ssh sftp",
5
5
  "scripts": {
6
6
  "build": "vite build"
7
7
  },
8
- "type": "module",
9
8
  "files": [
10
9
  "dist",
11
10
  "package.json",
12
11
  "README.md"
13
12
  ],
14
- "main": "./dist/yj-deploy.umd.cjs",
15
- "module": "./dist/yj-deploy.js",
13
+ "main": "./dist/yj-deploy.umd.js",
14
+ "module": "./dist/yj-deploy.mjs",
16
15
  "exports": {
17
16
  ".": {
18
- "import": "./dist/yj-deploy.js",
19
- "require": "./dist/yj-deploy.umd.cjs"
17
+ "import": "./dist/yj-deploy.mjs",
18
+ "require": "./dist/yj-deploy.umd.js"
20
19
  }
21
20
  },
22
21
  "author": "",
23
22
  "license": "ISC",
24
- "devDependencies": {
23
+ "dependencies": {
25
24
  "single-line-log": "^1.1.2",
26
- "ssh2": "^1.15.0",
25
+ "ssh2": "^1.15.0"
26
+ },
27
+ "devDependencies": {
27
28
  "vite": "4.5"
28
29
  },
29
30
  "publishConfig": {