(_options: XMPOptions = XMP_DEFAULTS)
| 226 | } |
| 227 | |
| 228 | export 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 | } |
no test coverage detected