mirror of
https://github.com/serverless/serverless.git
synced 2025-12-08 19:46:03 +00:00
28 lines
725 B
JavaScript
28 lines
725 B
JavaScript
'use strict';
|
|
|
|
const path = require('path');
|
|
const YAML = require('js-yaml');
|
|
const fse = require('./fse');
|
|
|
|
function writeFile(filePath, conts) {
|
|
let contents = conts || '';
|
|
|
|
return fse.mkdirs(path.dirname(filePath))
|
|
.then(() => {
|
|
if (filePath.indexOf('.json') !== -1 && typeof contents !== 'string') {
|
|
contents = JSON.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.writeFile(filePath, contents);
|
|
});
|
|
}
|
|
|
|
module.exports = writeFile;
|