MCPcopy
hub / github.com/donmccurdy/glTF-Transform / xmp

Function xmp

packages/cli/src/transforms/xmp.ts:228–295  ·  view source on GitHub ↗
(_options: XMPOptions = XMP_DEFAULTS)

Source from the content-addressed store, hash-verified

226}
227
228export const xmp = (_options: XMPOptions = XMP_DEFAULTS): Transform => {
229 const options = { ...XMP_DEFAULTS, ..._options } as Required<XMPOptions>;
230
231 return async (document: Document): Promise<void> => {
232 const logger = document.getLogger();
233 const root = document.getRoot();
234 const xmpExtension = document.createExtension(KHRXMP);
235
236 if (options.reset) {
237 xmpExtension.dispose();
238 logger.info('[xmp]: Reset XMP metadata.');
239 logger.debug('[xmp]: Complete.');
240 return;
241 }
242
243 if (options.packet) {
244 const packetPath = path.resolve(options.packet);
245 logger.info(`[xmp]: Loading "${packetPath}"...`);
246 const packetJSON = await fs.readFile(packetPath, 'utf-8');
247 const packetDef = validatePacket(JSON.parse(packetJSON));
248 const packet = xmpExtension.createPacket().fromJSONLD(packetDef);
249 root.setExtension('KHR_xmp_json_ld', packet);
250 logger.debug('[xmp]: Complete.');
251 return;
252 }
253
254 const packet = root.getExtension<Packet>('KHR_xmp_json_ld') || xmpExtension.createPacket();
255 const results = packet.toJSONLD();
256
257 try {
258 for await (const question of generateQuestions(results)) {
259 Object.assign(results, await prompts(question));
260 }
261 } catch (e) {
262 checkTTY(e, logger);
263 throw e;
264 }
265
266 // Context.
267 packet.setContext({
268 ...packet.getContext(),
269 ...createContext(results),
270 xmp: XMPContext.xmp, // required for xmp:MetadataDate below.
271 });
272
273 // Properties.
274 let numProperties = 0;
275 for (const name in results) {
276 // NOTICE: Calling 'continue' in this context hits a Babel bug.
277 if (!name.startsWith('_') && !name.startsWith('@') && results[name]) {
278 packet.setProperty(name, results[name] as string);
279 numProperties++;
280 }
281 }
282
283 if (numProperties === 0) {
284 throw new Error('xmp: No properties added.');
285 }

Callers 1

cli.tsFile · 0.85

Calls 15

validatePacketFunction · 0.85
generateQuestionsFunction · 0.85
checkTTYFunction · 0.85
createContextFunction · 0.85
getLoggerMethod · 0.80
getRootMethod · 0.80
createExtensionMethod · 0.80
fromJSONLDMethod · 0.80
createPacketMethod · 0.80
toJSONLDMethod · 0.80
setContextMethod · 0.80
getContextMethod · 0.80

Tested by

no test coverage detected