Skip to content

Commit b87b74b

Browse files
committed
[#88] Fix error thrown when attempting to pack or unpack with --in and --out if configuration hadn't been completed.
1 parent 5273994 commit b87b74b

4 files changed

Lines changed: 39 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 3.0.3
2+
3+
### Improvements
4+
- Added support for processing Level embedded Documents.
5+
- (Mana) Added the `transformSerialized` option which can be used to modify the serialized content before it is written to disk.
6+
7+
### Fixes
8+
- Fixed error thrown when attempting to pack or unpack with `--in` and `--out` options if configuration hadn't been completed.
9+
110
## 3.0.2
211

312
### Improvements

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ Extract the contents of a compendium pack into individual source files for each
213213
* **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.
214214
* **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.
216+
* **transformSerialized:** *(content: string, context: object): Promise<string>* A function that is called on the serialized content. The value returned from this will be written to disk.
216217
* **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.
217218
* **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.
218219
* **jsonOptions:** *object*

commands/package.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ function determinePaths(argv, operation) {
317317
return {};
318318
}
319319

320-
const typeDir = `${currentPackageType.toLowerCase()}s`;
321320
const compendiumName = argv.compendiumName ?? argv.value;
322321
if ( !compendiumName ) {
323322
console.error("No compendium name provided. Use `-n <name>` to supply it.");
@@ -327,7 +326,10 @@ function determinePaths(argv, operation) {
327326
let pack = operation === "pack" ? argv.outputDirectory : argv.inputDirectory;
328327
let source = operation === "pack" ? argv.inputDirectory : argv.outputDirectory;
329328
if ( pack ) pack = path.join(pack, compendiumName);
330-
else pack = path.join(dataPath, "Data", typeDir, currentPackageId, "packs", compendiumName);
329+
else {
330+
const typeDir = `${currentPackageType.toLowerCase()}s`;
331+
pack = path.join(dataPath, "Data", typeDir, currentPackageId, "packs", compendiumName);
332+
}
331333
source ??= path.join(pack, "_source");
332334
if ( argv.nedb ) pack += ".db";
333335
return { source: path.resolve(path.normalize(source)), pack: path.resolve(path.normalize(pack)) };

lib/package.mjs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ import { ClassicLevel } from "classic-level";
6464
* to that resolved location.
6565
* @property {NameTransformer} [transformFolderName] A function used to generate a filename for an extracted folder
6666
* when the folders option is enabled.
67-
* @property {SerializationTransformer} [transformSerialized] A function used to transform serialized data before writing.
67+
* @property {SerializationTransformer} [transformSerialized] A function used to transform serialized data before
68+
* writing.
6869
*/
6970

7071
/**
@@ -104,10 +105,11 @@ import { ClassicLevel } from "classic-level";
104105

105106
/**
106107
* @callback SerializationTransformer
107-
* @param {string} serialized Serialized data
108-
* @param {object} context Additional context
109-
* @param {string} context.filename Name of the file being transformed
110-
* @param {boolean} context.yaml Whether this is YAML or JSON
108+
* @param {string} content The data that will be written.
109+
* @param {object} context Additional serialization context.
110+
* @param {string} context.filename The full path to the file that will be written.
111+
* @param {boolean} context.yaml Whether this is YAML or JSON.
112+
* @returns {Promise<string>} The transformed data to write.
111113
*/
112114

113115
/**
@@ -187,6 +189,7 @@ const HIERARCHY = {
187189
scenes: {
188190
drawings: [],
189191
tokens: [],
192+
levels: [],
190193
lights: [],
191194
notes: [],
192195
regions: [],
@@ -474,7 +477,8 @@ export async function extractPack(src, dest, {
474477
try {
475478
if ( nedb ) {
476479
await extractNedb(src, tmp, {
477-
yaml, yamlOptions, jsonOptions, omitVolatile, log, collection, transformEntry, transformName, transformSerialized, existing: dest
480+
yaml, yamlOptions, jsonOptions, omitVolatile, log, collection, transformEntry, transformName,
481+
transformSerialized, existing: dest
478482
});
479483
}
480484
await extractClassicLevel(src, tmp, {
@@ -499,7 +503,8 @@ export async function extractPack(src, dest, {
499503
* @returns {Promise<void>}
500504
*/
501505
async function extractNedb(pack, dest, {
502-
yaml, yamlOptions, jsonOptions, omitVolatile, log, collection, transformEntry, transformName, transformSerialized, existing
506+
yaml, yamlOptions, jsonOptions, omitVolatile, log, collection, transformEntry, transformName, transformSerialized,
507+
existing
503508
}={}) {
504509
// Load the NeDB file.
505510
const db = new Datastore({ filename: pack, autoload: true });
@@ -524,7 +529,7 @@ async function extractNedb(pack, dest, {
524529
}
525530
const filename = path.join(dest, name);
526531
await serializeDocument(checkVolatile(doc, name, { collection, omitVolatile, existing, yaml }), filename, {
527-
yaml, yamlOptions, jsonOptions, transformSerialized, filename: name
532+
yaml, yamlOptions, jsonOptions, transformSerialized
528533
});
529534
if ( log ) console.log(`Wrote ${chalk.blue(name)}`);
530535
}
@@ -541,8 +546,8 @@ async function extractNedb(pack, dest, {
541546
* @returns {Promise<void>}
542547
*/
543548
async function extractClassicLevel(pack, dest, {
544-
yaml, yamlOptions, jsonOptions, log, folders, expandAdventures, omitVolatile, transformEntry, transformName, transformSerialized,
545-
transformFolderName, existing
549+
yaml, yamlOptions, jsonOptions, log, folders, expandAdventures, omitVolatile, transformEntry, transformName,
550+
transformFolderName, transformSerialized, existing
546551
}={}) {
547552
// Load the directory as a ClassicLevel DB.
548553
const db = new ClassicLevel(pack, { keyEncoding: "utf8", valueEncoding: "json", createIfMissing: false });
@@ -574,7 +579,8 @@ async function extractClassicLevel(pack, dest, {
574579
if ( await transformEntry?.(doc, context) === false ) continue;
575580
if ( key.startsWith("!adventures") && expandAdventures ) {
576581
await extractAdventure(doc, dest, { folderMap }, {
577-
yaml, yamlOptions, jsonOptions, log, folders, omitVolatile, transformEntry, transformName, transformSerialized, existing
582+
yaml, yamlOptions, jsonOptions, log, folders, omitVolatile, transformEntry, transformName, transformSerialized,
583+
existing
578584
});
579585
continue;
580586
}
@@ -591,7 +597,7 @@ async function extractClassicLevel(pack, dest, {
591597
}
592598
const filename = path.join(dest, name);
593599
await serializeDocument(checkVolatile(doc, name, { collection, omitVolatile, existing, yaml }), filename, {
594-
yaml, yamlOptions, jsonOptions, transformSerialized, filename: name
600+
yaml, yamlOptions, jsonOptions, transformSerialized
595601
});
596602
if ( log ) console.log(`Wrote ${chalk.blue(name)}`);
597603
}
@@ -611,7 +617,8 @@ async function extractClassicLevel(pack, dest, {
611617
* @param {string} [extractOptions.existing] The location of existing serialized Documents.
612618
*/
613619
async function extractAdventure(doc, dest, { folderMap }={}, {
614-
yaml, yamlOptions, jsonOptions, log, folders, omitVolatile, transformEntry, transformName, transformSerialized, transformFolderName, existing
620+
yaml, yamlOptions, jsonOptions, log, folders, omitVolatile, transformEntry, transformName, transformSerialized,
621+
transformFolderName, existing
615622
}={}) {
616623
let adventureFolder;
617624

@@ -661,7 +668,7 @@ async function extractAdventure(doc, dest, { folderMap }={}, {
661668
omitVolatile, existing, yaml,
662669
collection: embeddedCollectionName
663670
}), filename, {
664-
yaml, yamlOptions, jsonOptions, transformSerialized, filename: embeddedName
671+
yaml, yamlOptions, jsonOptions, transformSerialized
665672
});
666673
if ( log ) console.log(`Wrote ${chalk.blue(embeddedName)}`);
667674
}
@@ -671,7 +678,7 @@ async function extractAdventure(doc, dest, { folderMap }={}, {
671678
// Write the adventure itself
672679
const filename = path.join(dest, name);
673680
await serializeDocument(checkVolatile(doc, name, { omitVolatile, existing, yaml }), filename, {
674-
yaml, yamlOptions, jsonOptions, transformSerialized, filename: name
681+
yaml, yamlOptions, jsonOptions, transformSerialized
675682
});
676683
if ( log ) console.log(`Wrote ${chalk.blue(name)}`);
677684
}
@@ -808,7 +815,7 @@ function findSourceFiles(root, { yaml=false, recursive=false }={}) {
808815
* @param {string} filename The filename to write it to.
809816
* @param {Partial<ExtractOptions>} [options] Options to configure serialization behavior.
810817
*/
811-
async function serializeDocument(doc, filename, { yaml, yamlOptions, jsonOptions, transformSerialized, filename: destName }={}) {
818+
async function serializeDocument(doc, filename, { yaml, yamlOptions, jsonOptions, transformSerialized }={}) {
812819
fs.mkdirSync(path.dirname(filename), { recursive: true });
813820
let serialized;
814821
if ( yaml ) serialized = YAML.dump(doc, yamlOptions);
@@ -817,8 +824,8 @@ async function serializeDocument(doc, filename, { yaml, yamlOptions, jsonOptions
817824
serialized = JSON.stringify(doc, replacer, space) + "\n";
818825
}
819826

820-
if (typeof transformSerialized === "function") {
821-
serialized = await transformSerialized(serialized, { yaml, filename: destName });
827+
if ( typeof transformSerialized === "function" ) {
828+
serialized = await transformSerialized(serialized, { yaml, filename });
822829
}
823830

824831
fs.writeFileSync(filename, serialized);

0 commit comments

Comments
 (0)