-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
369 additions
and
481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,3 +59,6 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
|
||
# test temp files | ||
tests/resources/output.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
const yaml = require('yaml'); | ||
const merge = require('lodash.merge'); | ||
const fs = require('./promisified-fs'); | ||
|
||
/** | ||
* Read and combine all the received yaml files | ||
* @param {Array} files yaml files array | ||
* @returns {Object|Array} combined yaml, it will be an array or an object according to received yamls format. | ||
* Also returns nothing when all the received files are empty. | ||
* @throws if try to combine an array yaml with an object yaml or any of the received files is invalid | ||
* @example | ||
* combineYaml(['myFile.yml', 'myOtherFile.yml']); | ||
* // Expected | ||
* { | ||
* myProp: myValue | ||
* myOtherProp: { myOtherValue: 1 } | ||
* } | ||
*/ | ||
async function combineYaml(files) { | ||
|
||
let combinedYaml; | ||
|
||
for(const file of files) { | ||
|
||
const rawFile = await fs.readFile(file, 'utf8'); | ||
const parsedFile = yaml.parse(rawFile); | ||
|
||
if(parsedFile === null) | ||
continue; | ||
|
||
if(Array.isArray(parsedFile)) { | ||
|
||
if(typeof combinedYaml === 'undefined') | ||
combinedYaml = []; | ||
|
||
if(Array.isArray(combinedYaml)) { | ||
combinedYaml = [...combinedYaml, ...parsedFile]; | ||
continue; | ||
} | ||
|
||
throw new Error('Couldn\'t concat yaml files: Can\'t combine objects with arrays.'); | ||
} | ||
|
||
if(typeof combinedYaml === 'undefined') | ||
combinedYaml = {}; | ||
|
||
if(!Array.isArray(parsedFile) && !Array.isArray(combinedYaml)) { | ||
combinedYaml = merge(combinedYaml, parsedFile); | ||
continue; | ||
} | ||
|
||
throw new Error('Couldn\'t concat yaml files: Can\'t combine arrays with objects.'); | ||
} | ||
|
||
return combinedYaml; | ||
} | ||
|
||
module.exports = combineYaml; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.