Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "fs";
import yaml from "js-yaml";
import * as yaml from "yaml";
import path from "path";
import * as os from "os";

Expand Down Expand Up @@ -41,8 +41,8 @@ export default class Config {
this.configPath = path.join(basePath, ".fvttrc.yml");

// Ensure the config file exists
if ( !fs.existsSync(this.configPath) ) fs.writeFileSync(this.configPath, yaml.dump({}));
this.#config = yaml.load(fs.readFileSync(this.configPath, "utf8"));
if ( !fs.existsSync(this.configPath) ) fs.writeFileSync(this.configPath, yaml.stringify({}));
this.#config = yaml.parse(fs.readFileSync(this.configPath, "utf8"));
}

/* -------------------------------------------- */
Expand Down Expand Up @@ -102,7 +102,7 @@ export default class Config {
* Write the configuration to disk
*/
#writeConfig() {
fs.writeFileSync(this.configPath, yaml.dump(this.#config));
fs.writeFileSync(this.configPath, yaml.stringify(this.#config));
}

/* -------------------------------------------- */
Expand All @@ -114,7 +114,7 @@ export default class Config {
loadLocalConf(configFile) {
if ( !fs.existsSync(configFile) ) return;
/** @type {Record<string, any>} */
const conf = yaml.load(fs.readFileSync(configFile, "utf8"));
const conf = yaml.parse(fs.readFileSync(configFile, "utf8"));
this.configPath = configFile;
for ( const key of Object.keys(conf) ) this.#config[key] = conf[key];
}
Expand Down
21 changes: 14 additions & 7 deletions lib/package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import os from "node:os";
import path from "node:path";
import Datastore from "nedb-promises";
import chalk from "chalk";
import { default as YAML } from "js-yaml";
import * as YAML from "yaml";
import { ClassicLevel } from "classic-level";

/* -------------------------------------------- */
Expand Down Expand Up @@ -42,7 +42,7 @@ import { ClassicLevel } from "classic-level";

/**
* @typedef {PackageOptions} ExtractOptions
* @property {object} [yamlOptions] Options to pass to yaml.dump when serializing Documents.
* @property {object} [yamlOptions] Options to pass to yaml.stringify when serializing Documents.
* @property {JSONOptions} [jsonOptions] Options to pass to JSON.stringify when serializing Documents.
* @property {DocumentType} [documentType] Required only for NeDB packs in order to generate a correct key.
* @property {boolean} [clean] Delete the destination directory before unpacking.
Expand Down Expand Up @@ -285,7 +285,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.parse(contents) : JSON.parse(contents);
if ( !doc._key ) continue;
if ( doc._key.startsWith("!adventures") ) await reconstructAdventure(path.dirname(file), doc, {
transformEntry, log
Expand Down Expand Up @@ -345,7 +345,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.parse(contents) : JSON.parse(contents);
if ( !doc._key ) continue;
if ( doc._key.startsWith("!adventures") ) await reconstructAdventure(path.dirname(file), doc, {
transformEntry, log
Expand Down Expand Up @@ -398,7 +398,7 @@ async function reconstructAdventure(src, doc, { transformEntry, log }={}) {
}
const ext = path.extname(file);
const isYaml = ext === ".yml" || ext === ".yaml";
entry = isYaml ? YAML.load(contents) : JSON.parse(contents);
entry = isYaml ? YAML.parse(contents) : JSON.parse(contents);
if ( await transformEntry?.(entry, context) === false ) continue;
}
entries.push(entry);
Expand Down Expand Up @@ -789,7 +789,14 @@ 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) {
// Mimic js-yaml format as closely as possible
yamlOptions = {
singleQuote: true,
...yamlOptions
};
serialized = YAML.stringify(doc, undefined, yamlOptions);
}
else {
const { replacer=null, space=2 } = jsonOptions;
serialized = JSON.stringify(doc, replacer, space) + "\n";
Expand All @@ -813,7 +820,7 @@ function serializeDocument(doc, filename, { yaml, yamlOptions, jsonOptions }={})
*/
function checkVolatile(doc, name, { omitVolatile, existing, yaml, collection }={}) {
if ( !omitVolatile || !existing || !("_stats" in doc) ) return doc;
const parse = yaml ? YAML.load : JSON.parse;
const parse = yaml ? YAML.parse : JSON.parse;
try {
const base = parse(fs.readFileSync(path.join(existing, name), { encoding: "utf8" }));
if ( !base || !("_stats" in base) ) return doc;
Expand Down
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"chalk": "^5.4.1",
"classic-level": "^1.4.1",
"esm": "^3.2.25",
"js-yaml": "^4.1.0",
"mkdirp": "^3.0.1",
"nedb-promises": "^6.2.3",
"yaml": "^2.8.1",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down