hbuilder-build-move 0.0.1

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 (3) hide show
  1. package/index.mjs +40 -0
  2. package/move.js +100 -0
  3. package/package.json +17 -0
package/index.mjs ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env node
2
+
3
+ import path from "path";
4
+ import fs from "fs";
5
+ import { fileURLToPath } from "url";
6
+ import move from "./move.js";
7
+ // import move from "./cp.mjs";
8
+
9
+ import { Command } from "commander";
10
+ const program = new Command();
11
+
12
+ // get package json
13
+ const __filename = fileURLToPath(import.meta.url);
14
+ const __dirname = path.dirname(__filename);
15
+ const filePath = path.resolve(__dirname, "package.json");
16
+ const data = fs.readFileSync(filePath, "utf8");
17
+ const packageJson = JSON.parse(data);
18
+
19
+ program
20
+ .name(packageJson.name)
21
+ .description(packageJson.description)
22
+ .version(packageJson.version);
23
+
24
+ program
25
+ .description(
26
+ "hbuilderX 打包后的h5资源,移动到指定的项目目录下,并且通过git上传."
27
+ )
28
+ .argument("[string]", "git commit detail", "test")
29
+ .option("-t, --target <string>", "target directory", "build/h5")
30
+ .action((name, options) => {
31
+ console.log("git commit 填写信息 :>> ", name);
32
+ // console.log("options.target :>> ", options.target);
33
+
34
+ move("./unpackage/dist/build/web", options.target, "build: " + name);
35
+ // move("./unpackage/dist/build/web", options.target)
36
+ // .then(() => console.log("复制成功"))
37
+ // .catch(console.error);
38
+ });
39
+
40
+ program.parse();
package/move.js ADDED
@@ -0,0 +1,100 @@
1
+ const path = require("path");
2
+ const fs = require("fs");
3
+ const fsPromise = require("fs/promises");
4
+ const childProcess = require("child_process");
5
+
6
+ const { spawn, spawnSync } = childProcess;
7
+
8
+ async function main(src, dest, commitInfo) {
9
+ const getProjectRoot = (startPath = __dirname) => {
10
+ let currentDir = path.resolve(startPath);
11
+ while (true) {
12
+ // 检查是否存在 package.json .git
13
+ const hasPkg = fs.existsSync(path.join(currentDir, "package.json"));
14
+ const hasGit = fs.existsSync(path.join(currentDir, ".git"));
15
+ if (hasPkg || hasGit) return currentDir;
16
+
17
+ // 到达根目录时终止
18
+ const parentDir = path.dirname(currentDir);
19
+ if (parentDir === currentDir) break;
20
+ currentDir = parentDir;
21
+ }
22
+ throw new Error("未找到项目根目录");
23
+ };
24
+
25
+ // console.log("从脚本目录查找:", getProjectRoot(__dirname));
26
+ // console.log("从工作目录查找:", getProjectRoot(process.cwd()));
27
+ const projectRoot = getProjectRoot(process.cwd());
28
+ // 检查git目录是否存在
29
+ const gitDir = path.join(projectRoot, ".git");
30
+ // console.log("gitDir :>> ", gitDir);
31
+ if (!fs.existsSync(gitDir)) handleError("当前项目没有配置Git");
32
+
33
+ const resourceDir = path.join(projectRoot, src + "/index.html");
34
+ // console.log("resourceDir :>> ", resourceDir);
35
+ const hasBuild = fs.existsSync(resourceDir);
36
+ if (!hasBuild) {
37
+ handleError("未找打包完成的H5资源");
38
+ }
39
+ const targetDir = path.join(projectRoot, dest);
40
+ if (!fs.existsSync(targetDir)) {
41
+ fs.mkdirSync(targetDir, { recursive: true });
42
+ console.log(dest + " 目录创建完成");
43
+ }
44
+ console.log("源目录 :>> ", path.join(projectRoot, src));
45
+ console.log("目标目录 :>> ", targetDir);
46
+
47
+ await moveFiles(path.join(projectRoot, src), targetDir);
48
+
49
+ async function moveFiles(sourceDir, targetDir) {
50
+ // console.log("sourceDir :>> ", sourceDir);
51
+ // console.log("targetDir :>> ", targetDir);
52
+ try {
53
+ // 读取源目录内容
54
+ const files = await fsPromise.readdir(sourceDir, { withFileTypes: true });
55
+ for (const file of files) {
56
+ const sourcePath = path.join(sourceDir, file.name);
57
+ const targetPath = path.join(targetDir, file.name);
58
+
59
+ if (file.isDirectory()) {
60
+ // 创建目录
61
+ await fsPromise.mkdir(targetPath, { recursive: true });
62
+ // 递归处理子目录
63
+ await moveFiles(sourcePath, targetPath);
64
+ } else {
65
+ await fsPromise.copyFile(sourcePath, targetPath);
66
+ }
67
+ }
68
+ } catch (error) {
69
+ handleError(error.message);
70
+ }
71
+ }
72
+
73
+ console.log("🎉 所有文件复制完成 ");
74
+ spawnSync("cd", [projectRoot]);
75
+ const pwd = spawnSync("pwd");
76
+ console.log("当前执行路径:", pwd.stdout.toString());
77
+
78
+ const gitCommands = [
79
+ { cmd: "git", args: ["add", "."] },
80
+ { cmd: "git", args: ["commit", "-m", commitInfo] },
81
+ { cmd: "git", args: ["push"] },
82
+ ];
83
+
84
+ for (const command of gitCommands) {
85
+ const result = spawnSync(command.cmd, command.args, { stdio: "inherit" });
86
+ if (result.status !== 0) {
87
+ console.error(`命令 ${command.cmd} 执行失败,退出码: ${result.status}`);
88
+ handleError(`命令 ${command.cmd} 执行失败`);
89
+ process.exit(1); // 终止后续命令
90
+ }
91
+ }
92
+
93
+ // 基础版:红色错误提示 + 退出码
94
+ function handleError(message, code = 1) {
95
+ console.error("\x1b[31mERROR:\x1b[0m", message);
96
+ process.exit(code);
97
+ }
98
+ }
99
+
100
+ module.exports = main;
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "hbuilder-build-move",
3
+ "version": "0.0.1",
4
+ "description": "将打包好的资源移动到项目指定的目录下",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "hbm": "./index.mjs"
8
+ },
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "author": "hxf",
13
+ "license": "ISC",
14
+ "dependencies": {
15
+ "commander": "^13.1.0"
16
+ }
17
+ }