Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/persistence / dist / adapters / server / readwrite.js
Size: Mime:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @file https://github.com/simonlast/node-persist
 */
const path_1 = require("path");
const fs_1 = require("fs");
const mkdirp_1 = require("mkdirp");
const stringify_1 = require("../../deps/stringify");
/**
 * @tutorial https://shapeshed.com/writing-cross-platform-node/
 * @description path separator for any platform
 * @type {Path.sep | '\\' | '/'}
 */
const separator = path_1.sep;
/**
 * @tutorial http://stackabuse.com/read-files-with-node-js/
 * @param {String} absolutePath path to read from
 */
function read(absolutePath) {
    makePathIfNeeded(absolutePath);
    return fs_1.readFileSync(absolutePath, 'utf8');
}
exports.read = read;
function tryJSON(json) {
    try {
        const parsed = JSON.parse(json);
        return parsed;
    }
    catch (parseError) {
        return false;
    }
}
/**
 * @return parsed json or fallback object
 */
function readJSON(absolutePath, fallback = { readfailed: true }) {
    return tryJSON(read(absolutePath)) || fallback;
}
exports.readJSON = readJSON;
/**
 * @description split file into [directory, filename]
 *              check if directory exists
 *              mkdirp creates path compat
 *              fs always can create a single file, but not a dir
 *
 * @return true if it had to create it, false if not
 */
function makePathIfNeeded(absolutePath) {
    // get the final dir excluding the file
    const finalOutputDirList = absolutePath.split(separator);
    finalOutputDirList.pop();
    const finalOutputDir = finalOutputDirList.join(separator);
    // make the final dir if it does not exist
    if (fs_1.existsSync(finalOutputDir) === false) {
        mkdirp_1.sync(finalOutputDir);
        return true;
    }
    return false;
}
/**
 * @description write contents to a path
 * @param absolutePath path to write content to
 * @param content serializes content to write to file
 * @return wrote successfully
 */
function write(absolutePath, content) {
    let contents = content;
    // if !isPrimitive?
    if (typeof contents !== 'string') {
        contents = stringify_1.stringify(content);
    }
    makePathIfNeeded(absolutePath);
    return fs_1.writeFileSync(absolutePath, contents, 'utf8');
}
exports.write = write;
read.json = readJSON;
const exists = fs_1.existsSync;
exports.exists = exists;
//# sourceMappingURL=readwrite.js.map