serverless/lib/utils/fs/writeFileSync.js
Erik Erikson 67442013b9 update json-cycle version
use pretty-print capability
update tests
2017-11-17 00:01:40 +01:00

28 lines
706 B
JavaScript

'use strict';
const fse = require('./fse');
const path = require('path');
const jc = require('json-cycle');
const YAML = require('js-yaml');
function writeFileSync(filePath, conts) {
let contents = conts || '';
fse.mkdirsSync(path.dirname(filePath));
if (filePath.indexOf('.json') !== -1 && typeof contents !== 'string') {
contents = jc.stringify(contents, null, 2);
}
const yamlFileExists = (filePath.indexOf('.yaml') !== -1);
const ymlFileExists = (filePath.indexOf('.yml') !== -1);
if ((yamlFileExists || ymlFileExists) && typeof contents !== 'string') {
contents = YAML.dump(contents);
}
return fse.writeFileSync(filePath, contents);
}
module.exports = writeFileSync;