Skip to content

Commit

Permalink
Merge 91b5b0c into 44d7849
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Nov 12, 2019
2 parents 44d7849 + 91b5b0c commit 921d2bf
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 481 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ typings/

# next.js build output
.next

# test temp files
tests/resources/output.yml
60 changes: 60 additions & 0 deletions lib/utils/combine-yaml.js
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;
5 changes: 4 additions & 1 deletion lib/utils/promisified-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ const util = require('util');
[
'stat',
'readdir',
'readFile',
'writeFile',
'mkdir'
'mkdir',
'rmdir',
'unlink'

].forEach(method => {
fs[method] = util.promisify(fs[method]);
Expand Down
20 changes: 12 additions & 8 deletions lib/yml-builder.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

const path = require('path');
const YAML = require('yaml');
const mergeYaml = require('merge-yaml');
const yaml = require('yaml');
const recursive = require('recursive-readdir');
const fs = require('./utils/promisified-fs');
const log = require('./utils/lllog-wrapper');
const combineYaml = require('./utils/combine-yaml');

const YmlBuilderError = require('./yml-builder-error');

Expand Down Expand Up @@ -43,7 +43,6 @@ class YmlBuilder {
// If the directory not exists
return false;
}

}

async _validateOutputPath(filePath) {
Expand Down Expand Up @@ -95,7 +94,6 @@ class YmlBuilder {
} catch(err) {
throw new YmlBuilderError(`An error ocurred while reading source directory: ${err.message}`, YmlBuilderError.codes.READ_FILES_ERROR);
}

}


Expand All @@ -120,13 +118,19 @@ class YmlBuilder {
await this._validateOutputPath(output);
log.confirm('Output file path is valid', '✓ YML-BUILDER');

log.message('Reading source files...', '⚙ YML-BUILDER');

const sourceFiles = await this._getSourceFiles(input);

log.message('Building ymls...', '⚙ YML-BUILDER');

let mergedYaml;
let combinedYaml;

try {
mergedYaml = mergeYaml(await this._getSourceFiles(input));
mergedYaml = YAML.stringify(mergedYaml);

combinedYaml = await combineYaml(sourceFiles);
combinedYaml = yaml.stringify(combinedYaml);

} catch(err) {
log.error(err.message, 'Invalid yml', '⨯ YML-BUILDER');
throw new YmlBuilderError('Failed when building ymls', YmlBuilderError.codes.YML_BUILD_ERROR);
Expand All @@ -137,7 +141,7 @@ class YmlBuilder {
log.message(`Writing file '${output}'...`, '⚙ YML-BUILDER');

try {
await fs.writeFile(output, mergedYaml, { recursive: true });
await fs.writeFile(output, combinedYaml, { recursive: true });
} catch(err) {
log.error(err.message, err.code || 'Unknown fs write error', '⨯ YML-BUILDER');
throw new YmlBuilderError(`Unable to write file '${output}'`, YmlBuilderError.codes.WRITE_OUTPUT_FILE_ERROR);
Expand Down

0 comments on commit 921d2bf

Please sign in to comment.