Skip to content

Commit

Permalink
[#43] Fix issue with YAML serialisation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyorl committed Nov 17, 2023
1 parent dd8c918 commit 08669af
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.1

### Fixes
- Fixed issue with YAML serialization.

## 1.0.0

### Improvements
Expand Down
20 changes: 10 additions & 10 deletions lib/package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs";
import path from "path";
import Datastore from "nedb-promises";
import chalk from "chalk";
import yaml from "js-yaml";
import { default as YAML } from "js-yaml";
import { ClassicLevel } from "classic-level";

/* -------------------------------------------- */
Expand Down Expand Up @@ -214,7 +214,7 @@ async function compileNedb(pack, files, { log, transformEntry }={}) {
const contents = fs.readFileSync(file, "utf8");
const ext = path.extname(file);
const isYaml = ext === ".yml" || ext === ".yaml";
const doc = isYaml ? yaml.load(contents) : JSON.parse(contents);
const doc = isYaml ? YAML.load(contents) : JSON.parse(contents);
const key = doc._key;
const [, collection] = key.split("!");
// If the key starts with !folders, we should skip packing it as NeDB doesn't support folders.
Expand Down Expand Up @@ -267,7 +267,7 @@ async function compileClassicLevel(pack, files, { log, transformEntry }={}) {
const contents = fs.readFileSync(file, "utf8");
const ext = path.extname(file);
const isYaml = ext === ".yml" || ext === ".yaml";
const doc = isYaml ? yaml.load(contents) : JSON.parse(contents);
const doc = isYaml ? YAML.load(contents) : JSON.parse(contents);
const [, collection] = doc._key.split("!");
if ( await transformEntry?.(doc) === false ) continue;
await packDoc(doc, collection);
Expand Down Expand Up @@ -351,7 +351,7 @@ export async function extractPack(src, dest, {
* @returns {Promise<void>}
*/
async function extractNedb(pack, dest, {
yaml: asYaml, yamlOptions, jsonOptions, log, collection, transformEntry, transformName
yaml, yamlOptions, jsonOptions, log, collection, transformEntry, transformName
}={}) {
// Load the NeDB file.
const db = new Datastore({ filename: pack, autoload: true });
Expand All @@ -370,10 +370,10 @@ async function extractNedb(pack, dest, {
if ( await transformEntry?.(doc) === false ) continue;
let name = await transformName?.(doc);
if ( !name ) {
name = `${doc.name ? `${getSafeFilename(doc.name)}_${doc._id}` : doc._id}.${asYaml ? "yml" : "json"}`;
name = `${doc.name ? `${getSafeFilename(doc.name)}_${doc._id}` : doc._id}.${yaml ? "yml" : "json"}`;
}
const filename = path.join(dest, name);
serializeDocument(doc, filename, { yaml: asYaml, yamlOptions, jsonOptions });
serializeDocument(doc, filename, { yaml, yamlOptions, jsonOptions });
if ( log ) console.log(`Wrote ${chalk.blue(name)}`);
}
}
Expand All @@ -388,7 +388,7 @@ async function extractNedb(pack, dest, {
* @returns {Promise<void>}
*/
async function extractClassicLevel(pack, dest, {
yaml: asYaml, yamlOptions, jsonOptions, log, transformEntry, transformName
yaml, yamlOptions, jsonOptions, log, transformEntry, transformName
}) {
// Load the directory as a ClassicLevel DB.
const db = new ClassicLevel(pack, { keyEncoding: "utf8", valueEncoding: "json" });
Expand All @@ -411,10 +411,10 @@ async function extractClassicLevel(pack, dest, {
if ( await transformEntry?.(doc) === false ) continue;
let name = await transformName?.(doc);
if ( !name ) {
name = `${doc.name ? `${getSafeFilename(doc.name)}_${id}` : key}.${asYaml ? "yml" : "json"}`;
name = `${doc.name ? `${getSafeFilename(doc.name)}_${id}` : key}.${yaml ? "yml" : "json"}`;
}
const filename = path.join(dest, name);
serializeDocument(doc, filename, { yaml: asYaml, yamlOptions, jsonOptions });
serializeDocument(doc, filename, { yaml, yamlOptions, jsonOptions });
if ( log ) console.log(`Wrote ${chalk.blue(name)}`);
}

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

0 comments on commit 08669af

Please sign in to comment.