All files oc-gen.js

83.33% Statements 20/24
66.66% Branches 4/6
66.66% Functions 2/3
83.33% Lines 20/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46      1x 1x 1x 1x 1x   1x         1x           1x   1x     1x 1x 1x     1x 1x   1x     1x 1x 1x         1x 1x  
#!/usr/bin/env node
// @ts-check
 
const fs = require('fs'),
  path = require('path'),
  { preload, load, dump } = require('./index'),
  { createCommand } = require('commander'),
  { find, mapValues, merge, get } = require('lodash');
 
const opts = createCommand('genconf')
.argument('<deployment>', 'deployment YAML file to modify')
.argument('[config...]', 'configuration files to inject')
.parse(process.argv);
 
process.on('unhandledRejection', function(reason) {
  console.warn(reason);
  process.exitCode = 1;
});
 
/** @type {string} */
const outFile = opts.processedArgs[0];
/** @type {string[]} */
const inFiles = opts.processedArgs[1];
 
/** @type {{ [name: string]: string }} */
const confFiles = {};
inFiles.forEach((conf) => {
  confFiles[path.basename(conf)] = preload(fs.readFileSync(conf).toString());
});
 
const output = load(fs.readFileSync(outFile).toString());
const config = find(get(output, [ 'items' ]), { kind: 'ConfigMap' });
 
Iif (!config) {
  throw new Error('failed to find ConfigMap in: ' + outFile);
}
config.data = mapValues(config.data, (data, name) => {
  Eif (name.endsWith('.yaml') || name.endsWith('.yml')) {
    return preload(data);
  }
  return data;
});
 
config.data  = merge(config.data, confFiles);
console.log(dump(output));