qdmp-cli 0.1.11 → 0.1.13

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/actions.js CHANGED
@@ -8,6 +8,9 @@ import table from "table";
8
8
  import { inquirerAccountPassword } from "./utils/interactive.js";
9
9
  import { login, getUserInfo } from "./api.js";
10
10
  import fs from "fs";
11
+ import path from "path";
12
+ import { createRequire } from "module";
13
+ import { execFileSync } from "child_process";
11
14
  import {
12
15
  getUploadPath,
13
16
  getAppId,
@@ -178,14 +181,32 @@ export const buildAction = async () => {
178
181
  if (buildResult.code !== 0) {
179
182
  throw new Error("构建失败");
180
183
  }
181
- shell.cd("dist");
182
- const dmccResult = shell.exec("npx dmcc build --no-app-id-dir");
183
- if (dmccResult.code !== 0) {
184
+ info("开始 EMP 编译...");
185
+ const require = createRequire(import.meta.url);
186
+ const dmccBin = path.resolve(
187
+ path.dirname(require.resolve("@dimina/compiler")),
188
+ "bin/index.js",
189
+ );
190
+ const distDir = path.resolve(process.cwd(), "dist");
191
+ try {
192
+ execFileSync(
193
+ process.execPath,
194
+ [
195
+ dmccBin,
196
+ "build",
197
+ "--work-path",
198
+ distDir,
199
+ "--target-path",
200
+ distDir,
201
+ "--no-app-id-dir",
202
+ ],
203
+ { stdio: "inherit" },
204
+ );
205
+ } catch (e) {
184
206
  throw new Error(
185
207
  "dmcc 构建失败,请保证 npm run build 产物是一个标准的微信小程序",
186
208
  );
187
209
  }
188
- shell.cd("..");
189
210
  success("构建完成");
190
211
  break;
191
212
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qdmp-cli",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "qdmp-cli",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -18,6 +18,7 @@
18
18
  "author": "zhangyanran",
19
19
  "license": "ISC",
20
20
  "dependencies": {
21
+ "@dimina/compiler": "^1.0.16",
21
22
  "adm-zip": "^0.5.16",
22
23
  "chalk": "^5.4.1",
23
24
  "commander": "^14.0.0",
package/utils/common.js CHANGED
@@ -145,35 +145,82 @@ async function copyDistFolder(dirname) {
145
145
  return false;
146
146
  }
147
147
  }
148
+
149
+ function hasCompiledMainDir(dirname) {
150
+ try {
151
+ return fs.statSync(path.join(dirname, "main")).isDirectory();
152
+ } catch (e) {
153
+ return false;
154
+ }
155
+ }
156
+
157
+ function resolveCompiledEmpDistFolder(compiledOutputDir, compiledAppId) {
158
+ const resolvedOutputDir = path.resolve(compiledOutputDir);
159
+
160
+ if (hasCompiledMainDir(resolvedOutputDir)) {
161
+ return resolvedOutputDir;
162
+ }
163
+
164
+ if (compiledAppId) {
165
+ const appDir = path.join(resolvedOutputDir, compiledAppId);
166
+ if (hasCompiledMainDir(appDir)) {
167
+ return appDir;
168
+ }
169
+ }
170
+
171
+ const candidates = fs.existsSync(resolvedOutputDir)
172
+ ? fs
173
+ .readdirSync(resolvedOutputDir)
174
+ .map((name) => path.join(resolvedOutputDir, name))
175
+ .filter((dirname) => {
176
+ try {
177
+ return fs.statSync(dirname).isDirectory() && hasCompiledMainDir(dirname);
178
+ } catch (e) {
179
+ return false;
180
+ }
181
+ })
182
+ : [];
183
+
184
+ if (candidates.length === 1) {
185
+ return candidates[0];
186
+ }
187
+
188
+ throw new Error("未找到EMP编译产物,请先执行 qdmp build");
189
+ }
190
+
191
+ function writeEmpMetadata(workDir, appId, code) {
192
+ const metadataPath = path.join(workDir, "dist", "main", "qdmp.json");
193
+ fs.mkdirSync(path.dirname(metadataPath), { recursive: true });
194
+
195
+ let qdmpJson = {};
196
+ if (fs.existsSync(metadataPath)) {
197
+ try {
198
+ qdmpJson = JSON.parse(fs.readFileSync(metadataPath, "utf8"));
199
+ } catch (e) {
200
+ qdmpJson = {};
201
+ }
202
+ }
203
+
204
+ qdmpJson.appId = appId;
205
+ if (code != null) {
206
+ qdmpJson.version = code;
207
+ }
208
+
209
+ fs.writeFileSync(metadataPath, JSON.stringify(qdmpJson, null, 2));
210
+ }
211
+
148
212
  /** 复制EMP文件夹
149
213
  * @param {*} dirname 文件夹名
150
214
  */
151
215
  async function copyAndSetEmpDistFolder(appId, code) {
152
- //直接复制根目录下dist文件夹
153
216
  try {
154
- fs.cpSync("dist", `${WORKSPACE_DIR}/dist`, { recursive: true });
155
- if (!fs.existsSync(`${WORKSPACE_DIR}/dist/main/qdmp.json`)) {
156
- fs.writeFileSync(
157
- `${WORKSPACE_DIR}/dist/main/qdmp.json`,
158
- JSON.stringify({
159
- appId,
160
- version: code,
161
- }),
162
- );
163
- } else {
164
- const qdmpJson = JSON.parse(
165
- fs.readFileSync(`${WORKSPACE_DIR}/dist/main/qdmp.json`, "utf8"),
166
- );
167
- qdmpJson.version = code;
168
- fs.writeFileSync(
169
- `${WORKSPACE_DIR}/dist/main/qdmp.json`,
170
- JSON.stringify(qdmpJson),
171
- );
172
- }
217
+ const distSource = resolveCompiledEmpDistFolder("dist", appId);
218
+ fs.cpSync(distSource, `${WORKSPACE_DIR}/dist`, { recursive: true });
219
+ writeEmpMetadata(WORKSPACE_DIR, appId, code);
173
220
  success("文件复制完成");
174
221
  return true;
175
222
  } catch (e) {
176
- error(e);
223
+ error(e.message || e);
177
224
  return false;
178
225
  }
179
226
  }