@galacean/cli 0.0.3 → 0.0.4

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/lib/cli.js CHANGED
@@ -2,7 +2,7 @@ import { cac } from "cac";
2
2
  import path from "path";
3
3
  import fs from "fs-extra";
4
4
  import * as url from "url";
5
- import { diffProjects } from "./diff.js";
5
+ import { _diffProjects, diffProjects } from "./diff.js";
6
6
  import { getFileBufferFromUrl } from "./utils.js";
7
7
  import { markdown } from "./Markdown.js";
8
8
  const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
@@ -22,6 +22,15 @@ cli.command("diff [...files]", "diff files").action((files) => {
22
22
  markdown.generate();
23
23
  });
24
24
  });
25
+ export function diffProject(before, after) {
26
+ return Promise.all([
27
+ getFileBufferFromUrl(before),
28
+ getFileBufferFromUrl(after),
29
+ ]).then((res) => {
30
+ const result = res.map((item) => JSON.parse(item.toString()));
31
+ return _diffProjects(result[0], result[1]);
32
+ });
33
+ }
25
34
  const pkg = fs.readJSONSync(path.join(__dirname, "../package.json"));
26
35
  cli.help();
27
36
  cli.version(pkg.version);
package/lib/diff.js CHANGED
@@ -2,6 +2,39 @@ import { Operation, diff, flattenChangeset } from "json-diff-ts";
2
2
  import { groupBy, cloneDeep } from "lodash-es";
3
3
  import { getFileBufferFromUrl } from "./utils.js";
4
4
  import { markdown } from "./Markdown.js";
5
+ export async function _diffProjects(project1, project2) {
6
+ const result = diff(project1, project2, { files: "id" });
7
+ const changes = flattenChangeset(result);
8
+ const assets1 = {};
9
+ const assets2 = {};
10
+ project1.files.forEach((item) => {
11
+ assets1[item.id] = item;
12
+ });
13
+ project2.files.forEach((item) => {
14
+ assets2[item.id] = item;
15
+ });
16
+ const diffs = groupBy(changes, "type");
17
+ const sceneChanges = [];
18
+ const assetsChangeInfo = { add: [], remove: [] };
19
+ const scenesChangeInfo = {};
20
+ diffs[Operation.ADD]?.forEach((item) => {
21
+ assetsChangeInfo.add.push(item.value.virtualPath);
22
+ });
23
+ diffs[Operation.REMOVE]?.forEach((item) => {
24
+ assetsChangeInfo.remove.push(item.value.virtualPath);
25
+ });
26
+ diffs[Operation.UPDATE]?.forEach((item) => {
27
+ const id = getIdFromPath(item.path);
28
+ const asset = assets1[id];
29
+ if (asset.type === "Scene") {
30
+ sceneChanges.push(asset);
31
+ }
32
+ });
33
+ await Promise.all(sceneChanges.map(async (asset) => {
34
+ scenesChangeInfo[assets2[asset.id].virtualPath] = await _diffScene(asset.path, assets2[asset.id].path);
35
+ }));
36
+ return { assetsChangeInfo, scenesChangeInfo };
37
+ }
5
38
  export async function diffProjects(project1, project2) {
6
39
  const result = diff(project1, project2, { files: "id" });
7
40
  const changes = flattenChangeset(result);
@@ -54,12 +87,6 @@ async function diffScene(url1, url2) {
54
87
  const { diffObj: diffObj1, entities } = getEntities(scene1);
55
88
  const { diffObj: diffObj2, entities: newEntities } = getEntities(scene2);
56
89
  const changes = flattenChangeset(diff(diffObj1, diffObj2));
57
- // const changesGroup = groupBy(changes, "type");
58
- // markdown.addLine(``);
59
- // markdown.addLine(`- Entity 移除`);
60
- // changesGroup[Operation.REMOVE]?.forEach((item) => {
61
- // markdown.addLine(`- ${entities[item.key].name}`, 1);
62
- // });
63
90
  const groups = groupBy(changes, (item) => {
64
91
  if (item.path.includes(".components")) {
65
92
  return "components";
@@ -102,6 +129,56 @@ async function diffScene(url1, url2) {
102
129
  });
103
130
  }
104
131
  }
132
+ async function _diffScene(url1, url2) {
133
+ const [scene1, scene2] = await Promise.all([
134
+ getFileBufferFromUrl(url1),
135
+ getFileBufferFromUrl(url2),
136
+ ]).then((res) => res.map((item) => JSON.parse(item.toString())));
137
+ const { diffObj: diffObj1, entities } = getEntities(scene1);
138
+ const { diffObj: diffObj2, entities: newEntities } = getEntities(scene2);
139
+ const changes = flattenChangeset(diff(diffObj1, diffObj2));
140
+ const sceneChangeInfo = {
141
+ entities: { add: [], remove: [] },
142
+ components: { add: [], remove: [] },
143
+ error: null,
144
+ };
145
+ if (Object.values(newEntities).length <= 0) {
146
+ sceneChangeInfo.error = "Entities is empty";
147
+ }
148
+ const groups = groupBy(changes, (item) => {
149
+ if (item.path.includes(".components")) {
150
+ return "components";
151
+ }
152
+ return "entities";
153
+ });
154
+ if (groups["entities"]) {
155
+ const changesGroup = groupBy(groups["entities"], "type");
156
+ changesGroup[Operation.ADD]?.forEach((item) => {
157
+ const entityPath = getEntityPath(item.key, newEntities);
158
+ sceneChangeInfo.entities.add.push(entityPath);
159
+ });
160
+ changesGroup[Operation.REMOVE]?.forEach((item) => {
161
+ const entityPath = getEntityPath(item.key, entities);
162
+ sceneChangeInfo.entities.remove.push(entityPath);
163
+ });
164
+ }
165
+ if (groups["components"]) {
166
+ const changesGroup = groupBy(groups["components"], "type");
167
+ changesGroup[Operation.ADD]?.forEach((item) => {
168
+ const entityId = getComponentEntityId(item.path);
169
+ const entityPath = getEntityPath(entityId, entities);
170
+ const comp = item.value;
171
+ sceneChangeInfo.components.add.push(`- ${entityPath} [${comp["class"]}]`);
172
+ });
173
+ changesGroup[Operation.REMOVE]?.forEach((item) => {
174
+ const entityId = getComponentEntityId(item.path);
175
+ const entityPath = getEntityPath(entityId, entities);
176
+ const comp = item.value;
177
+ sceneChangeInfo.components.remove.push(`- ${entityPath} [${comp["class"]}]`);
178
+ });
179
+ }
180
+ return sceneChangeInfo;
181
+ }
105
182
  function getComponentEntityId(str) {
106
183
  let regex = /\.([^.]*?)\.components/;
107
184
  let matches = str.match(regex);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@galacean/cli",
3
- "version": "0.0.3",
4
- "main": "index.js",
3
+ "version": "0.0.4",
4
+ "main": "lib/index.js",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "gz65555",
package/types/cli.d.ts CHANGED
@@ -1 +1,7 @@
1
- export {};
1
+ export declare function diffProject(before: string, after: string): Promise<{
2
+ assetsChangeInfo: {
3
+ add: any[];
4
+ remove: any[];
5
+ };
6
+ scenesChangeInfo: {};
7
+ }>;
package/types/diff.d.ts CHANGED
@@ -1 +1,8 @@
1
+ export declare function _diffProjects(project1: any, project2: any): Promise<{
2
+ assetsChangeInfo: {
3
+ add: any[];
4
+ remove: any[];
5
+ };
6
+ scenesChangeInfo: {};
7
+ }>;
1
8
  export declare function diffProjects(project1: any, project2: any): Promise<void>;