@taole/deploy-helper 0.2.7 → 0.2.9

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.
Files changed (2) hide show
  1. package/index.mjs +66 -14
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { Client } from 'node-scp'
4
4
  import fs from 'fs';
5
- import { join } from "path";
5
+ import { join, basename, dirname } from "path";
6
6
  import { simpleGit } from 'simple-git';
7
7
  import { homedir } from 'os'
8
8
 
@@ -58,11 +58,31 @@ function hasChangeMe(content) {
58
58
  return content.includes("CHANGE_ME");
59
59
  }
60
60
 
61
- async function main() {
61
+ async function getScpClient() {
62
+ let scpClient = null;
63
+ // 不能放密码。。残念
64
+ const scpClientConfig = {
65
+ host: '192.168.0.35',
66
+ username: 'root',
67
+ tryKeyboard: true,
68
+ }
69
+ const sshKeyPath = join(homedir(), ".ssh/id_rsa");
70
+ if (fs.existsSync(sshKeyPath)) {
71
+ scpClientConfig.privateKey = fs.readFileSync(sshKeyPath, "utf-8");
72
+ } else {
73
+ console.log(`${sshKeyPath}不存在, 建议配置本机ssh免密登录, 参考地址:https://foochane.cn/article/2019061601.html`);
74
+ }
75
+ scpClient = await Client(scpClientConfig);
76
+ return scpClient;
77
+ }
78
+
62
79
 
80
+ async function main() {
81
+ // console.log(`process.argv: ${process.argv[2]} ${process.argv[3]} ${process.argv[4]}`);
82
+ // return;
63
83
  // other commands
64
84
  let command = process.argv[2];
65
- if (!["init", "prod", "test"].includes(command)) {
85
+ if (!["init", "prod", "test", "scp", "scpevt"].includes(command)) {
66
86
  command = "help";
67
87
  }
68
88
 
@@ -72,10 +92,52 @@ async function main() {
72
92
  console.log(`command: init 初始化配置`);
73
93
  console.log(`command: prod 生产环境部署`);
74
94
  console.log(`command: test 测试环境部署`);
95
+ console.log(`command: scp {file} 复制文件{file}到OfficialSite测试服务器{file}`);
96
+ console.log(`command: scp {file} {dest} 复制文件{file}到OfficialSite测试服务器{dest}`);
97
+ console.log(`command: scpevt {file} 复制文件{file}到events测试服务器{file}`);
98
+ console.log(`command: scpevt {file} {dest} 复制文件{file}到events测试服务器{dest}`);
75
99
  process.exit(0);
76
100
  } else if (command === "init") {
77
101
  await initConfigJson();
78
102
  process.exit(0);
103
+ } else if (["scp", "scpevt"].includes(command)) {
104
+ const file = process.argv[3];
105
+ if (!file) {
106
+ console.log(`file参数不能为空`);
107
+ process.exit(1);
108
+ }
109
+ const workDir = process.cwd(); // 当前项目根目录
110
+
111
+ const srcFilePath = join(workDir, file);
112
+ if (!fs.existsSync(srcFilePath)) {
113
+ console.log(`${srcFilePath}不存在`);
114
+ process.exit(1);
115
+ }
116
+ if (fs.statSync(srcFilePath).isDirectory()) {
117
+ console.log(`${srcFilePath}是目录, 不支持scp目录`);
118
+ process.exit(1);
119
+ }
120
+ // 获取srcFilePath的目录
121
+ const fileDir = dirname(srcFilePath);
122
+ const fileName = basename(srcFilePath);
123
+ const dest = process.argv[4] || fileName;
124
+ if(dest.includes("/") || dest.includes("\\")){
125
+ console.log(`dest不能包含/或\\`);
126
+ process.exit(1);
127
+ }
128
+
129
+ if(workDir !== fileDir){
130
+ console.log(`仅支持scp当前目录下的文件(不带子目录)`);
131
+ process.exit(1);
132
+ }
133
+
134
+ const destPath = "/home/web/website/tuwan_www/templets/static/play/" + (command === 'scp' ? '' : 'events/') + dest;
135
+
136
+ const scpClient = await getScpClient();
137
+ console.log(`scp: ${srcFilePath} -> ${destPath}`);
138
+ await scpClient.uploadFile(srcFilePath, destPath);
139
+ console.log(`scp done.`);
140
+ process.exit(0);
79
141
  }
80
142
  try {
81
143
  const workDir = process.cwd(); // 当前项目根目录
@@ -255,17 +317,7 @@ async function main() {
255
317
  // 8 测试环境将entry scp至测试环境
256
318
  if (mode === 'test' && config.testSync) {
257
319
  const syncTestFiles = config.testSync;
258
- // 不能放密码。。残念
259
- const scpClientConfig = {
260
- host: '192.168.0.35',
261
- username: 'root',
262
- tryKeyboard: true,
263
- }
264
- const sshKeyPath = join(homedir(), ".ssh/id_rsa");
265
- if (fs.existsSync(sshKeyPath)) {
266
- scpClientConfig.privateKey = fs.readFileSync(sshKeyPath, "utf-8");
267
- }
268
- const scpClient = await Client(scpClientConfig);
320
+ const scpClient = await getScpClient();
269
321
  for (const [src, dest] of Object.entries(syncTestFiles)) {
270
322
  const srcPath = join(workDir, src);
271
323
  const destPath = "/home/web/website/tuwan_www/templets/static/play/" + dest;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/deploy-helper",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "脚本部署工具,用于将项目部署到测试环境或生产环境",
5
5
  "main": "index.mjs",
6
6
  "type": "module",