@taole/deploy-helper 0.2.0 → 0.2.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.
Files changed (2) hide show
  1. package/index.mjs +64 -23
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -11,36 +11,71 @@ import { homedir } from 'os'
11
11
  * @returns 配置对象
12
12
  */
13
13
  async function loadConfig() {
14
- const configPath = join(process.cwd(), "deploy.config.js");
15
- if (fs.existsSync(configPath)) {
16
- const config = await import(`file://${configPath}`);
17
- return config.default;
18
- }
19
14
  const configJsonPath = join(process.cwd(), "deploy.config.json");
20
15
  if (fs.existsSync(configJsonPath)) {
21
16
  const configJson = JSON.parse(fs.readFileSync(configJsonPath, "utf-8"));
22
17
  return configJson;
23
18
  }
24
- console.log(`deploy.config.js或deploy.config.json不存在, 请先创建`);
19
+ console.log(`deploy.config.json不存在, 请先创建`);
25
20
  return null;
26
21
  }
27
22
 
23
+ async function initConfigJson() {
24
+ // check if deploy.config.json exists
25
+ const configJsonPath = join(process.cwd(), "deploy.config.json");
26
+ if (fs.existsSync(configJsonPath)) {
27
+ console.log(`deploy.config.json已存在, 请勿重复创建`);
28
+ return;
29
+ }
30
+
31
+ const content = `
32
+ {
33
+ "assets": {
34
+ "dest": "../Static_2025",
35
+ "commit": "feat:部署资源文件",
36
+ "files": {
37
+ "dist/assets": "CHANGE_ME",
38
+ }
39
+ },
40
+ "entry": {
41
+ "onlyProd": true,
42
+ "dest": "../events",
43
+ "commit": "feat:部署入口文件",
44
+ "files": {
45
+ "dist/index.html": "CHANGE_ME"
46
+ }
47
+ },
48
+ "testSync": {
49
+ "dist/index.html": "events/CHANGE_ME.htm"
50
+ }
51
+ }`;
52
+ fs.writeFileSync(configJsonPath, content, { encoding: "utf-8" });
53
+ console.log(`deploy.config.json创建成功`);
54
+ process.exit(0);
55
+ }
56
+
57
+ function hasChangeMe(content) {
58
+ return content.includes("CHANGE_ME");
59
+ }
28
60
 
29
61
  async function main() {
30
62
 
31
63
  // other commands
64
+ let command = process.argv[2];
65
+ if (!["init", "prod", "test"].includes(command)) {
66
+ command = "help";
67
+ }
32
68
 
33
- if (process.argv[2] === "help") {
69
+ if (command === "help") {
34
70
  console.log(`deploy-helper 部署脚本`);
35
- console.log(`usage: deploy-helper [mode]`);
36
- console.log(`mode: prod 生产环境部署`);
37
- console.log(`mode: test 测试环境部署`);
71
+ console.log(`usage: deploy-helper [command]`);
72
+ console.log(`command: init 初始化配置`);
73
+ console.log(`command: prod 生产环境部署`);
74
+ console.log(`command: test 测试环境部署`);
75
+ process.exit(0);
76
+ } else if (command === "init") {
77
+ await initConfigJson();
38
78
  process.exit(0);
39
- }
40
-
41
- if (!["prod", "test"].includes(process.argv[2])) {
42
- console.log("不支持的命令");
43
- process.exit(1);
44
79
  }
45
80
  try {
46
81
  const workDir = process.cwd(); // 当前项目根目录
@@ -57,11 +92,18 @@ async function main() {
57
92
  return;
58
93
  }
59
94
 
95
+ const result = hasChangeMe(JSON.stringify(config));
96
+ if (result) {
97
+ console.log(`配置中存在默认值(CHANGE_ME), 请先修改配置`);
98
+ process.exit(1);
99
+ }
100
+
60
101
  // 需要处理entry
61
102
  const needHandleEntry = (mode === 'prod' && config.entry.onlyProd) || (mode === 'test' && !config.entry.onlyProd);
62
103
  const entryTestBranch = (config.entry && config.entry.testBranch) || "test";
63
104
  const entryProdBranch = (config.entry && config.entry.prodBranch) || "master";
64
105
  const entryTargetBranch = mode === 'prod' ? entryProdBranch : entryTestBranch;
106
+ const currentProdBranch = config.prodBranch || "master";
65
107
 
66
108
  // 检查项目
67
109
  // 1. 检查项目是否存在
@@ -96,7 +138,7 @@ async function main() {
96
138
  }
97
139
  }
98
140
 
99
- // 2.1如果是prod模式,检查当前项目是不是处于master分支
141
+ // 2.1如果是prod模式,检查当前项目是不是处于主分支
100
142
  if (mode === 'prod') {
101
143
  const currentGit = simpleGit(workDir);
102
144
  const currentStatus = await currentGit.status();
@@ -104,13 +146,14 @@ async function main() {
104
146
  console.log(`当前项目有未提交内容, 请先处理`);
105
147
  process.exit(1);
106
148
  }
107
- if (currentStatus.branch !== "master") {
108
- console.log(`当前项目不是master分支, 请切换至master分支`);
149
+ if (currentStatus.branch !== currentProdBranch) {
150
+ console.log(`当前项目不是${currentProdBranch}分支, 请切换至${currentProdBranch}分支再执行prod命令`);
109
151
  process.exit(1);
110
152
  }
111
153
  }
112
154
 
113
- // 3. 检查项目分支,并切换至master分支
155
+ // 3. 检查assets项目分支,并切换至master分支
156
+ // assets项目固定使用master分支
114
157
  if (assetsStatus.current !== "master") {
115
158
  console.log(`${assetsDest}当前分支不是master, 切换至master分支`);
116
159
  await assetsGit.checkout("master");
@@ -199,14 +242,12 @@ async function main() {
199
242
 
200
243
  // 7. 推送
201
244
  if (canPushAssets) {
202
- const assetsPushResult = await assetsGit.push();
245
+ await assetsGit.push();
203
246
  console.log(`assets push done.`);
204
- // console.log(`assets push: ${JSON.stringify([...assetsPushResult.remoteMessages.all()])}`);
205
247
  }
206
248
  if (canPushEntry) {
207
- const entryPushResult = await entryGit.push();
249
+ await entryGit.push();
208
250
  console.log(`entry push done.`);
209
- // console.log(`entry push: ${JSON.stringify([...entryPushResult.remoteMessages.all()])}`);
210
251
  }
211
252
 
212
253
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/deploy-helper",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "脚本部署工具,用于将项目部署到测试环境或生产环境",
5
5
  "main": "index.mjs",
6
6
  "type": "module",