// Dependencies
const { map } = require('lodash');
// Relative
const {
  contentDir,
} = require('../src/site/stages/build/process-cms-exports/helpers');
const assembleEntityTree = require('../src/site/stages/build/process-cms-exports')(
  contentDir,
);
const {
  readAllNodeNames,
  readEntity,
} = require('../src/site/stages/build/process-cms-exports/helpers');

const fileNames = readAllNodeNames();
const entities = map(fileNames, entityDetails =>
  readEntity(contentDir, ...entityDetails),
);

// Assemble all the nodes
// const modifiedEntities = map(entities, entity => assembleEntityTree(entity));

// Assemble only the first node for debugging
const modifiedEntities = map([entities[0]], entity =>
  assembleEntityTree(entity),
);

// eslint-disable-next-line
console.log('Number of files:', modifiedEntities.length);
// console.log('First node:', JSON.stringify(modifiedEntities[0], null, 2));

// Dependencies
const assert = require('assert');
const commandLineArgs = require('command-line-args');
const { map } = require('lodash');
// Relative
const {
  contentDir,
} = require('../src/site/stages/build/process-cms-exports/helpers');
const assembleEntityTree = require('../src/site/stages/build/process-cms-exports')(
  contentDir,
);
const {
  readAllNodeNames,
  readEntity,
} = require('../src/site/stages/build/process-cms-exports/helpers');

const optionDefinitions = [
  { name: 'count', alias: 'c', type: Number },
  { name: 'node', alias: 'n', type: String },
  { name: 'print', alias: 'p', type: Number },
];

const { count, node: nodeName, print: printIndex } = commandLineArgs(
  optionDefinitions,
);

if (nodeName) {
  const nodeNamePieces = nodeName.split('.').slice(0, 2);
  assert(
    nodeNamePieces.length === 2,
    'Node needs to be the filename of an entity. E.g. node.<uuid>.json',
  );

  const node = assembleEntityTree(readEntity(contentDir, ...nodeNamePieces));
  // eslint-disable-next-line no-console
  console.log(JSON.stringify(node, null, 2));
} else {
  // Make sure the printIndex is valid if passed
  if (printIndex) {
    if (count)
      assert(
        printIndex < count,
        `Print index (${printIndex}) must be less than node count (${count}) to point to a valid node.`,
      );
    assert(printIndex >= 0, `Print index (${printIndex}) must be >= 0`);
  }

  const fileNames = readAllNodeNames();
  const entities = map(fileNames, entityDetails =>
    readEntity(contentDir, ...entityDetails),
  );

  const modifiedEntities = map(
    entities.slice(0, count || entities.length),
    entity => assembleEntityTree(entity),
  );

  // eslint-disable-next-line no-console
  console.log('Number of files:', modifiedEntities.length);
  if (printIndex !== undefined) {
    // eslint-disable-next-line no-console
    console.log(
      `Node ${printIndex}:`,
      JSON.stringify(modifiedEntities[printIndex], null, 2),
    );
  }
}

Update cms export build test (#11126)