Repository URL to install this package:
|
Version:
5.0.0-rc.9 ▾
|
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
const writeFileAsync = promisify(fs.writeFile);
const readFileAsync = promisify(fs.readFile);
/**
* File utilities
*
* @module @doodle/i18n/onesky/dist/utils
*/
/**
* Write a JSON object to a file
*
* @param {String} file
* @param {object} messages
* @returns {Promise}
* @async
*/
export const writeJsonFile = async (file, messages) => {
const content = JSON.stringify(messages);
try {
path
.dirname(file)
.split('/')
.reduce((currentPath, directory) => {
if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath);
}
return `${currentPath}/${directory}`;
});
} catch (e) {
// ignore
}
return writeFileAsync(file, content);
};
/**
* Read a JSON file and parse the contents
*
* @param {string} file
* @returns {Promise<object>}
* @async
*/
export const readJsonFile = async file => {
const content = await readFileAsync(file);
return JSON.parse(content);
};
/**
* Read OneSky resource configuration file, `.oneskyrc`
*
* @param {string} file
* @returns {Promise<object>}
* @async
*/
export const getResourceConfiguration = async (file = `${process.cwd()}/.oneskyrc`) => readJsonFile(file);