Skip to content

Commit c19d73d

Browse files
committed
[#81] Add documentType to transformName and transformEntry context.
1 parent 7895867 commit c19d73d

3 files changed

Lines changed: 31 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## 3.0.2
22

33
### Improvements
4-
- Add support for processing ActiveEffect primary Documents.
4+
- Added support for processing ActiveEffect primary Documents.
5+
- Added `documentType` to the context objects passed to `transformEntry` and `transformName`.
56

67
## 3.0.1
78

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ Extract the contents of a compendium pack into individual source files for each
210210
* **log:** *boolean = false* Whether to log operation progress to the console.
211211
* **folders:** *boolean = false* Create a directory structure that matches the pack's Folder documents. Folder documents are written to their matching directory with the name `_Folder.{yml|json}`.
212212
* **documentType:** *string* For NeDB operations, a **documentType** must be provided. This should be the same as the pack's *type* field in the *module.json* or *system.json*.
213-
* **transformEntry:** *(entry: object): Promise<false|void>* A function that is called on every entry. Returning *false* indicates that the entry should be discarded.
214-
* **transformName:** *(entry: object): Promise<string|void>* A function that is called on every entry. The value returned from this will be used as the entry's filename and must include the appropriate file extension. If nothing is returned, an auto-generated name will be used instead.
213+
* **transformEntry:** *(entry: object, context: object): Promise<false|void>* A function that is called on every entry. Returning *false* indicates that the entry should be discarded.
214+
* **transformName:** *(entry: object, context: object): Promise<string|void>* A function that is called on every entry. The value returned from this will be used as the entry's filename and must include the appropriate file extension. If nothing is returned, an auto-generated name will be used instead.
215215
* **transformFolderName:** *(entry: object): Promise<string|void>* A function used to generate a directory name for an extracted Folder document when the `folders` option is used.
216216
* **expandAdventures:** *boolean* Write documents embedded in Adventures to their own files. If the `folders` option is also supplied, the Adventure is treated like a folder, and written to `_Adventure.{yml|json}` instead of `_Folder.{yml|json}`. Additionally, all its entries are grouped into sub-folders by Document type.
217217
* **omitVolatile:** *boolean* When unpacking, diff the candidate entry against an existing one and only write it if non-volatile fields have changed. Currently, `_stats.createdTime`, `_stats.modifiedTime`, `_stats.lastModifiedBy`, `_stats.systemVersion`, and `_stats.coreVersion` are considered volatile.

lib/package.mjs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import { ClassicLevel } from "classic-level";
1212

1313
/**
1414
* @typedef {
15-
* "Actor"|"Adventure"|"Cards"|"ChatMessage"|"Combat"|"FogExploration"|"Folder"|"Item"|"JournalEntry"|"Macro"|
16-
* "Playlist"|"RollTable"|"Scene"|"Setting"|"User"
15+
* "ActiveEffect"|"Actor"|"Adventure"|"Cards"|"ChatMessage"|"Combat"|"FogExploration"|"Folder"|"Item"|"JournalEntry"|
16+
* "Macro"|"Playlist"|"RollTable"|"Scene"|"Setting"|"User"
1717
* } DocumentType
1818
*/
1919

2020
/**
2121
* @typedef {
22-
* "actors"|"adventures"|"cards"|"messages"|"combats"|"fog"|"folders"|"items"|"journal"|"macros"|"playlists"|"tables"|
23-
* "scenes"|"settings"|"users"
22+
* "actors"|"adventures"|"cards"|"messages"|"combats"|"effects"|"fog"|"folders"|"items"|"journal"|"macros"|
23+
* "playlists"|"tables"|"scenes"|"settings"|"users"
2424
* } DocumentCollection
2525
*/
2626

@@ -107,6 +107,7 @@ import { ClassicLevel } from "classic-level";
107107
* @property {object} [adventure.doc] The entire adventure document.
108108
* @property {string} [adventure.path] The path where the adventure will be extracted.
109109
* @property {string} [folder] Folder path if this entry is in a folder and the folders option is enabled.
110+
* @property {string} [documentType] The type of the Document being extracted.
110111
*/
111112

112113
/**
@@ -294,9 +295,11 @@ async function compileNedb(pack, files, { log, transformEntry }={}) {
294295
});
295296
const key = doc._key;
296297
const [, collection] = key.split("!");
298+
const documentType = COLLECTION_TYPE_MAP[collection];
299+
const context = { documentType };
297300
// If the key starts with !folders, we should skip packing it as NeDB doesn't support folders.
298301
if ( key.startsWith("!folders") ) continue;
299-
if ( await transformEntry?.(doc) === false ) continue;
302+
if ( await transformEntry?.(doc, context) === false ) continue;
300303
await packDoc(doc, collection);
301304
await db.insert(doc);
302305
if ( log ) console.log(`Packed ${chalk.blue(doc._id)}${chalk.blue(doc.name ? ` (${doc.name})` : "")}`);
@@ -353,7 +356,9 @@ async function compileClassicLevel(pack, files, { log, transformEntry }={}) {
353356
transformEntry, log
354357
});
355358
const [, collection] = doc._key.split("!");
356-
if ( await transformEntry?.(doc) === false ) continue;
359+
const documentType = COLLECTION_TYPE_MAP[collection];
360+
const context = { documentType };
361+
if ( await transformEntry?.(doc, context) === false ) continue;
357362
await packDoc(doc, collection);
358363
if ( log ) console.log(`Packed ${chalk.blue(doc._id)}${chalk.blue(doc.name ? ` (${doc.name})` : "")}`);
359364
} catch ( err ) {
@@ -385,7 +390,7 @@ async function compileClassicLevel(pack, files, { log, transformEntry }={}) {
385390
* @returns {Promise<void>}
386391
*/
387392
async function reconstructAdventure(src, doc, { transformEntry, log }={}) {
388-
const context = { adventure: doc };
393+
const context = { adventure: { doc } };
389394
for ( const embeddedCollectionName of ADVENTURE_DOCS ) {
390395
const entries = [];
391396
for ( let entry of doc[embeddedCollectionName] ?? [] ) {
@@ -400,8 +405,9 @@ async function reconstructAdventure(src, doc, { transformEntry, log }={}) {
400405
}
401406
const ext = path.extname(file);
402407
const isYaml = ext === ".yml" || ext === ".yaml";
408+
const documentType = COLLECTION_TYPE_MAP[embeddedCollectionName];
403409
entry = isYaml ? YAML.load(contents) : JSON.parse(contents);
404-
if ( await transformEntry?.(entry, context) === false ) continue;
410+
if ( await transformEntry?.(entry, { ...context, documentType }) === false ) continue;
405411
}
406412
entries.push(entry);
407413
}
@@ -500,8 +506,10 @@ async function extractNedb(pack, dest, {
500506
const docs = await db.find({});
501507
for ( const doc of docs ) {
502508
await unpackDoc(doc, collection);
503-
if ( await transformEntry?.(doc) === false ) continue;
504-
let name = await transformName?.(doc);
509+
const documentType = COLLECTION_TYPE_MAP[collection];
510+
const context = { documentType };
511+
if ( await transformEntry?.(doc, context) === false ) continue;
512+
let name = await transformName?.(doc, context);
505513
if ( !name ) {
506514
name = `${doc.name ? `${getSafeFilename(doc.name)}_${doc._id}` : doc._id}.${yaml ? "yml" : "json"}`;
507515
}
@@ -552,15 +560,17 @@ async function extractClassicLevel(pack, dest, {
552560
const [, collection, id] = key.split("!");
553561
if ( collection.includes(".") ) continue; // This is not a primary document, skip it.
554562
await unpackDoc(doc, collection);
555-
if ( await transformEntry?.(doc) === false ) continue;
563+
const documentType = COLLECTION_TYPE_MAP[collection];
564+
const context = { documentType };
565+
if ( await transformEntry?.(doc, context) === false ) continue;
556566
if ( key.startsWith("!adventures") && expandAdventures ) {
557567
await extractAdventure(doc, dest, { folderMap }, {
558568
yaml, yamlOptions, jsonOptions, log, folders, omitVolatile, transformEntry, transformName, existing
559569
});
560570
continue;
561571
}
562572
const folder = folderMap.get(doc.folder)?.path;
563-
let name = await transformName?.(doc, { folder });
573+
let name = await transformName?.(doc, { documentType, folder });
564574
if ( !name ) {
565575
if ( key.startsWith("!folders") && folderMap.has(doc._id) ) {
566576
const folder = folderMap.get(doc._id);
@@ -598,7 +608,7 @@ async function extractAdventure(doc, dest, { folderMap }={}, {
598608

599609
// Prepare name for the adventure
600610
const folder = folderMap?.get(doc.folder)?.path;
601-
let name = await transformName?.(doc, { folder });
611+
let name = await transformName?.(doc, { documentType: "Adventure", folder });
602612
adventureFolder = folders ? path.join(folder ?? "", `${getSafeFilename(doc.name)}_${doc._id}`) : folder;
603613
if ( !name ) {
604614
if ( folders ) {
@@ -618,11 +628,12 @@ async function extractAdventure(doc, dest, { folderMap }={}, {
618628
// Write all documents contained in the adventure
619629
for ( const embeddedCollectionName of ADVENTURE_DOCS ) {
620630
const paths = [];
621-
const typeSuffix = folders ? "" : `_${COLLECTION_TYPE_MAP[embeddedCollectionName]}`;
631+
const documentType = COLLECTION_TYPE_MAP[embeddedCollectionName];
632+
const typeSuffix = folders ? "" : `_${documentType}`;
622633
for ( const embeddedDoc of doc[embeddedCollectionName] ?? [] ) {
623-
if ( await transformEntry?.(embeddedDoc, context) === false ) continue;
634+
if ( await transformEntry?.(embeddedDoc, { ...context, documentType }) === false ) continue;
624635
let embeddedFolder = path.join(adventureFolder ?? "", embeddedFolderMap.get(embeddedDoc.folder)?.path ?? "");
625-
let embeddedName = await transformName?.(embeddedDoc, { ...context, folder: embeddedFolder });
636+
let embeddedName = await transformName?.(embeddedDoc, { ...context, documentType, folder: embeddedFolder });
626637
if ( !embeddedName ) {
627638
const { name, _id: id } = embeddedDoc;
628639
if ( (embeddedCollectionName === "folders") && embeddedFolderMap.has(embeddedDoc._id) ) {

0 commit comments

Comments
 (0)