/
create-scene.ts
171 lines (158 loc) · 5.22 KB
/
create-scene.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { flags } from '@oclif/command';
import {
createSceneAndSceneItems,
CreateSceneItemRequest,
isFailure,
logError,
SceneRelationshipDataTypeEnum,
Utf8,
} from '@vertexvis/vertex-api-client';
import cli from 'cli-ux';
import { readFile } from 'fs-extra';
import BaseCommand from '../lib/base';
import { SceneItem } from '../create-items';
import { vertexClient } from '../lib/client';
import { fileExists } from '../lib/fs';
import { progressBar } from '../lib/progress';
export default class CreateScene extends BaseCommand {
public static description = `Given JSON file containing SceneItems (as defined in src/create-items/index.d.ts), create scene in Vertex.`;
public static examples = [
`$ vertex create-scene --name my-scene [YOUR_PATH_TO_JSON_FILE]
████████████████████████████████████████ 100% | 10/10
f79d4760-0b71-44e4-ad0b-22743fdd4ca3
`,
];
public static args = [{ name: 'path' }];
public static flags = {
...BaseCommand.flags,
name: flags.string({
description: 'Name of scene.',
}),
noFailFast: flags.boolean({
description:
'Whether or not to fail if any scene item fails initial validation.',
default: false,
}),
parallelism: flags.integer({
char: 'p',
description: 'Number of scene-items to create in parallel.',
default: 20,
}),
suppliedId: flags.string({
description: 'SuppliedId of scene.',
}),
treeEnabled: flags.boolean({
description: 'Whether or not scene trees can be viewed for this scene.',
default: false,
}),
};
public async run(): Promise<void> {
const {
args: { path },
flags: {
name,
noFailFast,
parallelism,
suppliedId,
treeEnabled,
verbose,
},
} = this.parse(CreateScene);
const basePath = this.parsedFlags?.basePath;
if (!(await fileExists(path))) {
this.error(`'${path}' is not a valid file path, exiting.`);
}
if (parallelism < 1 || parallelism > 200) {
this.error(`Invalid parallelism ${parallelism}.`);
}
try {
const progress = progressBar('Creating scene');
const client = await vertexClient(basePath, this.userConfig);
const items: SceneItem[] = JSON.parse(await readFile(path, Utf8));
items.sort((a, b) => (a.depth || 0) - (b.depth || 0));
const createSceneItemReqs: CreateSceneItemRequest[] = items.map((i) => ({
data: {
attributes: {
materialOverride: i.materialOverride,
parent: i.parentId,
partInstanceSuppliedIdsAsSuppliedIds: Boolean(
i.suppliedInstanceIdKey
),
source: i.source
? {
suppliedPartId: i.source.suppliedPartId,
suppliedRevisionId: i.source.suppliedRevisionId,
}
: undefined,
suppliedId: i.suppliedId,
transform: i.transform,
visible: true,
},
relationships: {},
type: 'scene-item',
},
}));
progress.start(createSceneItemReqs.length, 0);
const res = await createSceneAndSceneItems({
client,
createSceneItemReqs,
createSceneReq: () => ({
data: {
attributes: { name, suppliedId, treeEnabled },
type: SceneRelationshipDataTypeEnum.Scene,
},
}),
failFast: !noFailFast,
onMsg: console.error,
onProgress: (complete, total) => {
progress.update(complete);
if (complete === total) {
progress.stop();
cli.action.start('Created scene items. Awaiting scene completion');
}
},
parallelism,
verbose: verbose,
});
const scene = res.scene.data;
const getSceneItemsRes = await client.sceneItems.getSceneItems({
id: scene.id,
pageSize: 1,
});
cli.action.stop();
this.log(scene.id);
if (res.errors.length > 0) {
this.warn(`Failed to create the following scene items...`);
cli.table(
res.errors.map((e) => {
const errors = isFailure(e.res)
? e.res.errors
: e.res?.data.attributes.errors;
const first = (errors ? [...errors] : [])[0];
return {
suppliedId: e.req.data.attributes.suppliedId,
suppliedPartId: e.req.data.attributes.source?.suppliedPartId,
suppliedRevisionId:
e.req.data.attributes.source?.suppliedRevisionId,
relSource: e.req.data.relationships.source?.data,
error: first?.detail ? first.detail : first?.title,
};
}),
{
suppliedId: { header: 'Id' },
suppliedPartId: { header: 'PartId' },
suppliedRevisionId: { header: 'RevisionId' },
relSource: { header: 'Source' },
error: { header: 'Error' },
},
{ 'no-truncate': true }
);
}
if (getSceneItemsRes.data.data.length === 0) {
this.error(`No scene items exist in the scene.`);
}
} catch (error) {
logError(error, this.error);
}
}
}