Skip to content

Commit 08669af

Browse files
committed
[#43] Fix issue with YAML serialisation.
1 parent dd8c918 commit 08669af

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.0.1
2+
3+
### Fixes
4+
- Fixed issue with YAML serialization.
5+
16
## 1.0.0
27

38
### Improvements

lib/package.mjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "fs";
22
import path from "path";
33
import Datastore from "nedb-promises";
44
import chalk from "chalk";
5-
import yaml from "js-yaml";
5+
import { default as YAML } from "js-yaml";
66
import { ClassicLevel } from "classic-level";
77

88
/* -------------------------------------------- */
@@ -214,7 +214,7 @@ async function compileNedb(pack, files, { log, transformEntry }={}) {
214214
const contents = fs.readFileSync(file, "utf8");
215215
const ext = path.extname(file);
216216
const isYaml = ext === ".yml" || ext === ".yaml";
217-
const doc = isYaml ? yaml.load(contents) : JSON.parse(contents);
217+
const doc = isYaml ? YAML.load(contents) : JSON.parse(contents);
218218
const key = doc._key;
219219
const [, collection] = key.split("!");
220220
// If the key starts with !folders, we should skip packing it as NeDB doesn't support folders.
@@ -267,7 +267,7 @@ async function compileClassicLevel(pack, files, { log, transformEntry }={}) {
267267
const contents = fs.readFileSync(file, "utf8");
268268
const ext = path.extname(file);
269269
const isYaml = ext === ".yml" || ext === ".yaml";
270-
const doc = isYaml ? yaml.load(contents) : JSON.parse(contents);
270+
const doc = isYaml ? YAML.load(contents) : JSON.parse(contents);
271271
const [, collection] = doc._key.split("!");
272272
if ( await transformEntry?.(doc) === false ) continue;
273273
await packDoc(doc, collection);
@@ -351,7 +351,7 @@ export async function extractPack(src, dest, {
351351
* @returns {Promise<void>}
352352
*/
353353
async function extractNedb(pack, dest, {
354-
yaml: asYaml, yamlOptions, jsonOptions, log, collection, transformEntry, transformName
354+
yaml, yamlOptions, jsonOptions, log, collection, transformEntry, transformName
355355
}={}) {
356356
// Load the NeDB file.
357357
const db = new Datastore({ filename: pack, autoload: true });
@@ -370,10 +370,10 @@ async function extractNedb(pack, dest, {
370370
if ( await transformEntry?.(doc) === false ) continue;
371371
let name = await transformName?.(doc);
372372
if ( !name ) {
373-
name = `${doc.name ? `${getSafeFilename(doc.name)}_${doc._id}` : doc._id}.${asYaml ? "yml" : "json"}`;
373+
name = `${doc.name ? `${getSafeFilename(doc.name)}_${doc._id}` : doc._id}.${yaml ? "yml" : "json"}`;
374374
}
375375
const filename = path.join(dest, name);
376-
serializeDocument(doc, filename, { yaml: asYaml, yamlOptions, jsonOptions });
376+
serializeDocument(doc, filename, { yaml, yamlOptions, jsonOptions });
377377
if ( log ) console.log(`Wrote ${chalk.blue(name)}`);
378378
}
379379
}
@@ -388,7 +388,7 @@ async function extractNedb(pack, dest, {
388388
* @returns {Promise<void>}
389389
*/
390390
async function extractClassicLevel(pack, dest, {
391-
yaml: asYaml, yamlOptions, jsonOptions, log, transformEntry, transformName
391+
yaml, yamlOptions, jsonOptions, log, transformEntry, transformName
392392
}) {
393393
// Load the directory as a ClassicLevel DB.
394394
const db = new ClassicLevel(pack, { keyEncoding: "utf8", valueEncoding: "json" });
@@ -411,10 +411,10 @@ async function extractClassicLevel(pack, dest, {
411411
if ( await transformEntry?.(doc) === false ) continue;
412412
let name = await transformName?.(doc);
413413
if ( !name ) {
414-
name = `${doc.name ? `${getSafeFilename(doc.name)}_${id}` : key}.${asYaml ? "yml" : "json"}`;
414+
name = `${doc.name ? `${getSafeFilename(doc.name)}_${id}` : key}.${yaml ? "yml" : "json"}`;
415415
}
416416
const filename = path.join(dest, name);
417-
serializeDocument(doc, filename, { yaml: asYaml, yamlOptions, jsonOptions });
417+
serializeDocument(doc, filename, { yaml, yamlOptions, jsonOptions });
418418
if ( log ) console.log(`Wrote ${chalk.blue(name)}`);
419419
}
420420

@@ -505,7 +505,7 @@ function findSourceFiles(root, { yaml=false, recursive=false }={}) {
505505
function serializeDocument(doc, filename, { yaml, yamlOptions, jsonOptions }={}) {
506506
fs.mkdirSync(path.dirname(filename), { recursive: true });
507507
let serialized;
508-
if ( yaml ) serialized = yaml.dump(doc, yamlOptions);
508+
if ( yaml ) serialized = YAML.dump(doc, yamlOptions);
509509
else {
510510
const { replacer=null, space=2 } = jsonOptions;
511511
serialized = JSON.stringify(doc, replacer, space);

0 commit comments

Comments
 (0)