@dypnb/dev-tools 1.0.8 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dypnb/dev-tools",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "开发时的提效工具",
5
5
  "main": "lib/bundle.cjs.js",
6
6
  "jsnext:main": "lib/bundle.esm.js",
@@ -20,7 +20,7 @@
20
20
  "publishD": "dyp-publish"
21
21
  },
22
22
  "bin": {
23
- "dyp-publish": "src/publish-server/index.js"
23
+ "dyp-publish": "src/publish-server/index.mjs"
24
24
  },
25
25
  "dependencies": {
26
26
  "@dypnb/dev-tools": "^1.0.5",
@@ -0,0 +1,130 @@
1
+ #! /usr/bin/env node
2
+
3
+ // scp2 : https://www.npmjs.com/package/
4
+ // 引入scp2
5
+ import client from "scp2";
6
+ import ora from "ora";
7
+ import moment from "moment";
8
+ import { getGlobalConfig, getGitInfo, errorLog, magentaLog, log, warningLog, successLog, cyanLog } from "../utils/index.js";
9
+ import { wxNotify } from "../wx-server-notice/index.js";
10
+ import pkg from 'ssh2';
11
+ const { Client } = pkg;
12
+
13
+ moment.locale('zh-cn');
14
+
15
+ async function getUploadServeConfig() {
16
+ return await getGlobalConfig();
17
+ }
18
+
19
+ getUploadServeConfig().then(res => {
20
+ const uploadServeConfig = res['uploadServeConfig'];
21
+ const wxServerConfig = res['wxServerConfig'];
22
+
23
+ const isProduction = process.env.VUE_APP_PACK_ENV === "pro"; // 是否是生产环境
24
+
25
+ const lineVersion = process.env.VUE_APP_LINE_VERSION; //线上环境版本(多个显示环境可使用此字段区分)
26
+
27
+ const isNewPro = isProduction && +lineVersion === 2; // 是否是新的线上版本
28
+
29
+ const host = !isNewPro ? uploadServeConfig.host[0] : uploadServeConfig.host[1];
30
+
31
+ const staticPath = !isProduction ? uploadServeConfig.staticPath.dev : uploadServeConfig.staticPath.pro;
32
+
33
+ const onlinePath = `${uploadServeConfig.protocol}://${host}/${staticPath}`
34
+
35
+ const gitInfo = getGitInfo();
36
+
37
+ const server = {
38
+ host, // 服务器ip
39
+ port: "22", // 端口一般默认22
40
+ locaPath: `${process.env.PWD}${uploadServeConfig.locaPath}`, // 本地打包文件的位置
41
+ ...uploadServeConfig.serverOption,
42
+ pathNmae: `${uploadServeConfig.serverOption.pathNmae}${staticPath}` // 上传到服务器的位置
43
+ };
44
+
45
+ const packInfo = {
46
+ ...(uploadServeConfig.baseInfo || {}),
47
+ serverPath: server.pathNmae
48
+ }
49
+
50
+
51
+ function wxMsg(status) {
52
+ return `<div class="gray">${moment().format('lll')}</div><div class="highlight">${uploadServeConfig.projectTitle}${isProduction ? "生产" : "开发"}环境发布${status}</div><div class="normal">commitId:${gitInfo.commitId}</div><div class="normal">commitMsg:${gitInfo.commitMsg}</div><div class="normal">访问地址:${onlinePath}</div>`
53
+ }
54
+
55
+
56
+ cyanLog("本次打包信息 =>", packInfo);
57
+
58
+
59
+
60
+ const spinner = ora(
61
+ "正在发布到" + (isProduction ? "生产" : "开发") + "服务器..."
62
+ );
63
+
64
+ // 创建shell脚本
65
+ const conn = new Client();
66
+
67
+ magentaLog("正在建立连接");
68
+
69
+ conn
70
+ .on("ready", function() {
71
+ log("已连接");
72
+ if (!server.pathNmae) {
73
+ log("连接已关闭");
74
+ conn.end();
75
+ return false;
76
+ }
77
+ // 这里我拼接了放置服务器资源目录的位置 ,首选通过rm -rf删除了这个目录下的文件
78
+ conn.exec("rm -rf " + server.pathNmae + "/*", function(err, stream) {
79
+ warningLog("已删除服务端文件")
80
+ stream.on("close", function(code, signal) {
81
+ log("开始上传");
82
+ spinner.start();
83
+ client.scp(
84
+ server.locaPath,
85
+ {
86
+ host: server.host,
87
+ port: server.port,
88
+ username: server.username,
89
+ password: server.password,
90
+ path: server.pathNmae
91
+ },
92
+ err => {
93
+ spinner.stop();
94
+ if (!err) {
95
+ successLog("Success! 成功发布到" + (isProduction ? "生产" : "开发") + "服务器!", `访问地址====>${onlinePath}`)
96
+ wxNotify({
97
+ msgtype: "textcard",
98
+ textcard: {
99
+ title: "发布成功",
100
+ description: wxMsg('成功'),
101
+ url: onlinePath,
102
+ btntxt: "更多"
103
+ }
104
+ }, wxServerConfig);
105
+ } else {
106
+ errorLog("发布失败.\n", err);
107
+ wxNotify({
108
+ msgtype: "textcard",
109
+ textcard: {
110
+ title: "发布失败",
111
+ description: wxMsg('失败'),
112
+ url: onlinePath,
113
+ btntxt: "更多"
114
+ }
115
+ }, wxServerConfig);
116
+ }
117
+ conn.end(); // 结束命令
118
+ }
119
+ );
120
+ });
121
+ });
122
+ })
123
+ .connect({
124
+ host: server.host,
125
+ port: server.port,
126
+ username: server.username,
127
+ password: server.password
128
+ //privateKey: '' //使用 私钥密钥登录 目前测试服务器不需要用到
129
+ });
130
+ })
@@ -3,67 +3,65 @@ import chalk from "chalk";
3
3
  import execa from "execa";
4
4
 
5
5
  // 最新 node 核心包的导入写法
6
- import { fileURLToPath } from 'node:url'
7
- import { dirname } from 'node:path'
6
+ import { fileURLToPath } from "node:url";
7
+ import { dirname } from "node:path";
8
+
8
9
 
9
10
  export async function getGlobalConfig(type) {
10
- const name = findup('dyp.config.js', {cwd: process.env.PWD});
11
+ const name = findup("dyp.config.js", { cwd: process.env.PWD });
11
12
  const dypConfig = await import(name);
12
13
  if (type) {
13
- return dypConfig['default'][type];
14
+ return dypConfig["default"][type];
14
15
  }
15
- return dypConfig['default'];
16
+ return dypConfig["default"];
16
17
  }
17
18
 
18
19
  // 首字母大写
19
- export function strCase(str) {
20
- return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
20
+ export function strCase(str) {
21
+ return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
21
22
  }
22
23
 
23
24
  // 获取文件名
24
- export function getFilename() {
25
+ export function getFilename() {
25
26
  return fileURLToPath(import.meta.url);
26
27
  }
27
28
 
28
29
  // 获取根路径
29
- export function getDirname() {
30
- return dirname(fileURLToPath(import.meta.url))
30
+ export function getDirname() {
31
+ return dirname(fileURLToPath(import.meta.url));
31
32
  }
32
33
 
33
-
34
- export function log(mainMsg, laterMsg) {
35
- return chalkLog('blue', mainMsg, laterMsg);
34
+ export function log(mainMsg, laterMsg) {
35
+ return chalkLog("blue", mainMsg, laterMsg);
36
36
  }
37
37
 
38
- export function cyanLog(mainMsg, laterMsg = '') {
39
- return chalkLog('cyan', mainMsg, laterMsg);
38
+ export function cyanLog(mainMsg, laterMsg = "") {
39
+ return chalkLog("cyan", mainMsg, laterMsg);
40
40
  }
41
41
 
42
- export function magentaLog(mainMsg, laterMsg = '') {
43
- return chalkLog('magenta', mainMsg, laterMsg);
42
+ export function magentaLog(mainMsg, laterMsg = "") {
43
+ return chalkLog("magenta", mainMsg, laterMsg);
44
44
  }
45
45
 
46
- export function successLog(mainMsg, laterMsg = '') {
47
- return chalkLog('green', mainMsg, laterMsg);
46
+ export function successLog(mainMsg, laterMsg = "") {
47
+ return chalkLog("green", mainMsg, laterMsg);
48
48
  }
49
49
 
50
- export function warningLog(mainMsg, laterMsg = '') {
51
- return chalkLog('yellow', mainMsg, laterMsg);
50
+ export function warningLog(mainMsg, laterMsg = "") {
51
+ return chalkLog("yellow", mainMsg, laterMsg);
52
52
  }
53
53
 
54
- export function errorLog(mainMsg, laterMsg = '') {
55
- return chalkLog('red', mainMsg, laterMsg);
54
+ export function errorLog(mainMsg, laterMsg = "") {
55
+ return chalkLog("red", mainMsg, laterMsg);
56
56
  }
57
57
 
58
- export function chalkLog(chalkType, mainMsg, laterMsg = '') {
58
+ export function chalkLog(chalkType, mainMsg, laterMsg = "") {
59
59
  if (!laterMsg) {
60
60
  return console.log(chalk[chalkType](`${mainMsg}`));
61
61
  }
62
62
  return console.log(chalk[chalkType](`${mainMsg}`), laterMsg);
63
-
64
63
  }
65
64
 
66
-
67
65
  /**
68
66
  * 获取最新 commit 提交信息
69
67
  * git 获取提交信息参考:https://www.cnblogs.com/ruiy/p/15904295.html
@@ -71,15 +69,14 @@ export function chalkLog(chalkType, mainMsg, laterMsg = '') {
71
69
  */
72
70
 
73
71
  export function getGitInfo() {
74
- const shortCommid = execa.commandSync('git rev-parse --short HEAD');
72
+ const shortCommid = execa.commandSync("git rev-parse --short HEAD");
75
73
 
76
- const gitComMsg = `git log --pretty=format:“%s” ${shortCommid.stdout} -1`
74
+ const gitComMsg = `git log --pretty=format:“%s” ${shortCommid.stdout} -1`;
77
75
 
78
76
  const { stdout, stderr } = execa.commandSync(gitComMsg);
79
77
 
80
78
  return {
81
79
  commitId: shortCommid.stdout,
82
- commitMsg: stdout
83
- }
80
+ commitMsg: stdout,
81
+ };
84
82
  }
85
-