@galacean/cli 0.0.3 → 0.0.5

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/Markdown.js CHANGED
@@ -1,18 +1,17 @@
1
1
  import fs from "fs-extra";
2
2
  import path from "path";
3
- export class Markdown {
4
- _txt = "";
3
+ export const markdown = {
4
+ _txt: "",
5
5
  get txt() {
6
6
  return this._txt;
7
- }
7
+ },
8
8
  addLine(line, tab = 0) {
9
9
  for (let i = 0; i < tab; i++) {
10
10
  line = "\t" + line;
11
11
  }
12
12
  this._txt += line + "\n";
13
- }
13
+ },
14
14
  generate() {
15
15
  fs.writeFileSync(path.join(process.cwd(), "项目 Diff.md"), this._txt);
16
- }
17
- }
18
- export const markdown = new Markdown();
16
+ },
17
+ };
package/lib/cli.js CHANGED
@@ -1,8 +1,17 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  import { cac } from "cac";
2
11
  import path from "path";
3
12
  import fs from "fs-extra";
4
13
  import * as url from "url";
5
- import { diffProjects } from "./diff.js";
14
+ import { _diffProjects, diffProjects } from "./diff.js";
6
15
  import { getFileBufferFromUrl } from "./utils.js";
7
16
  import { markdown } from "./Markdown.js";
8
17
  const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
@@ -16,12 +25,21 @@ cli.command("diff [...files]", "diff files").action((files) => {
16
25
  Promise.all([
17
26
  getFileBufferFromUrl(files[0]),
18
27
  getFileBufferFromUrl(files[1]),
19
- ]).then(async (res) => {
28
+ ]).then((res) => __awaiter(void 0, void 0, void 0, function* () {
20
29
  const result = res.map((item) => JSON.parse(item.toString()));
21
- await diffProjects(result[0], result[1]);
30
+ yield diffProjects(result[0], result[1]);
22
31
  markdown.generate();
23
- });
32
+ }));
24
33
  });
34
+ export function diffProject(before, after) {
35
+ return Promise.all([
36
+ getFileBufferFromUrl(before),
37
+ getFileBufferFromUrl(after),
38
+ ]).then((res) => {
39
+ const result = res.map((item) => JSON.parse(item.toString()));
40
+ return _diffProjects(result[0], result[1]);
41
+ });
42
+ }
25
43
  const pkg = fs.readJSONSync(path.join(__dirname, "../package.json"));
26
44
  cli.help();
27
45
  cli.version(pkg.version);
package/lib/diff.js CHANGED
@@ -1,106 +1,204 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  import { Operation, diff, flattenChangeset } from "json-diff-ts";
2
11
  import { groupBy, cloneDeep } from "lodash-es";
3
12
  import { getFileBufferFromUrl } from "./utils.js";
4
13
  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
- markdown.addLine("## 资产");
19
- markdown.addLine(``);
20
- markdown.addLine(`- 资产新增`);
21
- diffs[Operation.ADD]?.forEach((item) => {
22
- markdown.addLine(`- ${item.value.virtualPath}`, 1);
23
- });
24
- markdown.addLine(``);
25
- markdown.addLine(`- 资产移除`);
26
- diffs[Operation.REMOVE]?.forEach((item) => {
27
- markdown.addLine(`- ${item.value.virtualPath}`, 1);
28
- });
29
- // markdown.addLine(``);
30
- // markdown.addLine(`- 资产修改`);
31
- // markdown.addLine(``);
32
- diffs[Operation.UPDATE]?.forEach((item) => {
33
- const id = getIdFromPath(item.path);
34
- const asset = assets1[id];
35
- if (asset.type === "Scene") {
36
- sceneChanges.push(asset);
37
- }
38
- // markdown.addLine(`- ${asset.virtualPath}`, 1);
14
+ export function _diffProjects(project1, project2) {
15
+ var _a, _b, _c;
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ const result = diff(project1, project2, { files: "id" });
18
+ const changes = flattenChangeset(result);
19
+ const assets1 = {};
20
+ const assets2 = {};
21
+ project1.files.forEach((item) => {
22
+ assets1[item.id] = item;
23
+ });
24
+ project2.files.forEach((item) => {
25
+ assets2[item.id] = item;
26
+ });
27
+ const diffs = groupBy(changes, "type");
28
+ const sceneChanges = [];
29
+ const assetsChangeInfo = { add: [], remove: [] };
30
+ const scenesChangeInfo = {};
31
+ (_a = diffs[Operation.ADD]) === null || _a === void 0 ? void 0 : _a.forEach((item) => {
32
+ assetsChangeInfo.add.push(item.value.virtualPath);
33
+ });
34
+ (_b = diffs[Operation.REMOVE]) === null || _b === void 0 ? void 0 : _b.forEach((item) => {
35
+ assetsChangeInfo.remove.push(item.value.virtualPath);
36
+ });
37
+ (_c = diffs[Operation.UPDATE]) === null || _c === void 0 ? void 0 : _c.forEach((item) => {
38
+ const id = getIdFromPath(item.path);
39
+ const asset = assets1[id];
40
+ if (asset.type === "Scene") {
41
+ sceneChanges.push(asset);
42
+ }
43
+ });
44
+ yield Promise.all(sceneChanges.map((asset) => __awaiter(this, void 0, void 0, function* () {
45
+ scenesChangeInfo[assets2[asset.id].virtualPath] = yield _diffScene(asset.path, assets2[asset.id].path);
46
+ })));
47
+ return { assetsChangeInfo, scenesChangeInfo };
39
48
  });
40
- markdown.addLine(``);
41
- markdown.addLine("## 场景");
42
- markdown.addLine(``);
43
- await Promise.all(sceneChanges.map((asset) => {
44
- markdown.addLine(`### ${assets2[asset.id].virtualPath}`);
45
- markdown.addLine(``);
46
- return diffScene(asset.path, assets2[asset.id].path);
47
- }));
48
49
  }
49
- async function diffScene(url1, url2) {
50
- const [scene1, scene2] = await Promise.all([
51
- getFileBufferFromUrl(url1),
52
- getFileBufferFromUrl(url2),
53
- ]).then((res) => res.map((item) => JSON.parse(item.toString())));
54
- const { diffObj: diffObj1, entities } = getEntities(scene1);
55
- const { diffObj: diffObj2, entities: newEntities } = getEntities(scene2);
56
- 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
- const groups = groupBy(changes, (item) => {
64
- if (item.path.includes(".components")) {
65
- return "components";
66
- }
67
- return "entities";
68
- });
69
- if (groups["entities"]) {
70
- markdown.addLine(`#### Entities`);
71
- markdown.addLine("");
72
- const changesGroup = groupBy(groups["entities"], "type");
73
- markdown.addLine(`- 新增`);
74
- changesGroup[Operation.ADD]?.forEach((item) => {
75
- const entityPath = getEntityPath(item.key, newEntities);
76
- markdown.addLine(`- ${entityPath}`, 1);
50
+ export function diffProjects(project1, project2) {
51
+ var _a, _b, _c;
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ const result = diff(project1, project2, { files: "id" });
54
+ const changes = flattenChangeset(result);
55
+ const assets1 = {};
56
+ const assets2 = {};
57
+ project1.files.forEach((item) => {
58
+ assets1[item.id] = item;
77
59
  });
78
- markdown.addLine(`- 移除`);
79
- changesGroup[Operation.REMOVE]?.forEach((item) => {
80
- const entityPath = getEntityPath(item.key, entities);
81
- markdown.addLine(`- ${entityPath}`, 1);
60
+ project2.files.forEach((item) => {
61
+ assets2[item.id] = item;
62
+ });
63
+ const diffs = groupBy(changes, "type");
64
+ const sceneChanges = [];
65
+ markdown.addLine("## 资产");
66
+ markdown.addLine(``);
67
+ markdown.addLine(`- 资产新增`);
68
+ (_a = diffs[Operation.ADD]) === null || _a === void 0 ? void 0 : _a.forEach((item) => {
69
+ markdown.addLine(`- ${item.value.virtualPath}`, 1);
82
70
  });
83
- }
84
- if (groups["components"]) {
85
71
  markdown.addLine(``);
86
- markdown.addLine(`#### 组件`);
87
- markdown.addLine("");
88
- const changesGroup = groupBy(groups["components"], "type");
89
- markdown.addLine(`- 新增`);
90
- changesGroup[Operation.ADD]?.forEach((item) => {
91
- const entityId = getComponentEntityId(item.path);
92
- const entityPath = getEntityPath(entityId, entities);
93
- const comp = item.value;
94
- markdown.addLine(`- ${entityPath} [${comp["class"]}]`, 1);
72
+ markdown.addLine(`- 资产移除`);
73
+ (_b = diffs[Operation.REMOVE]) === null || _b === void 0 ? void 0 : _b.forEach((item) => {
74
+ markdown.addLine(`- ${item.value.virtualPath}`, 1);
95
75
  });
96
- markdown.addLine(`- 移除`);
97
- changesGroup[Operation.REMOVE]?.forEach((item) => {
98
- const entityId = getComponentEntityId(item.path);
99
- const entityPath = getEntityPath(entityId, entities);
100
- const comp = item.value;
101
- markdown.addLine(`- ${entityPath} [${comp["class"]}]`, 1);
76
+ // markdown.addLine(``);
77
+ // markdown.addLine(`- 资产修改`);
78
+ // markdown.addLine(``);
79
+ (_c = diffs[Operation.UPDATE]) === null || _c === void 0 ? void 0 : _c.forEach((item) => {
80
+ const id = getIdFromPath(item.path);
81
+ const asset = assets1[id];
82
+ if (asset.type === "Scene") {
83
+ sceneChanges.push(asset);
84
+ }
85
+ // markdown.addLine(`- ${asset.virtualPath}`, 1);
102
86
  });
103
- }
87
+ markdown.addLine(``);
88
+ markdown.addLine("## 场景");
89
+ markdown.addLine(``);
90
+ yield Promise.all(sceneChanges.map((asset) => {
91
+ markdown.addLine(`### ${assets2[asset.id].virtualPath}`);
92
+ markdown.addLine(``);
93
+ return diffScene(asset.path, assets2[asset.id].path);
94
+ }));
95
+ });
96
+ }
97
+ function diffScene(url1, url2) {
98
+ var _a, _b, _c, _d;
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ const [scene1, scene2] = yield Promise.all([
101
+ getFileBufferFromUrl(url1),
102
+ getFileBufferFromUrl(url2),
103
+ ]).then((res) => res.map((item) => JSON.parse(item.toString())));
104
+ const { diffObj: diffObj1, entities } = getEntities(scene1);
105
+ const { diffObj: diffObj2, entities: newEntities } = getEntities(scene2);
106
+ const changes = flattenChangeset(diff(diffObj1, diffObj2));
107
+ const groups = groupBy(changes, (item) => {
108
+ if (item.path.includes(".components")) {
109
+ return "components";
110
+ }
111
+ return "entities";
112
+ });
113
+ if (groups["entities"]) {
114
+ markdown.addLine(`#### Entities`);
115
+ markdown.addLine("");
116
+ const changesGroup = groupBy(groups["entities"], "type");
117
+ markdown.addLine(`- 新增`);
118
+ (_a = changesGroup[Operation.ADD]) === null || _a === void 0 ? void 0 : _a.forEach((item) => {
119
+ const entityPath = getEntityPath(item.key, newEntities);
120
+ markdown.addLine(`- ${entityPath}`, 1);
121
+ });
122
+ markdown.addLine(`- 移除`);
123
+ (_b = changesGroup[Operation.REMOVE]) === null || _b === void 0 ? void 0 : _b.forEach((item) => {
124
+ const entityPath = getEntityPath(item.key, entities);
125
+ markdown.addLine(`- ${entityPath}`, 1);
126
+ });
127
+ }
128
+ if (groups["components"]) {
129
+ markdown.addLine(``);
130
+ markdown.addLine(`#### 组件`);
131
+ markdown.addLine("");
132
+ const changesGroup = groupBy(groups["components"], "type");
133
+ markdown.addLine(`- 新增`);
134
+ (_c = changesGroup[Operation.ADD]) === null || _c === void 0 ? void 0 : _c.forEach((item) => {
135
+ const entityId = getComponentEntityId(item.path);
136
+ const entityPath = getEntityPath(entityId, entities);
137
+ const comp = item.value;
138
+ markdown.addLine(`- ${entityPath} [${comp["class"]}]`, 1);
139
+ });
140
+ markdown.addLine(`- 移除`);
141
+ (_d = changesGroup[Operation.REMOVE]) === null || _d === void 0 ? void 0 : _d.forEach((item) => {
142
+ const entityId = getComponentEntityId(item.path);
143
+ const entityPath = getEntityPath(entityId, entities);
144
+ const comp = item.value;
145
+ markdown.addLine(`- ${entityPath} [${comp["class"]}]`, 1);
146
+ });
147
+ }
148
+ });
149
+ }
150
+ function _diffScene(url1, url2) {
151
+ var _a, _b, _c, _d;
152
+ return __awaiter(this, void 0, void 0, function* () {
153
+ const [scene1, scene2] = yield Promise.all([
154
+ getFileBufferFromUrl(url1),
155
+ getFileBufferFromUrl(url2),
156
+ ]).then((res) => res.map((item) => JSON.parse(item.toString())));
157
+ const { diffObj: diffObj1, entities } = getEntities(scene1);
158
+ const { diffObj: diffObj2, entities: newEntities } = getEntities(scene2);
159
+ const changes = flattenChangeset(diff(diffObj1, diffObj2));
160
+ const sceneChangeInfo = {
161
+ entities: { add: [], remove: [] },
162
+ components: { add: [], remove: [] },
163
+ error: null,
164
+ };
165
+ if (Object.values(newEntities).length <= 0) {
166
+ sceneChangeInfo.error = "Entities is empty";
167
+ }
168
+ const groups = groupBy(changes, (item) => {
169
+ if (item.path.includes(".components")) {
170
+ return "components";
171
+ }
172
+ return "entities";
173
+ });
174
+ if (groups["entities"]) {
175
+ const changesGroup = groupBy(groups["entities"], "type");
176
+ (_a = changesGroup[Operation.ADD]) === null || _a === void 0 ? void 0 : _a.forEach((item) => {
177
+ const entityPath = getEntityPath(item.key, newEntities);
178
+ sceneChangeInfo.entities.add.push(entityPath);
179
+ });
180
+ (_b = changesGroup[Operation.REMOVE]) === null || _b === void 0 ? void 0 : _b.forEach((item) => {
181
+ const entityPath = getEntityPath(item.key, entities);
182
+ sceneChangeInfo.entities.remove.push(entityPath);
183
+ });
184
+ }
185
+ if (groups["components"]) {
186
+ const changesGroup = groupBy(groups["components"], "type");
187
+ (_c = changesGroup[Operation.ADD]) === null || _c === void 0 ? void 0 : _c.forEach((item) => {
188
+ const entityId = getComponentEntityId(item.path);
189
+ const entityPath = getEntityPath(entityId, entities);
190
+ const comp = item.value;
191
+ sceneChangeInfo.components.add.push(`- ${entityPath} [${comp["class"]}]`);
192
+ });
193
+ (_d = changesGroup[Operation.REMOVE]) === null || _d === void 0 ? void 0 : _d.forEach((item) => {
194
+ const entityId = getComponentEntityId(item.path);
195
+ const entityPath = getEntityPath(entityId, entities);
196
+ const comp = item.value;
197
+ sceneChangeInfo.components.remove.push(`- ${entityPath} [${comp["class"]}]`);
198
+ });
199
+ }
200
+ return sceneChangeInfo;
201
+ });
104
202
  }
105
203
  function getComponentEntityId(str) {
106
204
  let regex = /\.([^.]*?)\.components/;
package/lib/utils.js CHANGED
@@ -1,3 +1,12 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  import https from "https";
2
11
  import http from "http";
3
12
  import fs from "fs-extra";
@@ -71,29 +80,31 @@ export function sum(array) {
71
80
  }
72
81
  return sum;
73
82
  }
74
- export async function getBufferAndNameFromUri(uri) {
75
- let buffer;
76
- let name;
77
- const isUrl = /^(http|https):\/\/[^\s/$.?#].[^\s]*$/.test(uri);
78
- if (isUrl) {
79
- buffer = await urlToBuffer(uri);
80
- const urlRegex = /\/([^/]+\.[^/.]+)$/;
81
- const matches = uri.match(urlRegex);
82
- if (matches) {
83
- name = matches[1];
83
+ export function getBufferAndNameFromUri(uri) {
84
+ return __awaiter(this, void 0, void 0, function* () {
85
+ let buffer;
86
+ let name;
87
+ const isUrl = /^(http|https):\/\/[^\s/$.?#].[^\s]*$/.test(uri);
88
+ if (isUrl) {
89
+ buffer = yield urlToBuffer(uri);
90
+ const urlRegex = /\/([^/]+\.[^/.]+)$/;
91
+ const matches = uri.match(urlRegex);
92
+ if (matches) {
93
+ name = matches[1];
94
+ }
95
+ else {
96
+ name = "noname";
97
+ }
84
98
  }
85
99
  else {
86
- name = "noname";
100
+ uri = uri !== null && uri !== void 0 ? uri : ".";
101
+ const filepath = path.isAbsolute(uri) ? uri : path.join(process.cwd(), uri);
102
+ const stream = yield fs.createReadStream(filepath);
103
+ buffer = yield streamToBuffer(stream);
104
+ name = path.basename(filepath);
87
105
  }
88
- }
89
- else {
90
- uri = uri ?? ".";
91
- const filepath = path.isAbsolute(uri) ? uri : path.join(process.cwd(), uri);
92
- const stream = await fs.createReadStream(filepath);
93
- buffer = await streamToBuffer(stream);
94
- name = path.basename(filepath);
95
- }
96
- return { buffer, name };
106
+ return { buffer, name };
107
+ });
97
108
  }
98
109
  export function stringIsAValidUrl(s) {
99
110
  try {
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.5",
4
+ "main": "lib/index.js",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "gz65555",
@@ -1,7 +1,6 @@
1
- export declare class Markdown {
2
- private _txt;
3
- get txt(): string;
1
+ export declare const markdown: {
2
+ _txt: string;
3
+ readonly txt: any;
4
4
  addLine(line: string, tab?: number): void;
5
5
  generate(): void;
6
- }
7
- export declare const markdown: Markdown;
6
+ };
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>;